diff --git a/.codex-tasks/template-audit-execution/raw/completion-evidence.json b/.codex-tasks/template-audit-execution/raw/completion-evidence.json index 4aac64f..8264a22 100644 --- a/.codex-tasks/template-audit-execution/raw/completion-evidence.json +++ b/.codex-tasks/template-audit-execution/raw/completion-evidence.json @@ -1,7 +1,7 @@ { "schemaVersion": 1, "scope": "模板完成证据登记,保留命令、日志与产物哈希", - "generatedAt": "2026-10-21T08:37:34.818Z", + "generatedAt": "2026-10-23T06:35:59.337Z", "templates": { "acheng-campus-food": { "real-manifest-interaction": { @@ -256,5 +256,5 @@ } } }, - "sourceTreeHash": "ba35ee9629ec5aa84b9984139a4dbabc60fb576b1178f936eb8161eb905184e7" + "sourceTreeHash": "f39e31a1959e24c07bf60776a0d3c5f28e5745962ca004301ff304daa1430041" } diff --git a/.env.example b/.env.example index 3b00f0c..8f06002 100644 --- a/.env.example +++ b/.env.example @@ -9,6 +9,8 @@ API_PREFIX= CORS_ORIGINS=http://localhost:3000 ADMIN_PORT=3000 SESSION_SECRET=replace-with-a-long-random-value +# 单实例使用 memory;多实例部署改用 postgres 并先执行数据库迁移 +CLIENT_ERROR_THROTTLE_STORE=memory DEFAULT_BRAND_ID=00000000-0000-0000-0000-000000000001 TEMPLATE_ARTIFACT_ROOT=/var/lib/personal-brand/template-artifacts SEED_ADMIN_PASSWORD=change-me-now-123 diff --git a/.env.production.example b/.env.production.example index 6ece959..be486fe 100644 --- a/.env.production.example +++ b/.env.production.example @@ -6,6 +6,8 @@ POSTGRES_PORT=5432 DATABASE_URL=postgresql://personal_brand:replace-with-a-long-database-password@127.0.0.1:5432/personal_brand?schema=public SESSION_SECRET=replace-with-at-least-32-random-characters +# 单实例使用 memory;多实例部署改用 postgres 并先执行数据库迁移 +CLIENT_ERROR_THROTTLE_STORE=memory DEFAULT_BRAND_ID=00000000-0000-0000-0000-000000000001 SEED_ADMIN_PASSWORD=replace-with-a-one-time-initial-password SEED_MODE=skip diff --git a/.gitignore b/.gitignore index affc127..9e50abe 100644 --- a/.gitignore +++ b/.gitignore @@ -24,6 +24,9 @@ media/ **/.swc/ .codex-tasks/ +!.codex-tasks/template-audit-execution/ +!.codex-tasks/template-audit-execution/raw/ +!.codex-tasks/template-audit-execution/raw/completion-evidence.json docker-compose.override.yml .env.production diff --git a/apps/admin/src/features/brand/InquiryPreviewEditor.tsx b/apps/admin/src/features/brand/InquiryPreviewEditor.tsx index 2fccacf..beb25f8 100644 --- a/apps/admin/src/features/brand/InquiryPreviewEditor.tsx +++ b/apps/admin/src/features/brand/InquiryPreviewEditor.tsx @@ -28,8 +28,9 @@ export function InquiryPreviewEditor({ const record = query.data?.find((item) => item.id === runtime.key); const field = runtime.fields.find((item) => item.key === fieldKey); useEffect(() => { - if (field) form.setFieldsValue({ ...field }); - }, [fieldKey, runtime.key, form]); + if (!record || !field) return; + form.setFieldsValue({ ...field }); + }, [field, fieldKey, form, record, runtime.key]); const update = (values: Record) => runtime.fields.map((item) => (item.key === fieldKey ? { ...item, ...values } : item)); const save = useMutation({ diff --git a/apps/admin/src/features/brand/PresentationEditor.tsx b/apps/admin/src/features/brand/PresentationEditor.tsx index c9da99d..ffdea23 100644 --- a/apps/admin/src/features/brand/PresentationEditor.tsx +++ b/apps/admin/src/features/brand/PresentationEditor.tsx @@ -1,34 +1,15 @@ -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 } from "@personal-brand/api-client"; -import type { BrandProfile } from "@personal-brand/api-client"; +import { Alert, Grid, Segmented, Select, Space } from "antd"; +import type { + AdminTemplateContent, + BrandProfile, + RuntimeWorkspace, +} from "@personal-brand/api-client"; import { StatusTag } from "../../components/StatusTag.js"; -import { RecordContentEditor } from "../../components/RecordContentEditor.js"; -import { TemplatePreview } from "./TemplatePreview.js"; -import { InquiryPreviewEditor } from "./InquiryPreviewEditor.js"; -import { - resolvePreviewSelection, - previewTrustRecordId, - type PreviewSelection, - type PreviewTarget, -} from "./preview-selection.js"; -import { filterSectionsForPreviewTarget } from "../../lib/presentation/presentation-bindings.js"; import "./PresentationEditor.css"; -import { useEditorDraft } from "./use-editor-draft.js"; -import { - usePresentationRecordQueries, - useBrandPresentationSections, - useBrandSaveMutation, - useBrandPresentationForm, - 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"; +import type { PreviewSelection } from "./preview-selection.js"; +import { usePresentationEditorController } from "./presentation-editor-controller.js"; +import { PresentationEditorInspector } from "./PresentationEditorInspector.js"; +import { PresentationEditorPreview } from "./PresentationEditorPreview.js"; interface PresentationEditorProps { templateContent: AdminTemplateContent | null | undefined; @@ -49,254 +30,14 @@ export function PresentationEditor({ error, initialSelection, }: PresentationEditorProps) { - const queryClient = useQueryClient(); - const navigate = useNavigate(); const breakpoints = Grid.useBreakpoint(); - const [activePage, setActivePage] = useState(); - const [selection, setSelection] = useState(initialSelection); - const [hasInitializedSelection, setHasInitializedSelection] = useState(Boolean(initialSelection)); - const [detailOfferingId, setDetailOfferingId] = useState(); - const [recordDraft, setRecordDraft] = useState(); - const [inquiryDraft, setInquiryDraft] = - useState(); - const [brandDirty, setBrandDirty] = useState(false); - const guardDraft = useEditorDraft(brandDirty || !!recordDraft || !!inquiryDraft); - const inquiryRuntime = previewWorkspace?.inquiryForms.find((item) => item.target === "brand"); - const sections = useBrandPresentationSections(templateContent); - const pageOptions = useMemo(() => { - const pages = new Map(); - for (const item of templateContent?.navigation ?? []) pages.set(item.page, item.label); - for (const section of templateContent?.admin.sections ?? []) { - const navigation = templateContent?.navigation.find((item) => item.page === section.page); - if (!pages.has(section.page)) - pages.set(section.page, navigation?.label ?? pageTitle(section.page)); - } - return [...pages].map(([value, label]) => ({ value, label })); - }, [templateContent]); - const selectedPage = pageOptions.some((item) => item.value === activePage) - ? activePage - : pageOptions[0]?.value; - const visibleSections = sections.filter((section) => section.page === selectedPage); - const recordSections = useMemo( - () => - (templateContent?.trustContentTypes ?? []) - .filter((item) => item.pages?.includes(selectedPage ?? "")) - .map((item) => trustSection(item, selectedPage ?? "home")), - [templateContent, selectedPage], - ); - const visibleTargets = useMemo( - () => - [...(templateContent?.admin.sections ?? []), ...recordSections] - .filter( - (section) => - section.page === selectedPage && - (selectedPage !== "detail" || section.target === "brand"), - ) - .flatMap((section) => - (section.previewTargets ?? []).map((target) => ({ - ...target, - page: section.page, - source: previewTargetSource(section, target.key), - ...(section.key.startsWith("trust:") ? { trustType: section.key.slice(6) } : {}), - })), - ) - .concat( - selectedPage === "inquiry" && inquiryRuntime - ? inquiryRuntime.fields.map((field) => ({ - key: `inquiry-field:${field.key}`, - label: field.label, - page: "inquiry", - source: "inquiry" as const, - })) - : [], - ), - [templateContent, selectedPage, recordSections, inquiryRuntime], - ); - const activeTarget = selection?.targetKey; - const selectedTarget = visibleTargets.find((target) => target.key === activeTarget); - const selectedSection = [...(templateContent?.admin.sections ?? []), ...recordSections].find( - (section) => - section.page === selectedPage && - section.previewTargets?.some((target) => target.key === selectedTarget?.key), - ); - const selectedRecordSource = - selectedTarget?.source === "offering" || selectedTarget?.source === "trust" - ? selectedTarget.source - : undefined; - const { offeringsQuery, trustQuery, selectedRecord } = usePresentationRecordQueries( - selectedRecordSource, - selection?.recordId, - ); - const editableSections = filterSectionsForPreviewTarget( - visibleSections.filter((section) => - section.fields.some((field) => field.binding.startsWith("brand.")), - ), - selectedTarget?.key, - ); - const recordSection = useMemo( - () => - selectedSection && selection?.targetKey - ? { - ...selectedSection, - fields: selectedSection.fields.filter( - (field) => field.previewTarget === selection.targetKey, - ), - } - : undefined, - [selectedSection, selection?.targetKey], - ); - const handleTargetSelected = useCallback( - (next?: PreviewSelection) => { - const select = () => - setSelection( - next - ? resolvePreviewSelection( - next.targetKey, - next.recordId, - visibleTargets, - selectedPage ?? "home", - ) - : undefined, - ); - if (recordDraft || inquiryDraft) - guardDraft(() => { - setInquiryDraft(undefined); - setRecordDraft(undefined); - select(); - }); - else select(); - }, - [visibleTargets, selectedPage, recordDraft, inquiryDraft, guardDraft], - ); - const relatedTargets = [ - ...(templateContent?.admin.sections ?? []) - .filter((section) => section.page === selectedPage && section.target !== "brand") - .map((section) => section.target), - ...((templateContent?.trustContentTypes ?? []).some((item) => - item.pages?.includes(selectedPage ?? ""), - ) - ? ["trust"] - : []), - ]; - const pageLinks = [...new Set(relatedTargets)].flatMap((target) => - target === "trust" - ? (templateContent?.trustContentTypes ?? []) - .filter((item) => item.pages?.includes(selectedPage ?? "")) - .map((item) => ({ - target: `trust:${item.type}`, - label: `编辑${item.label}`, - })) - : [ - { - target, - label: ( - { offering: "管理此页展示的业务内容", inquiry: "管理此页的咨询表单" } as Record< - string, - string - > - )[target], - }, - ], - ); - const relevantPageLinks = selectedSection - ? pageLinks.filter((item) => { - if (item.target.startsWith("trust:")) return selectedSection.target === "trust"; - return item.target === selectedSection.target; - }) - : pageLinks; - const formState = useBrandPresentationForm({ brand, config, sections }); - const { form, initialValues } = formState; - const save = useBrandSaveMutation({ - ...formState, - brand, - config, - sections, - queryClient, - onSaved: () => setBrandDirty(false), - }); - - useEffect(() => { - const initialPage = - typeof initialSelection === "object" && "page" in initialSelection - ? String(initialSelection.page) - : undefined; - if (initialPage) { - setActivePage( - pageOptions.some((item) => item.value === initialPage) - ? initialPage - : pageOptions[0]?.value, - ); - return; - } - if (!activePage && pageOptions.length) setActivePage(pageOptions[0]?.value); - }, [activePage, initialSelection, pageOptions]); - useEffect(() => { - if (hasInitializedSelection) return; - if (!pageOptions.length) return; - if (!initialSelection) return; - const requestedPage = - typeof initialSelection === "object" && "page" in initialSelection - ? String(initialSelection.page) - : undefined; - const resolvedPage = - requestedPage && pageOptions.some((item) => item.value === requestedPage) - ? requestedPage - : (pageOptions[0]?.value ?? "home"); - setSelection( - resolvePreviewSelection( - initialSelection.targetKey, - initialSelection.recordId, - visibleTargets, - resolvedPage, - ), - ); - setActivePage(resolvedPage); - setHasInitializedSelection(true); - }, [hasInitializedSelection, initialSelection, pageOptions, selectedPage, visibleTargets]); - useEffect(() => { - if (!initialSelection) setSelection(undefined); - }, [initialSelection, selectedPage, templateContent?.displayName]); - useEffect(() => { - if (!selection?.recordId) return; - const records = - selectedTarget?.source === "trust" - ? previewWorkspace?.trustItems - : previewWorkspace?.offerings; - if (records && !records.some((item) => item.id === selection.recordId)) setSelection(undefined); - }, [ - previewWorkspace?.offerings, - previewWorkspace?.trustItems, - selectedTarget?.source, - selection, - ]); - useEffect( - () => setRecordDraft(undefined), - [selection?.targetKey, selection?.recordId, selectedPage], - ); - const { previewBrand, handleValuesChange, resetPreview } = useBrandPreviewDraft({ - brand, - config, - sections, - form, - onDirty: () => setBrandDirty(true), - }); - const recordPreview = useRecordPreview({ recordDraft, selectedRecord, recordSection }); - useEffect(() => resetPreview(), [config, initialValues, resetPreview]); - const draftWorkspace = useDraftWorkspace({ - previewWorkspace, - previewBrand, - inquiryRuntime, - inquiryDraft, - recordDraft, - selectedRecord, - recordSection, - recordPreview, + const controller = usePresentationEditorController({ templateContent, + previewWorkspace, + config, + brand, + ...(initialSelection ? { initialSelection } : {}), }); - const selectedDetailOfferingId = - detailOfferingId && draftWorkspace?.offerings.some((item) => item.id === detailOfferingId) - ? detailOfferingId - : draftWorkspace?.offerings[0]?.id; return (
@@ -324,306 +65,33 @@ export function PresentationEditor({ {breakpoints.xs ? ( ({ - value: item.id, - label: item.name, - }))} - onChange={(value) => { - setDetailOfferingId(value); - setSelection(undefined); - }} - /> - ) : null} - - - -
-

- {selectedTarget?.label ?? pageTitle(selectedPage ?? "home")} -

- - {selectedPage === "detail" && !selectedTarget ? ( - { - void navigate("/admin/services"); - }} - > - 管理业务内容 - - } - /> - ) : activeTarget?.startsWith("inquiry-field:") && inquiryRuntime ? ( - item.key === inquiryRuntime.key) ?? - inquiryRuntime - } - fieldKey={activeTarget.slice("inquiry-field:".length)} - onDraft={setInquiryDraft} - onSaved={() => setInquiryDraft(undefined)} - /> - ) : selectedTarget?.composite ? ( - - {(selectedTarget.children ?? []).map((childKey) => { - const child = visibleTargets.find((item) => item.key === childKey); - if (!child) return null; - return ( - - ); - })} - - } - /> - ) : selectedRecordSource && selection?.recordId && selectedSection ? ( - selectedRecord ? ( - - setRecordDraft({ - source: selectedRecordSource, - id: selectedRecord.id, - values, - }) - } - onSaved={() => setRecordDraft(undefined)} - /> - ) : ( - { - if (selectedRecordSource === "offering") - void offeringsQuery.refetch(); - else void trustQuery.refetch(); - }} - > - 重试 - - ), - } - : {})} - /> - ) - ) : selectedRecordSource ? ( - - guardDraft(() => { - void navigate( - selectedRecordSource === "trust" - ? `/admin/trust?type=${encodeURIComponent(selectedTarget?.trustType ?? "")}` - : "/admin/services", - ); - }) - } - > - 配置此区域内容 - - } - /> - ) : !selectedTarget ? ( -
-

选择要编辑的区域

-

- 点击预览中的内容,或使用区域列表,查看并修改对应字段。 -

-
- ) : ( - save.mutate(values), - }} - onValuesChange={handleValuesChange} - /> - )} -
+ + ) : null} diff --git a/apps/admin/src/features/brand/PresentationEditorInspector.tsx b/apps/admin/src/features/brand/PresentationEditorInspector.tsx new file mode 100644 index 0000000..dbd7fea --- /dev/null +++ b/apps/admin/src/features/brand/PresentationEditorInspector.tsx @@ -0,0 +1,196 @@ +import { Alert, Button, Space } from "antd"; +import { StatusTag } from "../../components/StatusTag.js"; +import { RecordContentEditor } from "../../components/RecordContentEditor.js"; +import { InquiryPreviewEditor } from "./InquiryPreviewEditor.js"; +import { PresentationFormPanel } from "./PresentationFormPanel.js"; +import { pageTitle } from "./presentation-editor-helpers.js"; +import type { PresentationEditorController } from "./presentation-editor-controller.js"; + +export function PresentationEditorInspector({ + controller, +}: { + controller: PresentationEditorController; +}) { + const { + selectedPage, + selectedTarget, + activeTarget, + selection, + selectedSection, + selectedRecordSource, + selectedRecord, + recordSection, + editableSections, + relevantPageLinks, + inquiryRuntime, + draftWorkspace, + form, + initialValues, + save, + offeringsQuery, + trustQuery, + recordDraft, + inquiryDraft, + brandDirty, + guardDraft, + } = controller; + const recordQuery = selectedRecordSource === "trust" ? trustQuery : offeringsQuery; + + return ( +
+

+ {selectedTarget?.label ?? pageTitle(selectedPage ?? "home")} +

+ + {selectedPage === "detail" && !selectedTarget ? ( + { + void controller.navigate("/admin/services"); + }} + > + 管理业务内容 + + } + /> + ) : activeTarget?.startsWith("inquiry-field:") && inquiryRuntime ? ( + item.key === inquiryRuntime.key) ?? + inquiryRuntime + } + fieldKey={activeTarget.slice("inquiry-field:".length)} + onDraft={controller.setInquiryDraft} + onSaved={() => controller.setInquiryDraft(undefined)} + /> + ) : selectedTarget?.composite ? ( + + {(selectedTarget.children ?? []).map((childKey) => { + const child = visibleTarget(childKey, controller); + if (!child) return null; + return ( + + ); + })} + + } + /> + ) : selectedRecordSource && selection?.recordId && selectedSection ? ( + selectedRecord ? ( + + controller.setRecordDraft({ + source: selectedRecordSource, + id: selectedRecord.id, + values, + }) + } + onSaved={() => controller.setRecordDraft(undefined)} + /> + ) : ( + void recordQuery.refetch()}> + 重试 + + ), + } + : {})} + /> + ) + ) : selectedRecordSource ? ( + + guardDraft(() => { + void controller.navigate( + selectedRecordSource === "trust" + ? `/admin/trust?type=${encodeURIComponent(selectedTarget?.trustType ?? "")}` + : "/admin/services", + ); + }) + } + > + 配置此区域内容 + + } + /> + ) : !selectedTarget ? ( +
+

选择要编辑的区域

+

+ 点击预览中的内容,或使用区域列表,查看并修改对应字段。 +

+
+ ) : ( + save.mutate(values), + }} + onValuesChange={controller.handleValuesChange} + /> + )} +
+ ); +} + +function visibleTarget(key: string, controller: PresentationEditorController) { + return controller.visibleTargets.find((item) => item.key === key); +} diff --git a/apps/admin/src/features/brand/PresentationEditorPreview.tsx b/apps/admin/src/features/brand/PresentationEditorPreview.tsx new file mode 100644 index 0000000..cda7516 --- /dev/null +++ b/apps/admin/src/features/brand/PresentationEditorPreview.tsx @@ -0,0 +1,88 @@ +import { Button, Select, Space, Typography } from "antd"; +import type { BrandProfile, RuntimeWorkspace } from "@personal-brand/api-client"; +import { TemplatePreview } from "./TemplatePreview.js"; +import type { PreviewSelection, PreviewTarget } from "./preview-selection.js"; + +interface PresentationEditorPreviewProps { + previewBrand: BrandProfile; + draftWorkspace?: RuntimeWorkspace | undefined; + selectedPage: string | undefined; + selectedDetailOfferingId: string | undefined; + activeTarget: string | undefined; + selection: PreviewSelection | undefined; + visibleTargets: PreviewTarget[]; + onSelectDetailOffering: (id: string) => void; + onTargetSelected: (next?: PreviewSelection) => void; + onSelectPreviewTarget: (target: PreviewTarget) => void; +} + +export function PresentationEditorPreview({ + previewBrand, + draftWorkspace, + selectedPage, + selectedDetailOfferingId, + activeTarget, + selection, + visibleTargets, + onSelectDetailOffering, + onTargetSelected, + onSelectPreviewTarget, +}: PresentationEditorPreviewProps) { + return ( + <> +
+

页面预览

+ {selectedPage === "detail" && draftWorkspace?.offerings.length ? ( +