feat: 收口多品牌与模板构建基础
This commit is contained in:
1 parent
0425a1c672
commit
77224ea51f
317 files changed
+11290
-1158
No files matched your search
@@ -14,11 +14,59 @@ import {
|
||||
import { useMutation, useQuery, useQueryClient } from "@tanstack/react-query";
|
||||
import { createApiClient } from "../../lib/api.js";
|
||||
import { ApiClientError } from "@personal-brand/api-client";
|
||||
import type { BrandFieldDefinition, BrandModule, InquiryForm } from "@personal-brand/api-client";
|
||||
import type {
|
||||
BrandFieldDefinition,
|
||||
BrandModule,
|
||||
BrandTemplateKey,
|
||||
InquiryForm,
|
||||
} from "@personal-brand/api-client";
|
||||
import { useState } from "react";
|
||||
import { AdminPageHeader } from "../../components/AdminPageHeader.js";
|
||||
import { MutationError } from "../../components/QueryState.js";
|
||||
|
||||
const inquiryFieldTypes = [
|
||||
{ value: "text", label: "简短文字" },
|
||||
{ value: "textarea", label: "详细说明" },
|
||||
{ value: "phone", label: "联系电话" },
|
||||
{ value: "number", label: "数字" },
|
||||
{ value: "date", label: "日期" },
|
||||
{ value: "image", label: "图片" },
|
||||
{ value: "url", label: "链接" },
|
||||
{ value: "select", label: "单项选择" },
|
||||
{ value: "multi-select", label: "多项选择" },
|
||||
];
|
||||
|
||||
function optionsToText(options: unknown): string {
|
||||
if (!Array.isArray(options)) return "";
|
||||
return options
|
||||
.map((option) => {
|
||||
if (!option || typeof option !== "object") return "";
|
||||
const label = (option as { label?: unknown }).label;
|
||||
return typeof label === "string" ? label : "";
|
||||
})
|
||||
.filter(Boolean)
|
||||
.join("\n");
|
||||
}
|
||||
|
||||
function optionsFromText(value: unknown): Array<{ label: string; value: string }> {
|
||||
if (typeof value !== "string") return [];
|
||||
return value
|
||||
.split(/\r?\n|、|,/)
|
||||
.map((item) => item.trim())
|
||||
.filter(Boolean)
|
||||
.map((item) => ({ label: item, value: item }));
|
||||
}
|
||||
|
||||
function supportsIndustry(
|
||||
visual: { supportedIndustryTemplates?: unknown },
|
||||
industry: string,
|
||||
): boolean {
|
||||
return (
|
||||
Array.isArray(visual.supportedIndustryTemplates) &&
|
||||
visual.supportedIndustryTemplates.includes(industry)
|
||||
);
|
||||
}
|
||||
|
||||
export function WorkspacePage() {
|
||||
const client = createApiClient();
|
||||
const queryClient = useQueryClient();
|
||||
@@ -32,7 +80,7 @@ export function WorkspacePage() {
|
||||
description: string;
|
||||
capabilities: string[];
|
||||
} | null>(null);
|
||||
const [previewIndustry, setPreviewIndustry] = useState("personal");
|
||||
const [previewIndustry, setPreviewIndustry] = useState<BrandTemplateKey>("personal");
|
||||
const [previewVisual, setPreviewVisual] = useState("linjing-editorial");
|
||||
const [form] = Form.useForm<Record<string, unknown>>();
|
||||
const [inquiryForm] = Form.useForm<Record<string, unknown>>();
|
||||
@@ -53,6 +101,9 @@ export function WorkspacePage() {
|
||||
queryFn: () => client.previewPublicTemplateCombination(previewIndustry, previewVisual),
|
||||
enabled: Boolean(templateCatalog.data),
|
||||
});
|
||||
const compatibleVisuals = (templateCatalog.data?.visuals ?? []).filter((visual) =>
|
||||
supportsIndustry(visual, query.data?.brand?.templateKey ?? "personal"),
|
||||
);
|
||||
const visualTemplate = useMutation({
|
||||
mutationFn: (key: string) =>
|
||||
client.updateAdminBrand({
|
||||
@@ -247,8 +298,7 @@ export function WorkspacePage() {
|
||||
loading={visualTemplate.isPending}
|
||||
onChange={(key) => setVisualTarget(key)}
|
||||
options={[
|
||||
{ value: "linjing-editorial", label: "林境建筑画册" },
|
||||
{ value: "minimal-neutral", label: "极简留白" },
|
||||
...compatibleVisuals.map((visual) => ({ value: visual.key, label: visual.name })),
|
||||
]}
|
||||
/>
|
||||
{runtimeQuery.data?.visualTemplate && (
|
||||
@@ -285,7 +335,7 @@ export function WorkspacePage() {
|
||||
<Space direction="vertical" style={{ width: "100%" }}>
|
||||
<Typography.Text strong>可用视觉模板</Typography.Text>
|
||||
<Space wrap>
|
||||
{(templateCatalog.data?.visuals ?? []).map((visual) => (
|
||||
{compatibleVisuals.map((visual) => (
|
||||
<Card key={visual.key} size="small" style={{ width: 260 }}>
|
||||
<Typography.Text strong>{visual.name}</Typography.Text>
|
||||
<Typography.Paragraph type="secondary" ellipsis={{ rows: 2 }}>
|
||||
@@ -321,9 +371,7 @@ export function WorkspacePage() {
|
||||
<Typography.Paragraph>
|
||||
当前模板:{runtimeQuery.data?.visualTemplate?.name ?? "林境建筑画册"}
|
||||
</Typography.Paragraph>
|
||||
<Typography.Paragraph>
|
||||
目标模板:{visualTarget === "minimal-neutral" ? "极简留白" : "林境建筑画册"}
|
||||
</Typography.Paragraph>
|
||||
<Typography.Paragraph>目标模板:林境建筑画册</Typography.Paragraph>
|
||||
<Typography.Paragraph type="secondary">
|
||||
只会替换视觉主题和模块变体,不会覆盖品牌内容、业务提供项或咨询记录。
|
||||
</Typography.Paragraph>
|
||||
@@ -418,10 +466,9 @@ export function WorkspacePage() {
|
||||
<Select
|
||||
value={previewVisual}
|
||||
onChange={setPreviewVisual}
|
||||
options={(templateCatalog.data?.visuals ?? []).map((item) => ({
|
||||
value: item.key,
|
||||
label: item.name,
|
||||
}))}
|
||||
options={(templateCatalog.data?.visuals ?? [])
|
||||
.filter((item) => supportsIndustry(item, previewIndustry))
|
||||
.map((item) => ({ value: item.key, label: item.name }))}
|
||||
/>
|
||||
</Space>
|
||||
{combinationPreview.isLoading ? (
|
||||
@@ -490,9 +537,7 @@ export function WorkspacePage() {
|
||||
const value = field as Record<string, unknown>;
|
||||
return {
|
||||
...value,
|
||||
...(Array.isArray(value.options)
|
||||
? { optionsText: JSON.stringify(value.options) }
|
||||
: {}),
|
||||
optionsText: optionsToText(value.options),
|
||||
};
|
||||
}),
|
||||
});
|
||||
@@ -692,15 +737,11 @@ export function WorkspacePage() {
|
||||
: []
|
||||
).map((field) => {
|
||||
const item = field as Record<string, unknown>;
|
||||
if (typeof item.optionsText !== "string") return item;
|
||||
try {
|
||||
const options = JSON.parse(item.optionsText) as unknown;
|
||||
const { optionsText, ...rest } = item;
|
||||
void optionsText;
|
||||
return { ...rest, options };
|
||||
} catch {
|
||||
return item;
|
||||
}
|
||||
const { optionsText, ...rest } = item;
|
||||
const type = typeof rest.type === "string" ? rest.type : "text";
|
||||
return ["select", "radio", "multi-select"].includes(type)
|
||||
? { ...rest, options: optionsFromText(optionsText) }
|
||||
: rest;
|
||||
}),
|
||||
};
|
||||
if (editingForm) updateForm.mutate({ id: editingForm.id, body });
|
||||
@@ -738,47 +779,70 @@ export function WorkspacePage() {
|
||||
{(fields, { add, remove }) => (
|
||||
<>
|
||||
<Form.Item label="表单字段">
|
||||
<Button onClick={() => add({ type: "text", required: false })}>新增字段</Button>
|
||||
<Button
|
||||
onClick={() =>
|
||||
add({
|
||||
key: `field_${Date.now()}`,
|
||||
type: "text",
|
||||
required: false,
|
||||
})
|
||||
}
|
||||
>
|
||||
新增字段
|
||||
</Button>
|
||||
</Form.Item>
|
||||
{fields.map(({ key, name, ...restField }) => (
|
||||
<Space key={key} align="start" style={{ display: "flex", marginBottom: 8 }}>
|
||||
<Form.Item {...restField} name={[name, "key"]} rules={[{ required: true }]}>
|
||||
<Input placeholder="Key" />
|
||||
</Form.Item>
|
||||
<Form.Item {...restField} name={[name, "label"]} rules={[{ required: true }]}>
|
||||
<Input placeholder="字段名称" />
|
||||
</Form.Item>
|
||||
<Form.Item {...restField} name={[name, "type"]}>
|
||||
<Select
|
||||
style={{ width: 120 }}
|
||||
options={[
|
||||
"text",
|
||||
"textarea",
|
||||
"number",
|
||||
"date",
|
||||
"select",
|
||||
"multi-select",
|
||||
"image",
|
||||
"url",
|
||||
"phone",
|
||||
].map((value) => ({ value, label: value }))}
|
||||
/>
|
||||
<Card key={key} size="small" style={{ marginBottom: 12 }}>
|
||||
<Form.Item {...restField} name={[name, "key"]} hidden>
|
||||
<Input />
|
||||
</Form.Item>
|
||||
<Space align="start" style={{ display: "flex" }}>
|
||||
<Form.Item
|
||||
{...restField}
|
||||
name={[name, "label"]}
|
||||
label="要询问客户什么"
|
||||
rules={[{ required: true, message: "请填写字段名称" }]}
|
||||
style={{ flex: 1 }}
|
||||
>
|
||||
<Input placeholder="例如:联系电话、预计预算、项目地址" />
|
||||
</Form.Item>
|
||||
<Form.Item {...restField} name={[name, "type"]} label="回答方式">
|
||||
<Select style={{ width: 130 }} options={inquiryFieldTypes} />
|
||||
</Form.Item>
|
||||
<Form.Item {...restField} name={[name, "required"]} label="必填">
|
||||
<Select
|
||||
style={{ width: 90 }}
|
||||
options={[
|
||||
{ value: true, label: "必填" },
|
||||
{ value: false, label: "选填" },
|
||||
]}
|
||||
/>
|
||||
</Form.Item>
|
||||
<Button danger onClick={() => remove(name)} style={{ marginTop: 30 }}>
|
||||
删除
|
||||
</Button>
|
||||
</Space>
|
||||
<Form.Item noStyle shouldUpdate>
|
||||
{({ getFieldValue }) =>
|
||||
["select", "radio"].includes(
|
||||
["select", "radio", "multi-select"].includes(
|
||||
String(getFieldValue(["fieldsSnapshot", name, "type"])),
|
||||
) ? (
|
||||
<Form.Item {...restField} name={[name, "optionsText"]}>
|
||||
<Input placeholder="选项 JSON" />
|
||||
<Form.Item
|
||||
{...restField}
|
||||
name={[name, "optionsText"]}
|
||||
label="可选内容"
|
||||
help="每行填写一个选项,也可以用顿号分隔。"
|
||||
rules={[{ required: true, message: "请至少填写一个选项" }]}
|
||||
>
|
||||
<Input.TextArea
|
||||
rows={2}
|
||||
placeholder="例如:预算内、需要沟通、暂不确定"
|
||||
/>
|
||||
</Form.Item>
|
||||
) : null
|
||||
}
|
||||
</Form.Item>
|
||||
<Button danger onClick={() => remove(name)}>
|
||||
删除
|
||||
</Button>
|
||||
</Space>
|
||||
</Card>
|
||||
))}
|
||||
</>
|
||||
)}
|
||||
|
||||
Reference in new issue
Block a user