refactor: 拆分工作台查询与模板变更逻辑
This commit is contained in:
1 parent
3003e7801d
commit
7e1dd543fb
3 files changed
+217
-141
No files matched your search
@@ -1,30 +1,25 @@
|
|||||||
import {
|
import { Button, Card, Form, Input, Modal, Select, Space, Table, Tag, Typography } from "antd";
|
||||||
Button,
|
|
||||||
Card,
|
|
||||||
Form,
|
|
||||||
Input,
|
|
||||||
Modal,
|
|
||||||
Select,
|
|
||||||
Space,
|
|
||||||
Table,
|
|
||||||
Tag,
|
|
||||||
Typography,
|
|
||||||
message,
|
|
||||||
} from "antd";
|
|
||||||
import { useMutation, useQuery, useQueryClient } from "@tanstack/react-query";
|
|
||||||
import { useNavigate } from "react-router-dom";
|
import { useNavigate } from "react-router-dom";
|
||||||
import { createApiClient } from "../../lib/api.js";
|
|
||||||
import { ApiClientError } from "@personal-brand/api-client";
|
import { ApiClientError } from "@personal-brand/api-client";
|
||||||
|
import { createApiClient } from "../../lib/api.js";
|
||||||
import type {
|
import type {
|
||||||
BrandFieldDefinition,
|
BrandFieldDefinition,
|
||||||
BrandTemplateKey,
|
BrandTemplateKey,
|
||||||
InquiryForm,
|
InquiryForm,
|
||||||
} from "@personal-brand/api-client";
|
} from "@personal-brand/api-client";
|
||||||
|
import { useQuery, useQueryClient } from "@tanstack/react-query";
|
||||||
import { useState } from "react";
|
import { useState } from "react";
|
||||||
|
import {
|
||||||
|
useWorkspaceQueries,
|
||||||
|
useTemplateMutations,
|
||||||
|
useFieldMutations,
|
||||||
|
useInquiryFormMutations,
|
||||||
|
} from "./workspace-mutations.js";
|
||||||
|
import { supportsIndustry, type IndustryTarget } from "./workspace-templates.js";
|
||||||
import { AdminPageHeader } from "../../components/AdminPageHeader.js";
|
import { AdminPageHeader } from "../../components/AdminPageHeader.js";
|
||||||
import { MutationError } from "../../components/QueryState.js";
|
import { MutationError } from "../../components/QueryState.js";
|
||||||
|
|
||||||
const inquiryFieldTypes = [
|
export const inquiryFieldTypes = [
|
||||||
{ value: "text", label: "简短文字" },
|
{ value: "text", label: "简短文字" },
|
||||||
{ value: "textarea", label: "详细说明" },
|
{ value: "textarea", label: "详细说明" },
|
||||||
{ value: "phone", label: "联系电话" },
|
{ value: "phone", label: "联系电话" },
|
||||||
@@ -36,42 +31,26 @@ const inquiryFieldTypes = [
|
|||||||
{ value: "multi-select", label: "多项选择" },
|
{ value: "multi-select", label: "多项选择" },
|
||||||
];
|
];
|
||||||
|
|
||||||
function readOptionLabel(option: unknown): string {
|
export function readOptionLabel(option: unknown): string {
|
||||||
const value = option as { label?: unknown } | null | undefined;
|
const value = option as { label?: unknown } | null | undefined;
|
||||||
return typeof value?.label === "string" ? value.label : "";
|
return typeof value?.label === "string" ? value.label : "";
|
||||||
}
|
}
|
||||||
|
|
||||||
function normalizeOptionLabels(options: unknown): Array<{ label: string; value: string }> {
|
export function normalizeOptionLabels(options: unknown): Array<{ label: string; value: string }> {
|
||||||
return (Array.isArray(options) ? options : [])
|
return (Array.isArray(options) ? options : [])
|
||||||
.map(readOptionLabel)
|
.map(readOptionLabel)
|
||||||
.filter((label) => label.trim())
|
.filter((label) => label.trim())
|
||||||
.map((label) => ({ label, value: label }));
|
.map((label) => ({ label, value: label }));
|
||||||
}
|
}
|
||||||
|
|
||||||
function supportsIndustry(
|
|
||||||
visual: { supportedIndustryTemplates?: unknown },
|
|
||||||
industry: string,
|
|
||||||
): boolean {
|
|
||||||
return (
|
|
||||||
Array.isArray(visual.supportedIndustryTemplates) &&
|
|
||||||
visual.supportedIndustryTemplates.includes(industry)
|
|
||||||
);
|
|
||||||
}
|
|
||||||
|
|
||||||
export function WorkspacePage() {
|
export function WorkspacePage() {
|
||||||
const navigate = useNavigate();
|
const navigate = useNavigate();
|
||||||
const client = createApiClient();
|
|
||||||
const queryClient = useQueryClient();
|
const queryClient = useQueryClient();
|
||||||
const [fieldOpen, setFieldOpen] = useState(false);
|
const [fieldOpen, setFieldOpen] = useState(false);
|
||||||
const [formOpen, setFormOpen] = useState(false);
|
const [formOpen, setFormOpen] = useState(false);
|
||||||
const [editingForm, setEditingForm] = useState<InquiryForm | null>(null);
|
const [editingForm, setEditingForm] = useState<InquiryForm | null>(null);
|
||||||
const [visualTarget, setVisualTarget] = useState<string | null>(null);
|
const [visualTarget, setVisualTarget] = useState<string | null>(null);
|
||||||
const [industryTarget, setIndustryTarget] = useState<{
|
const [industryTarget, setIndustryTarget] = useState<IndustryTarget | null>(null);
|
||||||
key: "personal" | "food" | "doors";
|
|
||||||
name: string;
|
|
||||||
description: string;
|
|
||||||
capabilities: string[];
|
|
||||||
} | null>(null);
|
|
||||||
const [previewIndustry, setPreviewIndustry] = useState<BrandTemplateKey>("personal");
|
const [previewIndustry, setPreviewIndustry] = useState<BrandTemplateKey>("personal");
|
||||||
const [previewVisual, setPreviewVisual] = useState("linjing-editorial");
|
const [previewVisual, setPreviewVisual] = useState("linjing-editorial");
|
||||||
const [form] = Form.useForm<Record<string, unknown>>();
|
const [form] = Form.useForm<Record<string, unknown>>();
|
||||||
@@ -89,126 +68,35 @@ export function WorkspacePage() {
|
|||||||
});
|
});
|
||||||
setFormOpen(true);
|
setFormOpen(true);
|
||||||
};
|
};
|
||||||
const query = useQuery({
|
const { query, runtimeQuery, templateCatalog, forms, templatePreview } = useWorkspaceQueries();
|
||||||
queryKey: ["admin-workspace"],
|
|
||||||
queryFn: () => client.getAdminWorkspace(),
|
|
||||||
});
|
|
||||||
const runtimeQuery = useQuery({
|
|
||||||
queryKey: ["public-runtime-workspace"],
|
|
||||||
queryFn: () => client.getPublicWorkspace(),
|
|
||||||
});
|
|
||||||
const templateCatalog = useQuery({
|
|
||||||
queryKey: ["template-catalog"],
|
|
||||||
queryFn: () => client.listPublicTemplates(),
|
|
||||||
});
|
|
||||||
const combinationPreview = useQuery({
|
const combinationPreview = useQuery({
|
||||||
queryKey: ["template-combination-preview", previewIndustry, previewVisual],
|
queryKey: ["template-combination-preview", previewIndustry, previewVisual],
|
||||||
queryFn: () => client.previewPublicTemplateCombination(previewIndustry, previewVisual),
|
queryFn: () =>
|
||||||
|
createApiClient().previewPublicTemplateCombination(previewIndustry, previewVisual),
|
||||||
enabled: Boolean(templateCatalog.data),
|
enabled: Boolean(templateCatalog.data),
|
||||||
});
|
});
|
||||||
const compatibleVisuals = (templateCatalog.data?.visuals ?? []).filter((visual) =>
|
const compatibleVisuals = (templateCatalog.data?.visuals ?? []).filter((visual) =>
|
||||||
supportsIndustry(visual, query.data?.brand?.templateKey ?? "personal"),
|
supportsIndustry(visual, query.data?.brand?.templateKey ?? "personal"),
|
||||||
);
|
);
|
||||||
const visualTemplate = useMutation({
|
const { visualTemplate, rollbackVisual, install, upgrade, rollback } = useTemplateMutations({
|
||||||
mutationFn: (key: string) =>
|
runtimeConfig: runtimeQuery.data?.brand?.config ?? undefined,
|
||||||
client.updateAdminBrand({
|
queryClient,
|
||||||
config: { ...(runtimeQuery.data?.brand?.config ?? {}), visualTemplateKey: key },
|
|
||||||
}),
|
|
||||||
onSuccess: () => {
|
|
||||||
message.success("视觉模板已切换");
|
|
||||||
void runtimeQuery.refetch();
|
|
||||||
void queryClient.invalidateQueries({ queryKey: ["admin-brand"] });
|
|
||||||
},
|
|
||||||
});
|
});
|
||||||
const visualHistory = useQuery({
|
const visualHistory = useQuery({
|
||||||
queryKey: ["visual-template-history"],
|
queryKey: ["visual-template-history"],
|
||||||
queryFn: () => client.getAdminVisualTemplateHistory(),
|
queryFn: () => createApiClient().getAdminVisualTemplateHistory(),
|
||||||
});
|
});
|
||||||
const rollbackVisual = useMutation({
|
const { createField, removeField } = useFieldMutations({
|
||||||
mutationFn: () => client.rollbackAdminVisualTemplate(),
|
form,
|
||||||
onSuccess: () => {
|
queryClient,
|
||||||
message.success("视觉模板已回滚");
|
onClose: () => setFieldOpen(false),
|
||||||
void runtimeQuery.refetch();
|
|
||||||
void visualHistory.refetch();
|
|
||||||
},
|
|
||||||
});
|
});
|
||||||
const install = useMutation({
|
const { createForm, updateForm } = useInquiryFormMutations({
|
||||||
mutationFn: (key: "personal" | "food" | "doors") => client.installAdminTemplate(key),
|
form: inquiryForm,
|
||||||
onSuccess: () => {
|
queryClient,
|
||||||
message.success("模板已安装");
|
onClose: () => {
|
||||||
void queryClient.invalidateQueries({ queryKey: ["admin-workspace"] });
|
|
||||||
},
|
|
||||||
});
|
|
||||||
const createField = useMutation({
|
|
||||||
mutationFn: (values: Omit<BrandFieldDefinition, "id" | "source">) =>
|
|
||||||
client.createAdminField(values),
|
|
||||||
onSuccess: () => {
|
|
||||||
message.success("字段已创建");
|
|
||||||
setFieldOpen(false);
|
|
||||||
form.resetFields();
|
|
||||||
void queryClient.invalidateQueries({ queryKey: ["admin-workspace"] });
|
|
||||||
},
|
|
||||||
});
|
|
||||||
const removeField = useMutation({
|
|
||||||
mutationFn: (id: string) => client.deleteAdminField(id),
|
|
||||||
onSuccess: () => void queryClient.invalidateQueries({ queryKey: ["admin-workspace"] }),
|
|
||||||
});
|
|
||||||
const forms = useQuery({
|
|
||||||
queryKey: ["admin-inquiry-forms"],
|
|
||||||
queryFn: () => client.listAdminInquiryForms(),
|
|
||||||
});
|
|
||||||
const templatePreview = useQuery({
|
|
||||||
queryKey: ["admin-template-preview"],
|
|
||||||
queryFn: () => client.getAdminTemplateUpgradePreview(),
|
|
||||||
enabled: Boolean(query.data?.installations?.length),
|
|
||||||
});
|
|
||||||
const upgrade = useMutation({
|
|
||||||
mutationFn: () => client.upgradeAdminTemplate(),
|
|
||||||
onSuccess: () => {
|
|
||||||
message.success("模板已升级,已有内容未被覆盖");
|
|
||||||
void queryClient.invalidateQueries({ queryKey: ["admin-workspace"] });
|
|
||||||
void queryClient.invalidateQueries({ queryKey: ["admin-template-preview"] });
|
|
||||||
},
|
|
||||||
});
|
|
||||||
const rollback = useMutation({
|
|
||||||
mutationFn: () => client.rollbackAdminTemplate(),
|
|
||||||
onSuccess: () => {
|
|
||||||
message.success("模板版本已回滚,现有内容未删除");
|
|
||||||
void queryClient.invalidateQueries({ queryKey: ["admin-workspace"] });
|
|
||||||
void queryClient.invalidateQueries({ queryKey: ["admin-template-preview"] });
|
|
||||||
},
|
|
||||||
});
|
|
||||||
const createForm = useMutation({
|
|
||||||
mutationFn: (values: {
|
|
||||||
name: string;
|
|
||||||
offeringId?: string;
|
|
||||||
purpose?: InquiryForm["purpose"];
|
|
||||||
targetType?: InquiryForm["targetType"];
|
|
||||||
submissionAction?: InquiryForm["submissionAction"];
|
|
||||||
fieldsSnapshot: unknown[];
|
|
||||||
}) => client.createAdminInquiryForm(values),
|
|
||||||
onSuccess: () => {
|
|
||||||
message.success("咨询表单已创建");
|
|
||||||
setFormOpen(false);
|
|
||||||
inquiryForm.resetFields();
|
|
||||||
void queryClient.invalidateQueries({ queryKey: ["admin-inquiry-forms"] });
|
|
||||||
},
|
|
||||||
});
|
|
||||||
const updateForm = useMutation({
|
|
||||||
mutationFn: ({ id, body }: { id: string; body: Partial<InquiryForm> }) =>
|
|
||||||
client.updateAdminInquiryForm(id, {
|
|
||||||
...(body.name === undefined ? {} : { name: body.name }),
|
|
||||||
...(body.offeringId == null ? {} : { offeringId: body.offeringId }),
|
|
||||||
...(body.fieldsSnapshot === undefined ? {} : { fieldsSnapshot: body.fieldsSnapshot }),
|
|
||||||
...(body.isEnabled === undefined ? {} : { isEnabled: body.isEnabled }),
|
|
||||||
}),
|
|
||||||
onSuccess: () => {
|
|
||||||
message.success("咨询表单已更新");
|
|
||||||
setFormOpen(false);
|
setFormOpen(false);
|
||||||
setEditingForm(null);
|
setEditingForm(null);
|
||||||
inquiryForm.resetFields();
|
|
||||||
void queryClient.invalidateQueries({ queryKey: ["admin-inquiry-forms"] });
|
|
||||||
void queryClient.invalidateQueries({ queryKey: ["public-runtime-workspace"] });
|
|
||||||
},
|
},
|
||||||
});
|
});
|
||||||
if (query.isLoading) return <Card loading />;
|
if (query.isLoading) return <Card loading />;
|
||||||
|
|||||||
@@ -0,0 +1,170 @@
|
|||||||
|
import { message } from "antd";
|
||||||
|
import { useMutation, useQuery } from "@tanstack/react-query";
|
||||||
|
import type { FormInstance } from "antd";
|
||||||
|
import type {
|
||||||
|
BrandFieldDefinition,
|
||||||
|
BrandTemplateKey,
|
||||||
|
InquiryForm,
|
||||||
|
} from "@personal-brand/api-client";
|
||||||
|
import { createApiClient } from "../../lib/api.js";
|
||||||
|
|
||||||
|
export function useWorkspaceQueries(client = createApiClient()) {
|
||||||
|
const query = useQuery({
|
||||||
|
queryKey: ["admin-workspace"],
|
||||||
|
queryFn: () => client.getAdminWorkspace(),
|
||||||
|
});
|
||||||
|
const runtimeQuery = useQuery({
|
||||||
|
queryKey: ["public-runtime-workspace"],
|
||||||
|
queryFn: () => client.getPublicWorkspace(),
|
||||||
|
});
|
||||||
|
const templateCatalog = useQuery({
|
||||||
|
queryKey: ["template-catalog"],
|
||||||
|
queryFn: () => client.listPublicTemplates(),
|
||||||
|
});
|
||||||
|
const forms = useQuery({
|
||||||
|
queryKey: ["admin-inquiry-forms"],
|
||||||
|
queryFn: () => client.listAdminInquiryForms(),
|
||||||
|
});
|
||||||
|
const templatePreview = useQuery({
|
||||||
|
queryKey: ["admin-template-preview"],
|
||||||
|
queryFn: () => client.getAdminTemplateUpgradePreview(),
|
||||||
|
enabled: Boolean(query.data?.installations?.length),
|
||||||
|
});
|
||||||
|
return { query, runtimeQuery, templateCatalog, forms, templatePreview };
|
||||||
|
}
|
||||||
|
|
||||||
|
interface TemplateMutationOptions {
|
||||||
|
client?: ReturnType<typeof createApiClient>;
|
||||||
|
runtimeConfig: Record<string, unknown> | undefined;
|
||||||
|
queryClient: {
|
||||||
|
invalidateQueries: (options: { queryKey: readonly unknown[] }) => Promise<void>;
|
||||||
|
};
|
||||||
|
}
|
||||||
|
|
||||||
|
export function useTemplateMutations({
|
||||||
|
client = createApiClient(),
|
||||||
|
runtimeConfig,
|
||||||
|
queryClient,
|
||||||
|
}: TemplateMutationOptions) {
|
||||||
|
const visualTemplate = useMutation({
|
||||||
|
mutationFn: (key: string) =>
|
||||||
|
client.updateAdminBrand({ config: { ...(runtimeConfig ?? {}), visualTemplateKey: key } }),
|
||||||
|
onSuccess: () => {
|
||||||
|
message.success("视觉模板已切换");
|
||||||
|
void queryClient.invalidateQueries({ queryKey: ["public-runtime-workspace"] });
|
||||||
|
void queryClient.invalidateQueries({ queryKey: ["admin-brand"] });
|
||||||
|
},
|
||||||
|
});
|
||||||
|
const rollbackVisual = useMutation({
|
||||||
|
mutationFn: () => client.rollbackAdminVisualTemplate(),
|
||||||
|
onSuccess: () => {
|
||||||
|
message.success("视觉模板已回滚");
|
||||||
|
void queryClient.invalidateQueries({ queryKey: ["public-runtime-workspace"] });
|
||||||
|
void queryClient.invalidateQueries({ queryKey: ["visual-template-history"] });
|
||||||
|
},
|
||||||
|
});
|
||||||
|
const install = useMutation({
|
||||||
|
mutationFn: (key: BrandTemplateKey) => client.installAdminTemplate(key),
|
||||||
|
onSuccess: () => {
|
||||||
|
message.success("模板已安装");
|
||||||
|
void queryClient.invalidateQueries({ queryKey: ["admin-workspace"] });
|
||||||
|
},
|
||||||
|
});
|
||||||
|
const upgrade = useMutation({
|
||||||
|
mutationFn: () => client.upgradeAdminTemplate(),
|
||||||
|
onSuccess: () => {
|
||||||
|
message.success("模板已升级,已有内容未被覆盖");
|
||||||
|
void queryClient.invalidateQueries({ queryKey: ["admin-workspace"] });
|
||||||
|
void queryClient.invalidateQueries({ queryKey: ["admin-template-preview"] });
|
||||||
|
},
|
||||||
|
});
|
||||||
|
const rollback = useMutation({
|
||||||
|
mutationFn: () => client.rollbackAdminTemplate(),
|
||||||
|
onSuccess: () => {
|
||||||
|
message.success("模板版本已回滚,现有内容未删除");
|
||||||
|
void queryClient.invalidateQueries({ queryKey: ["admin-workspace"] });
|
||||||
|
void queryClient.invalidateQueries({ queryKey: ["admin-template-preview"] });
|
||||||
|
},
|
||||||
|
});
|
||||||
|
return { visualTemplate, rollbackVisual, install, upgrade, rollback };
|
||||||
|
}
|
||||||
|
|
||||||
|
interface FieldMutationOptions {
|
||||||
|
client?: ReturnType<typeof createApiClient>;
|
||||||
|
form: FormInstance<Record<string, unknown>>;
|
||||||
|
queryClient: {
|
||||||
|
invalidateQueries: (options: { queryKey: readonly unknown[] }) => Promise<void>;
|
||||||
|
};
|
||||||
|
onClose: () => void;
|
||||||
|
}
|
||||||
|
|
||||||
|
export function useFieldMutations({
|
||||||
|
client = createApiClient(),
|
||||||
|
form,
|
||||||
|
queryClient,
|
||||||
|
onClose,
|
||||||
|
}: FieldMutationOptions) {
|
||||||
|
const createField = useMutation({
|
||||||
|
mutationFn: (values: Omit<BrandFieldDefinition, "id" | "source">) =>
|
||||||
|
client.createAdminField(values),
|
||||||
|
onSuccess: () => {
|
||||||
|
message.success("字段已创建");
|
||||||
|
onClose();
|
||||||
|
form.resetFields();
|
||||||
|
void queryClient.invalidateQueries({ queryKey: ["admin-workspace"] });
|
||||||
|
},
|
||||||
|
});
|
||||||
|
const removeField = useMutation({
|
||||||
|
mutationFn: (id: string) => client.deleteAdminField(id),
|
||||||
|
onSuccess: () => void queryClient.invalidateQueries({ queryKey: ["admin-workspace"] }),
|
||||||
|
});
|
||||||
|
return { createField, removeField };
|
||||||
|
}
|
||||||
|
|
||||||
|
export function useInquiryFormMutations({
|
||||||
|
client = createApiClient(),
|
||||||
|
form,
|
||||||
|
queryClient,
|
||||||
|
onClose,
|
||||||
|
}: {
|
||||||
|
client?: ReturnType<typeof createApiClient>;
|
||||||
|
form: FormInstance<Record<string, unknown>>;
|
||||||
|
queryClient: {
|
||||||
|
invalidateQueries: (options: { queryKey: readonly unknown[] }) => Promise<void>;
|
||||||
|
};
|
||||||
|
onClose: () => void;
|
||||||
|
}) {
|
||||||
|
const createForm = useMutation({
|
||||||
|
mutationFn: (values: {
|
||||||
|
name: string;
|
||||||
|
offeringId?: string;
|
||||||
|
purpose?: InquiryForm["purpose"];
|
||||||
|
targetType?: InquiryForm["targetType"];
|
||||||
|
submissionAction?: InquiryForm["submissionAction"];
|
||||||
|
fieldsSnapshot: unknown[];
|
||||||
|
}) => client.createAdminInquiryForm(values),
|
||||||
|
onSuccess: () => {
|
||||||
|
message.success("咨询表单已创建");
|
||||||
|
onClose();
|
||||||
|
form.resetFields();
|
||||||
|
void queryClient.invalidateQueries({ queryKey: ["admin-inquiry-forms"] });
|
||||||
|
},
|
||||||
|
});
|
||||||
|
const updateForm = useMutation({
|
||||||
|
mutationFn: ({ id, body }: { id: string; body: Partial<InquiryForm> }) =>
|
||||||
|
client.updateAdminInquiryForm(id, {
|
||||||
|
...(body.name === undefined ? {} : { name: body.name }),
|
||||||
|
...(body.offeringId == null ? {} : { offeringId: body.offeringId }),
|
||||||
|
...(body.fieldsSnapshot === undefined ? {} : { fieldsSnapshot: body.fieldsSnapshot }),
|
||||||
|
...(body.isEnabled === undefined ? {} : { isEnabled: body.isEnabled }),
|
||||||
|
}),
|
||||||
|
onSuccess: () => {
|
||||||
|
message.success("咨询表单已更新");
|
||||||
|
onClose();
|
||||||
|
form.resetFields();
|
||||||
|
void queryClient.invalidateQueries({ queryKey: ["admin-inquiry-forms"] });
|
||||||
|
void queryClient.invalidateQueries({ queryKey: ["public-runtime-workspace"] });
|
||||||
|
},
|
||||||
|
});
|
||||||
|
return { createForm, updateForm };
|
||||||
|
}
|
||||||
@@ -0,0 +1,18 @@
|
|||||||
|
import type { BrandTemplateKey } from "@personal-brand/api-client";
|
||||||
|
|
||||||
|
export interface IndustryTarget {
|
||||||
|
key: BrandTemplateKey;
|
||||||
|
name: string;
|
||||||
|
description: string;
|
||||||
|
capabilities: string[];
|
||||||
|
}
|
||||||
|
|
||||||
|
export function supportsIndustry(
|
||||||
|
visual: { supportedIndustryTemplates?: unknown },
|
||||||
|
industry: string,
|
||||||
|
): boolean {
|
||||||
|
return (
|
||||||
|
Array.isArray(visual.supportedIndustryTemplates) &&
|
||||||
|
visual.supportedIndustryTemplates.includes(industry)
|
||||||
|
);
|
||||||
|
}
|
||||||
Reference in new issue
Block a user