From 6f18c6731596949678a73908a6504edfb3e38177 Mon Sep 17 00:00:00 2001 From: que01 Date: Thu, 15 Oct 2026 14:42:20 +0800 Subject: [PATCH] =?UTF-8?q?refactor:=20=E6=8B=86=E5=88=86=E5=93=81?= =?UTF-8?q?=E7=89=8C=E5=B1=95=E7=A4=BA=E7=BC=96=E8=BE=91=E5=99=A8=E8=81=8C?= =?UTF-8?q?=E8=B4=A3?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../src/features/brand/PresentationEditor.tsx | 333 ++---------------- .../features/brand/PresentationFormPanel.tsx | 134 +++++++ .../brand/presentation-editor-helpers.ts | 93 +++++ .../presentation-editor-record-preview.ts | 3 +- .../src/features/brand/use-draft-workspace.ts | 128 +++++++ 5 files changed, 376 insertions(+), 315 deletions(-) create mode 100644 apps/admin/src/features/brand/PresentationFormPanel.tsx create mode 100644 apps/admin/src/features/brand/presentation-editor-helpers.ts create mode 100644 apps/admin/src/features/brand/use-draft-workspace.ts diff --git a/apps/admin/src/features/brand/PresentationEditor.tsx b/apps/admin/src/features/brand/PresentationEditor.tsx index f4209ef..df8198e 100644 --- a/apps/admin/src/features/brand/PresentationEditor.tsx +++ b/apps/admin/src/features/brand/PresentationEditor.tsx @@ -1,30 +1,13 @@ -import { - Alert, - Button, - Divider, - Col, - Form, - Grid, - Row, - Select, - Segmented, - Space, - Typography, -} from "antd"; +import { Alert, Button, Grid, Select, Segmented, Space, Typography } from "antd"; import { useQueryClient } from "@tanstack/react-query"; import { useCallback, useEffect, useMemo, useState } from "react"; import { useNavigate } from "react-router-dom"; import type { AdminTemplateContent, RuntimeWorkspace, - TemplateAdminField, TemplateAdminSection, } from "@personal-brand/api-client"; import type { BrandProfile } from "@personal-brand/api-client"; -import { projectBrandAliases, projectTrustPresentation } from "@personal-brand/api-client"; -import { TemplateListField } from "../../components/TemplateListField.js"; -import { TemplateFieldRenderer } from "../../components/TemplateFieldRenderer.js"; -import { MutationError } from "../../components/QueryState.js"; import { StatusTag } from "../../components/StatusTag.js"; import { RecordContentEditor } from "../../components/RecordContentEditor.js"; import { TemplatePreview } from "./TemplatePreview.js"; @@ -37,7 +20,6 @@ import { } from "./preview-selection.js"; import { filterSectionsForPreviewTarget, - presentationFieldName, type PresentationValues, } from "../../lib/presentation/presentation-bindings.js"; import "./PresentationEditor.css"; @@ -50,6 +32,10 @@ import { useBrandPreviewDraft, } from "./presentation-editor-state.js"; import { useRecordPreview } from "./presentation-editor-record-preview.js"; +import type { RecordDraft } from "./presentation-editor-record-preview.js"; +import { pageTitle, previewTargetSource, trustSection } from "./presentation-editor-helpers.js"; +import { useDraftWorkspace } from "./use-draft-workspace.js"; +import { PresentationFormPanel } from "./PresentationFormPanel.js"; interface PresentationEditorProps { templateContent: AdminTemplateContent | null | undefined; @@ -61,73 +47,6 @@ interface PresentationEditorProps { initialSelection?: PreviewSelection; } -function trustSection( - declaration: NonNullable[number], - page: string, -): TemplateAdminSection { - const key = `trust:${declaration.type}`; - return { - key, - title: declaration.label, - description: declaration.help ?? "编辑当前展示内容。", - page, - target: "trust", - previewTargets: [ - { key, label: declaration.label }, - ...(declaration.previewTargets ?? []).filter( - (target) => !target.page || target.page === page, - ), - ], - fields: [ - { - key: "title", - binding: "trust.title", - label: declaration.adminFields.title, - input: "text" as const, - required: true, - }, - ...(["summary", "content"] as const) - .filter((field) => !declaration.hideStandardFields?.includes(field)) - .map((field) => ({ - key: field, - binding: `trust.${field}`, - label: declaration.adminFields[field], - input: "textarea" as const, - previewTarget: `trust:${declaration.type}`, - ...(field === "summary" && declaration.adminFields.summaryHelp - ? { help: declaration.adminFields.summaryHelp } - : {}), - ...(field === "content" && declaration.required ? { required: true } : {}), - })), - ...(declaration.configFields ?? []) - .filter((field) => { - const target = declaration.previewTargets?.find( - (target) => target.key === field.previewTarget, - ); - return !target?.page || target.page === page; - }) - .map((field) => ({ - ...field, - previewTarget: field.previewTarget ?? `trust:${declaration.type}`, - })), - ].map((field) => ({ - ...field, - previewTarget: (field as TemplateAdminField).previewTarget ?? `trust:${declaration.type}`, - })), - }; -} - -function previewTargetSource( - section: TemplateAdminSection, - targetKey: string, -): "brand" | "offering" | "trust" | "inquiry" { - const fields = section.fields.filter((field) => field.previewTarget === targetKey); - if (fields.some((field) => field.binding.startsWith("offering."))) return "offering"; - if (fields.some((field) => field.binding.startsWith("trust."))) return "trust"; - if (fields.some((field) => field.binding.startsWith("brand."))) return "brand"; - return section.target; -} - export function PresentationEditor({ templateContent, previewWorkspace, @@ -144,11 +63,7 @@ export function PresentationEditor({ const [selection, setSelection] = useState(initialSelection); const [hasInitializedSelection, setHasInitializedSelection] = useState(Boolean(initialSelection)); const [detailOfferingId, setDetailOfferingId] = useState(); - const [recordDraft, setRecordDraft] = useState<{ - source: "offering" | "trust"; - id: string; - values: PresentationValues; - }>(); + const [recordDraft, setRecordDraft] = useState(); const [inquiryDraft, setInquiryDraft] = useState(); const [brandDirty, setBrandDirty] = useState(false); @@ -367,113 +282,17 @@ export function PresentationEditor({ }); const recordPreview = useRecordPreview({ recordDraft, selectedRecord, recordSection }); useEffect(() => resetPreview(), [config, initialValues, resetPreview]); - const previewConfig = previewBrand.config; - const draftWorkspace = useMemo(() => { - if (!previewWorkspace) return undefined; - const next: RuntimeWorkspace = { - ...previewWorkspace, - templateData: { - ...previewWorkspace.templateData, - ...(previewConfig.templateData && - typeof previewConfig.templateData === "object" && - !Array.isArray(previewConfig.templateData) - ? (previewConfig.templateData as Record) - : {}), - }, - inquiryForms: previewWorkspace.inquiryForms.map((item) => - item.key === inquiryRuntime?.key && inquiryDraft ? { ...item, fields: inquiryDraft } : item, - ), - brand: projectBrandAliases( - { - ...previewWorkspace.brand, - ...(previewBrand as unknown as Record), - config: previewConfig, - }, - previewConfig, - ), - }; - if (!recordDraft || !selectedRecord || !recordSection || recordDraft.id !== selectedRecord.id) - return next; - const config = recordPreview?.config; - const standard = recordPreview?.standard; - if (!config || !standard) return next; - if (recordDraft.source === "trust") { - next.trustItems = next.trustItems.map((item) => - item.id === recordDraft.id - ? { - ...item, - title: typeof standard.title === "string" ? standard.title : item.title, - summary: typeof standard.summary === "string" ? standard.summary : item.summary, - content: typeof standard.content === "string" ? standard.content : item.content, - config, - } - : item, - ); - next.trustPresentation = projectTrustPresentation( - next.trustItems, - templateContent?.trustContentTypes ?? [], - ); - } else { - next.offerings = next.offerings.map((item) => - item.id === recordDraft.id - ? { - ...item, - name: - (recordDraft.values[presentationFieldName(recordSection.key, "name")] as string) || - item.name, - summary: - (recordDraft.values[ - presentationFieldName(recordSection.key, "summary") - ] as string) || item.summary, - priceText: - (recordDraft.values[ - presentationFieldName(recordSection.key, "priceText") - ] as string) || null, - coverUrl: - (recordDraft.values[ - presentationFieldName(recordSection.key, "coverUrl") - ] as string) || null, - presentation: { - ...item.presentation, - ...config, - ...(config.presentation && typeof config.presentation === "object" - ? (config.presentation as Record) - : {}), - ...(typeof config.area === "string" ? { area: config.area } : {}), - ...(typeof config.series === "string" ? { series: config.series } : {}), - ...(typeof config.spaceType === "string" ? { spaceType: config.spaceType } : {}), - heroImage: - typeof config.detailHero === "string" - ? config.detailHero - : typeof standard.coverUrl === "string" - ? standard.coverUrl - : null, - details: { - ...item.presentation.details, - ...(config.details && - typeof config.details === "object" && - "background" in config.details - ? (config.details as RuntimeWorkspace["offerings"][number]["presentation"]["details"]) - : {}), - }, - }, - } - : item, - ); - } - return next; - }, [ + const draftWorkspace = useDraftWorkspace({ previewWorkspace, previewBrand, - previewConfig, + inquiryRuntime, + inquiryDraft, recordDraft, selectedRecord, recordSection, recordPreview, templateContent, - inquiryDraft, - inquiryRuntime, - ]); + }); const selectedDetailOfferingId = detailOfferingId && draftWorkspace?.offerings.some((item) => item.id === detailOfferingId) ? detailOfferingId @@ -790,110 +609,19 @@ export function PresentationEditor({

) : ( -
save.mutate(values), + }} onValuesChange={handleValuesChange} - onFinish={() => save.mutate(form.getFieldsValue(true) as PresentationValues)} - className="presentation-editor__form" - > -
- {editableSections.map((section, index) => ( -
- {selectedTarget && index > 0 ? : null} -

- {selectedTarget - ? fieldTargetTitle(section, selectedTarget.label) - : section.title} -

- {!selectedTarget ? ( -

- {section.description} -

- ) : null} - - {section.fields.map((field) => ( - - {field.input === "list" ? ( - - ) : ( - - - - )} - - ))} - -
- ))} - {relevantPageLinks.length ? ( - - {relevantPageLinks.map((item) => ( - - ))} - - } - /> - ) : null} - {!editableSections.some((section) => section.fields.length) && - !relevantPageLinks.length ? ( -

这个页面没有单独的品牌文案配置。

- ) : null} - {selectedTarget && - !editableSections.some((section) => section.fields.length) && - relevantPageLinks.length ? ( -

- 此区域内容由对应的业务管理页面维护。 -

- ) : null} -
-
- - -
-
+ /> )} @@ -902,24 +630,3 @@ export function PresentationEditor({ ); } - -function pageTitle(page: string): string { - return ( - ( - { - home: "首页", - offerings: "业务展示", - service: "服务说明", - detail: "详情页", - inquiry: "咨询页", - success: "提交成功页", - records: "记录页", - profile: "主理人页", - } as Record - )[page] ?? page - ); -} - -function fieldTargetTitle(section: { title: string }, targetLabel: string): string { - return section.title === targetLabel ? targetLabel : `${targetLabel} · ${section.title}`; -} diff --git a/apps/admin/src/features/brand/PresentationFormPanel.tsx b/apps/admin/src/features/brand/PresentationFormPanel.tsx new file mode 100644 index 0000000..f61e4a8 --- /dev/null +++ b/apps/admin/src/features/brand/PresentationFormPanel.tsx @@ -0,0 +1,134 @@ +import { Alert, Button, Col, Divider, Form, Row, Space } from "antd"; +import type { FormInstance } from "antd"; +import { useNavigate } from "react-router-dom"; +import type { TemplateAdminSection } from "@personal-brand/api-client"; +import { TemplateFieldRenderer } from "../../components/TemplateFieldRenderer.js"; +import { TemplateListField } from "../../components/TemplateListField.js"; +import { MutationError } from "../../components/QueryState.js"; +import { + presentationFieldName, + type PresentationValues, +} from "../../lib/presentation/presentation-bindings.js"; +import { fieldTargetTitle } from "./presentation-editor-helpers.js"; +import type { PreviewTarget } from "./preview-selection.js"; + +interface PresentationFormPanelProps { + form: FormInstance; + initialValues: PresentationValues; + editableSections: TemplateAdminSection[]; + relevantPageLinks: { target: string; label: string | undefined }[]; + selectedTarget: PreviewTarget | undefined; + save: { + error: unknown; + isPending: boolean; + mutate: (values: PresentationValues) => void; + }; + onValuesChange: (changed: PresentationValues, allValues: PresentationValues) => void; +} + +export function PresentationFormPanel({ + form, + initialValues, + editableSections, + relevantPageLinks, + selectedTarget, + save, + onValuesChange, +}: PresentationFormPanelProps) { + const navigate = useNavigate(); + const hasEditableFields = editableSections.some((section) => section.fields.length); + + return ( +
save.mutate(form.getFieldsValue(true) as PresentationValues)} + className="presentation-editor__form" + > +
+ {editableSections.map((section, index) => ( +
+ {selectedTarget && index > 0 ? : null} +

+ {selectedTarget ? fieldTargetTitle(section, selectedTarget.label) : section.title} +

+ {!selectedTarget ? ( +

{section.description}

+ ) : null} + + {section.fields.map((field) => ( + + {field.input === "list" ? ( + + ) : ( + + + + )} + + ))} + +
+ ))} + {relevantPageLinks.length ? ( + + {relevantPageLinks.map((item) => ( + + ))} + + } + /> + ) : null} + {!hasEditableFields && !relevantPageLinks.length ? ( +

这个页面没有单独的品牌文案配置。

+ ) : null} + {selectedTarget && !hasEditableFields && relevantPageLinks.length ? ( +

此区域内容由对应的业务管理页面维护。

+ ) : null} +
+
+ + +
+
+ ); +} diff --git a/apps/admin/src/features/brand/presentation-editor-helpers.ts b/apps/admin/src/features/brand/presentation-editor-helpers.ts new file mode 100644 index 0000000..6efac4e --- /dev/null +++ b/apps/admin/src/features/brand/presentation-editor-helpers.ts @@ -0,0 +1,93 @@ +import type { + AdminTemplateContent, + TemplateAdminField, + TemplateAdminSection, +} from "@personal-brand/api-client"; + +export function trustSection( + declaration: NonNullable[number], + page: string, +): TemplateAdminSection { + const key = `trust:${declaration.type}`; + return { + key, + title: declaration.label, + description: declaration.help ?? "编辑当前展示内容。", + page, + target: "trust", + previewTargets: [ + { key, label: declaration.label }, + ...(declaration.previewTargets ?? []).filter( + (target) => !target.page || target.page === page, + ), + ], + fields: [ + { + key: "title", + binding: "trust.title", + label: declaration.adminFields.title, + input: "text" as const, + required: true, + }, + ...(["summary", "content"] as const) + .filter((field) => !declaration.hideStandardFields?.includes(field)) + .map((field) => ({ + key: field, + binding: `trust.${field}`, + label: declaration.adminFields[field], + input: "textarea" as const, + previewTarget: `trust:${declaration.type}`, + ...(field === "summary" && declaration.adminFields.summaryHelp + ? { help: declaration.adminFields.summaryHelp } + : {}), + ...(field === "content" && declaration.required ? { required: true } : {}), + })), + ...(declaration.configFields ?? []) + .filter((field) => { + const target = declaration.previewTargets?.find( + (target) => target.key === field.previewTarget, + ); + return !target?.page || target.page === page; + }) + .map((field) => ({ + ...field, + previewTarget: field.previewTarget ?? `trust:${declaration.type}`, + })), + ].map((field) => ({ + ...field, + previewTarget: (field as TemplateAdminField).previewTarget ?? `trust:${declaration.type}`, + })), + }; +} + +export function previewTargetSource( + section: TemplateAdminSection, + targetKey: string, +): "brand" | "offering" | "trust" | "inquiry" { + const fields = section.fields.filter((field) => field.previewTarget === targetKey); + if (fields.some((field) => field.binding.startsWith("offering."))) return "offering"; + if (fields.some((field) => field.binding.startsWith("trust."))) return "trust"; + if (fields.some((field) => field.binding.startsWith("brand."))) return "brand"; + return section.target; +} + +export function pageTitle(page: string): string { + return ( + ( + { + home: "首页", + offerings: "业务展示", + service: "服务说明", + detail: "详情页", + inquiry: "咨询页", + success: "提交成功页", + records: "记录页", + profile: "主理人页", + } as Record + )[page] ?? page + ); +} + +export function fieldTargetTitle(section: { title: string }, targetLabel: string): string { + return section.title === targetLabel ? targetLabel : `${targetLabel} · ${section.title}`; +} diff --git a/apps/admin/src/features/brand/presentation-editor-record-preview.ts b/apps/admin/src/features/brand/presentation-editor-record-preview.ts index cbef822..7328cc5 100644 --- a/apps/admin/src/features/brand/presentation-editor-record-preview.ts +++ b/apps/admin/src/features/brand/presentation-editor-record-preview.ts @@ -1,6 +1,5 @@ import { useMemo } from "react"; -import type { Offering, TrustContent } from "@personal-brand/api-client"; -import type { TemplateAdminSection } from "@personal-brand/api-client"; +import type { Offering, TemplateAdminSection, TrustContent } from "@personal-brand/api-client"; import { applyPresentationValues, applyStandardEntityValues, diff --git a/apps/admin/src/features/brand/use-draft-workspace.ts b/apps/admin/src/features/brand/use-draft-workspace.ts new file mode 100644 index 0000000..2c94020 --- /dev/null +++ b/apps/admin/src/features/brand/use-draft-workspace.ts @@ -0,0 +1,128 @@ +import { useMemo } from "react"; +import type { + AdminTemplateContent, + BrandProfile, + Offering, + RuntimeWorkspace, + TemplateAdminSection, + TrustContent, +} from "@personal-brand/api-client"; +import { projectBrandAliases, projectTrustPresentation } from "@personal-brand/api-client"; +import { + presentationFieldName, + type PresentationValues, +} from "../../lib/presentation/presentation-bindings.js"; +import type { RecordDraft } from "./presentation-editor-record-preview.js"; + +interface DraftWorkspaceInput { + previewWorkspace: RuntimeWorkspace | undefined; + previewBrand: BrandProfile; + inquiryRuntime: RuntimeWorkspace["inquiryForms"][number] | undefined; + inquiryDraft: RuntimeWorkspace["inquiryForms"][number]["fields"] | undefined; + recordDraft: RecordDraft | undefined; + selectedRecord: Offering | TrustContent | undefined; + recordSection: TemplateAdminSection | undefined; + recordPreview: { config: Record; standard: Record } | undefined; + templateContent: AdminTemplateContent | null | undefined; +} + +export function useDraftWorkspace(input: DraftWorkspaceInput): RuntimeWorkspace | undefined { + const { previewWorkspace, previewBrand, inquiryRuntime, inquiryDraft } = input; + return useMemo(() => { + if (!previewWorkspace) return undefined; + const previewConfig = previewBrand.config ?? {}; + const next: RuntimeWorkspace = { + ...previewWorkspace, + templateData: { + ...previewWorkspace.templateData, + ...(previewConfig.templateData && + typeof previewConfig.templateData === "object" && + !Array.isArray(previewConfig.templateData) + ? (previewConfig.templateData as Record) + : {}), + }, + inquiryForms: previewWorkspace.inquiryForms.map((item) => + item.key === inquiryRuntime?.key && inquiryDraft ? { ...item, fields: inquiryDraft } : item, + ), + brand: projectBrandAliases( + { + ...previewWorkspace.brand, + ...(previewBrand as unknown as Record), + config: previewConfig, + }, + previewConfig, + ), + }; + const { recordDraft, selectedRecord, recordSection, recordPreview } = input; + if (!recordDraft || !selectedRecord || !recordSection || recordDraft.id !== selectedRecord.id) + return next; + const config = recordPreview?.config; + const standard = recordPreview?.standard; + if (!config || !standard) return next; + if (recordDraft.source === "trust") { + next.trustItems = next.trustItems.map((item) => + item.id === recordDraft.id + ? { + ...item, + title: typeof standard.title === "string" ? standard.title : item.title, + summary: typeof standard.summary === "string" ? standard.summary : item.summary, + content: typeof standard.content === "string" ? standard.content : item.content, + config, + } + : item, + ); + next.trustPresentation = projectTrustPresentation( + next.trustItems, + input.templateContent?.trustContentTypes ?? [], + ); + } else { + next.offerings = next.offerings.map((item) => + item.id === recordDraft.id + ? { + ...item, + name: + (recordDraft.values[presentationFieldName(recordSection.key, "name")] as string) || + item.name, + summary: + (recordDraft.values[ + presentationFieldName(recordSection.key, "summary") + ] as string) || item.summary, + priceText: + (recordDraft.values[ + presentationFieldName(recordSection.key, "priceText") + ] as string) || null, + coverUrl: + (recordDraft.values[ + presentationFieldName(recordSection.key, "coverUrl") + ] as string) || null, + presentation: { + ...item.presentation, + ...config, + ...(config.presentation && typeof config.presentation === "object" + ? (config.presentation as Record) + : {}), + ...(typeof config.area === "string" ? { area: config.area } : {}), + ...(typeof config.series === "string" ? { series: config.series } : {}), + ...(typeof config.spaceType === "string" ? { spaceType: config.spaceType } : {}), + heroImage: + typeof config.detailHero === "string" + ? config.detailHero + : typeof standard.coverUrl === "string" + ? standard.coverUrl + : null, + details: { + ...item.presentation.details, + ...(config.details && + typeof config.details === "object" && + "background" in config.details + ? (config.details as RuntimeWorkspace["offerings"][number]["presentation"]["details"]) + : {}), + }, + }, + } + : item, + ); + } + return next; + }, [input, previewWorkspace, previewBrand, inquiryRuntime, inquiryDraft]); +}