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 { 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"; interface PresentationEditorProps { templateContent: AdminTemplateContent | null | undefined; previewWorkspace?: RuntimeWorkspace | undefined; config: Record | null | undefined; brand: BrandProfile; loading?: boolean; error?: unknown; initialSelection?: PreviewSelection; } export function PresentationEditor({ templateContent, previewWorkspace, config, brand, loading, 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, templateContent, }); const selectedDetailOfferingId = detailOfferingId && draftWorkspace?.offerings.some((item) => item.id === detailOfferingId) ? detailOfferingId : draftWorkspace?.offerings[0]?.id; return (

小程序页面配置

按页面查找内容。这里修改的是草稿,发布后访客才会看到更新。

{templateContent ? : null}
{loading ? : null} {error ? ( ) : null} {!loading && !error && !templateContent ? ( ) : null} {templateContent ? ( <> {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}
); }