refactor: 拆分后台可视化编辑器
This commit is contained in:
1 parent
b190f2758a
commit
00aeaa3cb1
5 files changed
+736
-568
No files matched your search
@@ -1,7 +1,7 @@
|
|||||||
{
|
{
|
||||||
"schemaVersion": 1,
|
"schemaVersion": 1,
|
||||||
"scope": "模板完成证据登记,保留命令、日志与产物哈希",
|
"scope": "模板完成证据登记,保留命令、日志与产物哈希",
|
||||||
"generatedAt": "2026-10-23T04:26:48.766Z",
|
"generatedAt": "2026-10-23T04:56:52.289Z",
|
||||||
"templates": {
|
"templates": {
|
||||||
"acheng-campus-food": {
|
"acheng-campus-food": {
|
||||||
"real-manifest-interaction": {
|
"real-manifest-interaction": {
|
||||||
@@ -256,5 +256,5 @@
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
},
|
},
|
||||||
"sourceTreeHash": "4a4f5161e6da114837458987d9574d5d09f0d89f1d53e88cd838fc9b31946c9b"
|
"sourceTreeHash": "503e7b49d4b7c34c3405cd3d89e58e624a30e4c4831997480ebd7d4359c34052"
|
||||||
}
|
}
|
||||||
@@ -1,34 +1,15 @@
|
|||||||
import { Alert, Button, Grid, Select, Segmented, Space, Typography } from "antd";
|
import { Alert, Grid, Segmented, Select, Space } from "antd";
|
||||||
import { useQueryClient } from "@tanstack/react-query";
|
import type {
|
||||||
import { useCallback, useEffect, useMemo, useState } from "react";
|
AdminTemplateContent,
|
||||||
import { useNavigate } from "react-router-dom";
|
BrandProfile,
|
||||||
import type { AdminTemplateContent, RuntimeWorkspace } from "@personal-brand/api-client";
|
RuntimeWorkspace,
|
||||||
import type { BrandProfile } from "@personal-brand/api-client";
|
} from "@personal-brand/api-client";
|
||||||
import { StatusTag } from "../../components/StatusTag.js";
|
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 "./PresentationEditor.css";
|
||||||
import { useEditorDraft } from "./use-editor-draft.js";
|
import type { PreviewSelection } from "./preview-selection.js";
|
||||||
import {
|
import { usePresentationEditorController } from "./presentation-editor-controller.js";
|
||||||
usePresentationRecordQueries,
|
import { PresentationEditorInspector } from "./PresentationEditorInspector.js";
|
||||||
useBrandPresentationSections,
|
import { PresentationEditorPreview } from "./PresentationEditorPreview.js";
|
||||||
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 {
|
interface PresentationEditorProps {
|
||||||
templateContent: AdminTemplateContent | null | undefined;
|
templateContent: AdminTemplateContent | null | undefined;
|
||||||
@@ -49,254 +30,14 @@ export function PresentationEditor({
|
|||||||
error,
|
error,
|
||||||
initialSelection,
|
initialSelection,
|
||||||
}: PresentationEditorProps) {
|
}: PresentationEditorProps) {
|
||||||
const queryClient = useQueryClient();
|
|
||||||
const navigate = useNavigate();
|
|
||||||
const breakpoints = Grid.useBreakpoint();
|
const breakpoints = Grid.useBreakpoint();
|
||||||
const [activePage, setActivePage] = useState<string>();
|
const controller = usePresentationEditorController({
|
||||||
const [selection, setSelection] = useState<PreviewSelection | undefined>(initialSelection);
|
|
||||||
const [hasInitializedSelection, setHasInitializedSelection] = useState(Boolean(initialSelection));
|
|
||||||
const [detailOfferingId, setDetailOfferingId] = useState<string>();
|
|
||||||
const [recordDraft, setRecordDraft] = useState<RecordDraft>();
|
|
||||||
const [inquiryDraft, setInquiryDraft] =
|
|
||||||
useState<RuntimeWorkspace["inquiryForms"][number]["fields"]>();
|
|
||||||
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<string, string>();
|
|
||||||
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<PreviewTarget[]>(
|
|
||||||
() =>
|
|
||||||
[...(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,
|
templateContent,
|
||||||
|
previewWorkspace,
|
||||||
|
config,
|
||||||
|
brand,
|
||||||
|
...(initialSelection ? { initialSelection } : {}),
|
||||||
});
|
});
|
||||||
const selectedDetailOfferingId =
|
|
||||||
detailOfferingId && draftWorkspace?.offerings.some((item) => item.id === detailOfferingId)
|
|
||||||
? detailOfferingId
|
|
||||||
: draftWorkspace?.offerings[0]?.id;
|
|
||||||
|
|
||||||
return (
|
return (
|
||||||
<section className="presentation-editor" aria-labelledby="presentation-editor-title">
|
<section className="presentation-editor" aria-labelledby="presentation-editor-title">
|
||||||
@@ -324,306 +65,33 @@ export function PresentationEditor({
|
|||||||
{breakpoints.xs ? (
|
{breakpoints.xs ? (
|
||||||
<Select
|
<Select
|
||||||
aria-label="选择小程序页面"
|
aria-label="选择小程序页面"
|
||||||
value={selectedPage}
|
value={controller.selectedPage ?? null}
|
||||||
options={pageOptions}
|
options={controller.pageOptions}
|
||||||
onChange={(value) =>
|
onChange={controller.selectPage}
|
||||||
guardDraft(() => {
|
|
||||||
setBrandDirty(false);
|
|
||||||
form.setFieldsValue(initialValues);
|
|
||||||
resetPreview();
|
|
||||||
setRecordDraft(undefined);
|
|
||||||
setInquiryDraft(undefined);
|
|
||||||
setActivePage(value);
|
|
||||||
})
|
|
||||||
}
|
|
||||||
className="admin-field-w-2xl"
|
className="admin-field-w-2xl"
|
||||||
/>
|
/>
|
||||||
) : (
|
) : (
|
||||||
<Segmented
|
<Segmented
|
||||||
value={selectedPage}
|
value={controller.selectedPage ?? null}
|
||||||
options={pageOptions}
|
options={controller.pageOptions}
|
||||||
onChange={(value) =>
|
onChange={(value) => controller.selectPage(String(value))}
|
||||||
guardDraft(() => {
|
|
||||||
setBrandDirty(false);
|
|
||||||
form.setFieldsValue(initialValues);
|
|
||||||
resetPreview();
|
|
||||||
setRecordDraft(undefined);
|
|
||||||
setInquiryDraft(undefined);
|
|
||||||
setActivePage(String(value));
|
|
||||||
})
|
|
||||||
}
|
|
||||||
/>
|
/>
|
||||||
)}
|
)}
|
||||||
</Space>
|
</Space>
|
||||||
<div className="presentation-editor__workspace">
|
<div className="presentation-editor__workspace">
|
||||||
<div className="presentation-editor__preview">
|
<PresentationEditorPreview
|
||||||
<h3 className="admin-section-title admin-flush">页面预览</h3>
|
previewBrand={controller.previewBrand}
|
||||||
{selectedPage === "detail" && draftWorkspace?.offerings.length ? (
|
draftWorkspace={controller.draftWorkspace}
|
||||||
<Select
|
selectedPage={controller.selectedPage}
|
||||||
aria-label="选择详情页业务内容"
|
selectedDetailOfferingId={controller.selectedDetailOfferingId}
|
||||||
className="admin-block admin-offset-bottom-sm"
|
activeTarget={controller.activeTarget}
|
||||||
value={selectedDetailOfferingId}
|
selection={controller.selection}
|
||||||
options={draftWorkspace.offerings.map((item) => ({
|
visibleTargets={controller.visibleTargets}
|
||||||
value: item.id,
|
onSelectDetailOffering={controller.selectDetailOffering}
|
||||||
label: item.name,
|
onTargetSelected={controller.handleTargetSelected}
|
||||||
}))}
|
onSelectPreviewTarget={controller.selectPreviewTarget}
|
||||||
onChange={(value) => {
|
/>
|
||||||
setDetailOfferingId(value);
|
<PresentationEditorInspector controller={controller} />
|
||||||
setSelection(undefined);
|
|
||||||
}}
|
|
||||||
/>
|
|
||||||
) : null}
|
|
||||||
<TemplatePreview
|
|
||||||
brand={previewBrand}
|
|
||||||
{...(draftWorkspace ? { workspace: draftWorkspace } : {})}
|
|
||||||
page={selectedPage ?? "home"}
|
|
||||||
{...(selectedPage === "detail" && selectedDetailOfferingId
|
|
||||||
? { offeringId: selectedDetailOfferingId }
|
|
||||||
: {})}
|
|
||||||
{...(activeTarget ? { targetKey: activeTarget } : {})}
|
|
||||||
{...(selection ? { selection } : {})}
|
|
||||||
targets={visibleTargets}
|
|
||||||
onTargetSelected={handleTargetSelected}
|
|
||||||
/>
|
|
||||||
</div>
|
|
||||||
<aside className="presentation-editor__outline" aria-label="页面区域">
|
|
||||||
<h3 className="admin-section-title admin-flush">页面区域</h3>
|
|
||||||
<p className="admin-section-description">选择区域,编辑对应内容</p>
|
|
||||||
{visibleTargets.length ? (
|
|
||||||
<Space className="presentation-editor__regions admin-offset-top" wrap>
|
|
||||||
{visibleTargets.map((target) => (
|
|
||||||
<Button
|
|
||||||
key={target.key}
|
|
||||||
className={
|
|
||||||
visibleTargets.some((parent) => parent.children?.includes(target.key))
|
|
||||||
? "is-child-region"
|
|
||||||
: ""
|
|
||||||
}
|
|
||||||
aria-pressed={activeTarget === target.key}
|
|
||||||
type={activeTarget === target.key ? "primary" : "default"}
|
|
||||||
onClick={() => {
|
|
||||||
const recordId =
|
|
||||||
target.source === "offering"
|
|
||||||
? selectedPage === "detail"
|
|
||||||
? selectedDetailOfferingId
|
|
||||||
: draftWorkspace?.offerings[0]?.id
|
|
||||||
: target.source === "trust"
|
|
||||||
? previewTrustRecordId(
|
|
||||||
draftWorkspace?.trustItems ?? [],
|
|
||||||
target.trustType,
|
|
||||||
)
|
|
||||||
: undefined;
|
|
||||||
handleTargetSelected({
|
|
||||||
targetKey: target.key,
|
|
||||||
...(recordId ? { recordId } : {}),
|
|
||||||
});
|
|
||||||
}}
|
|
||||||
>
|
|
||||||
{target.label}
|
|
||||||
</Button>
|
|
||||||
))}
|
|
||||||
</Space>
|
|
||||||
) : null}
|
|
||||||
{!visibleTargets.length ? (
|
|
||||||
<Typography.Text type="secondary">此页为动态预览</Typography.Text>
|
|
||||||
) : null}
|
|
||||||
</aside>
|
|
||||||
<div className="presentation-editor__inspector">
|
|
||||||
<h3 className="admin-section-title admin-flush">
|
|
||||||
{selectedTarget?.label ?? pageTitle(selectedPage ?? "home")}
|
|
||||||
</h3>
|
|
||||||
<StatusTag
|
|
||||||
tone={
|
|
||||||
save.isError
|
|
||||||
? "error"
|
|
||||||
: save.isPending || brandDirty || recordDraft || inquiryDraft
|
|
||||||
? "warning"
|
|
||||||
: "neutral"
|
|
||||||
}
|
|
||||||
label={
|
|
||||||
save.isPending
|
|
||||||
? "正在保存"
|
|
||||||
: save.isError
|
|
||||||
? "保存失败,请重试"
|
|
||||||
: brandDirty || recordDraft || inquiryDraft
|
|
||||||
? "有未保存修改"
|
|
||||||
: "草稿已同步"
|
|
||||||
}
|
|
||||||
/>
|
|
||||||
{selectedPage === "detail" && !selectedTarget ? (
|
|
||||||
<Alert
|
|
||||||
type="info"
|
|
||||||
showIcon
|
|
||||||
title="详情页由所选业务内容动态生成"
|
|
||||||
description="选择业务内容预览详情,记录内容在业务提供项中维护;公共标题和按钮文案可从左侧区域列表配置,对全部详情生效。"
|
|
||||||
action={
|
|
||||||
<Button
|
|
||||||
onClick={() => {
|
|
||||||
void navigate("/admin/services");
|
|
||||||
}}
|
|
||||||
>
|
|
||||||
管理业务内容
|
|
||||||
</Button>
|
|
||||||
}
|
|
||||||
/>
|
|
||||||
) : activeTarget?.startsWith("inquiry-field:") && inquiryRuntime ? (
|
|
||||||
<InquiryPreviewEditor
|
|
||||||
runtime={
|
|
||||||
draftWorkspace?.inquiryForms.find((item) => item.key === inquiryRuntime.key) ??
|
|
||||||
inquiryRuntime
|
|
||||||
}
|
|
||||||
fieldKey={activeTarget.slice("inquiry-field:".length)}
|
|
||||||
onDraft={setInquiryDraft}
|
|
||||||
onSaved={() => setInquiryDraft(undefined)}
|
|
||||||
/>
|
|
||||||
) : selectedTarget?.composite ? (
|
|
||||||
<Alert
|
|
||||||
type="info"
|
|
||||||
showIcon
|
|
||||||
title="这是一个组合区域"
|
|
||||||
description={
|
|
||||||
<Space wrap>
|
|
||||||
{(selectedTarget.children ?? []).map((childKey) => {
|
|
||||||
const child = visibleTargets.find((item) => item.key === childKey);
|
|
||||||
if (!child) return null;
|
|
||||||
return (
|
|
||||||
<Button
|
|
||||||
key={child.key}
|
|
||||||
onClick={() => {
|
|
||||||
const recordId =
|
|
||||||
child.source === "offering"
|
|
||||||
? selectedPage === "detail"
|
|
||||||
? selectedDetailOfferingId
|
|
||||||
: draftWorkspace?.offerings[0]?.id
|
|
||||||
: child.source === "trust"
|
|
||||||
? previewTrustRecordId(
|
|
||||||
draftWorkspace?.trustItems ?? [],
|
|
||||||
child.trustType,
|
|
||||||
)
|
|
||||||
: undefined;
|
|
||||||
handleTargetSelected({
|
|
||||||
targetKey: child.key,
|
|
||||||
...(recordId ? { recordId } : {}),
|
|
||||||
});
|
|
||||||
}}
|
|
||||||
>
|
|
||||||
编辑{child.label}
|
|
||||||
</Button>
|
|
||||||
);
|
|
||||||
})}
|
|
||||||
</Space>
|
|
||||||
}
|
|
||||||
/>
|
|
||||||
) : selectedRecordSource && selection?.recordId && selectedSection ? (
|
|
||||||
selectedRecord ? (
|
|
||||||
<RecordContentEditor
|
|
||||||
key={`${selectedRecordSource}:${selectedRecord.id}:${selectedSection.key}`}
|
|
||||||
source={selectedRecordSource}
|
|
||||||
record={selectedRecord}
|
|
||||||
section={recordSection!}
|
|
||||||
context="visual"
|
|
||||||
onDraft={(values) =>
|
|
||||||
setRecordDraft({
|
|
||||||
source: selectedRecordSource,
|
|
||||||
id: selectedRecord.id,
|
|
||||||
values,
|
|
||||||
})
|
|
||||||
}
|
|
||||||
onSaved={() => setRecordDraft(undefined)}
|
|
||||||
/>
|
|
||||||
) : (
|
|
||||||
<Alert
|
|
||||||
type={
|
|
||||||
(
|
|
||||||
selectedRecordSource === "offering"
|
|
||||||
? offeringsQuery.isError
|
|
||||||
: trustQuery.isError
|
|
||||||
)
|
|
||||||
? "error"
|
|
||||||
: "info"
|
|
||||||
}
|
|
||||||
title={
|
|
||||||
(
|
|
||||||
selectedRecordSource === "offering"
|
|
||||||
? offeringsQuery.isError
|
|
||||||
: trustQuery.isError
|
|
||||||
)
|
|
||||||
? "内容读取失败"
|
|
||||||
: (
|
|
||||||
selectedRecordSource === "offering"
|
|
||||||
? offeringsQuery.isSuccess
|
|
||||||
: trustQuery.isSuccess
|
|
||||||
)
|
|
||||||
? "对应内容已不存在,请重新选择页面中的区域"
|
|
||||||
: "正在读取对应内容"
|
|
||||||
}
|
|
||||||
{...((
|
|
||||||
selectedRecordSource === "offering"
|
|
||||||
? offeringsQuery.isError
|
|
||||||
: trustQuery.isError
|
|
||||||
)
|
|
||||||
? {
|
|
||||||
action: (
|
|
||||||
<Button
|
|
||||||
size="small"
|
|
||||||
onClick={() => {
|
|
||||||
if (selectedRecordSource === "offering")
|
|
||||||
void offeringsQuery.refetch();
|
|
||||||
else void trustQuery.refetch();
|
|
||||||
}}
|
|
||||||
>
|
|
||||||
重试
|
|
||||||
</Button>
|
|
||||||
),
|
|
||||||
}
|
|
||||||
: {})}
|
|
||||||
/>
|
|
||||||
)
|
|
||||||
) : selectedRecordSource ? (
|
|
||||||
<Alert
|
|
||||||
type="info"
|
|
||||||
showIcon
|
|
||||||
title="此区域尚未配置内容"
|
|
||||||
description="创建并启用对应内容后,即可在预览中点选编辑。"
|
|
||||||
action={
|
|
||||||
<Button
|
|
||||||
onClick={() =>
|
|
||||||
guardDraft(() => {
|
|
||||||
void navigate(
|
|
||||||
selectedRecordSource === "trust"
|
|
||||||
? `/admin/trust?type=${encodeURIComponent(selectedTarget?.trustType ?? "")}`
|
|
||||||
: "/admin/services",
|
|
||||||
);
|
|
||||||
})
|
|
||||||
}
|
|
||||||
>
|
|
||||||
配置此区域内容
|
|
||||||
</Button>
|
|
||||||
}
|
|
||||||
/>
|
|
||||||
) : !selectedTarget ? (
|
|
||||||
<div className="presentation-editor__empty">
|
|
||||||
<h3 className="admin-subhead">选择要编辑的区域</h3>
|
|
||||||
<p className="admin-section-description">
|
|
||||||
点击预览中的内容,或使用区域列表,查看并修改对应字段。
|
|
||||||
</p>
|
|
||||||
</div>
|
|
||||||
) : (
|
|
||||||
<PresentationFormPanel
|
|
||||||
form={form}
|
|
||||||
initialValues={initialValues}
|
|
||||||
editableSections={editableSections}
|
|
||||||
relevantPageLinks={relevantPageLinks}
|
|
||||||
selectedTarget={selectedTarget}
|
|
||||||
save={{
|
|
||||||
error: save.error,
|
|
||||||
isPending: save.isPending,
|
|
||||||
mutate: (values) => save.mutate(values),
|
|
||||||
}}
|
|
||||||
onValuesChange={handleValuesChange}
|
|
||||||
/>
|
|
||||||
)}
|
|
||||||
</div>
|
|
||||||
</div>
|
</div>
|
||||||
</>
|
</>
|
||||||
) : null}
|
) : null}
|
||||||
|
|||||||
@@ -0,0 +1,195 @@
|
|||||||
|
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 (
|
||||||
|
<div className="presentation-editor__inspector">
|
||||||
|
<h3 className="admin-section-title admin-flush">
|
||||||
|
{selectedTarget?.label ?? pageTitle(selectedPage ?? "home")}
|
||||||
|
</h3>
|
||||||
|
<StatusTag
|
||||||
|
tone={
|
||||||
|
save.isError
|
||||||
|
? "error"
|
||||||
|
: save.isPending || brandDirty || recordDraft || inquiryDraft
|
||||||
|
? "warning"
|
||||||
|
: "neutral"
|
||||||
|
}
|
||||||
|
label={
|
||||||
|
save.isPending
|
||||||
|
? "正在保存"
|
||||||
|
: save.isError
|
||||||
|
? "保存失败,请重试"
|
||||||
|
: brandDirty || recordDraft || inquiryDraft
|
||||||
|
? "有未保存修改"
|
||||||
|
: "草稿已同步"
|
||||||
|
}
|
||||||
|
/>
|
||||||
|
{selectedPage === "detail" && !selectedTarget ? (
|
||||||
|
<Alert
|
||||||
|
type="info"
|
||||||
|
showIcon
|
||||||
|
title="详情页由所选业务内容动态生成"
|
||||||
|
description="选择业务内容预览详情,记录内容在业务提供项中维护;公共标题和按钮文案可从左侧区域列表配置,对全部详情生效。"
|
||||||
|
action={
|
||||||
|
<Button
|
||||||
|
onClick={() => {
|
||||||
|
void controller.navigate("/admin/services");
|
||||||
|
}}
|
||||||
|
>
|
||||||
|
管理业务内容
|
||||||
|
</Button>
|
||||||
|
}
|
||||||
|
/>
|
||||||
|
) : activeTarget?.startsWith("inquiry-field:") && inquiryRuntime ? (
|
||||||
|
<InquiryPreviewEditor
|
||||||
|
runtime={
|
||||||
|
draftWorkspace?.inquiryForms.find((item) => item.key === inquiryRuntime.key) ??
|
||||||
|
inquiryRuntime
|
||||||
|
}
|
||||||
|
fieldKey={activeTarget.slice("inquiry-field:".length)}
|
||||||
|
onDraft={controller.setInquiryDraft}
|
||||||
|
onSaved={() => controller.setInquiryDraft(undefined)}
|
||||||
|
/>
|
||||||
|
) : selectedTarget?.composite ? (
|
||||||
|
<Alert
|
||||||
|
type="info"
|
||||||
|
showIcon
|
||||||
|
title="这是一个组合区域"
|
||||||
|
description={
|
||||||
|
<Space wrap>
|
||||||
|
{(selectedTarget.children ?? []).map((childKey) => {
|
||||||
|
const child = visibleTarget(childKey, controller);
|
||||||
|
if (!child) return null;
|
||||||
|
return (
|
||||||
|
<Button key={child.key} onClick={() => controller.selectPreviewTarget(child)}>
|
||||||
|
编辑{child.label}
|
||||||
|
</Button>
|
||||||
|
);
|
||||||
|
})}
|
||||||
|
</Space>
|
||||||
|
}
|
||||||
|
/>
|
||||||
|
) : selectedRecordSource && selection?.recordId && selectedSection ? (
|
||||||
|
selectedRecord ? (
|
||||||
|
<RecordContentEditor
|
||||||
|
key={`${selectedRecordSource}:${selectedRecord.id}:${selectedSection.key}`}
|
||||||
|
source={selectedRecordSource}
|
||||||
|
record={selectedRecord}
|
||||||
|
section={recordSection!}
|
||||||
|
context="visual"
|
||||||
|
onDraft={(values) =>
|
||||||
|
controller.setRecordDraft({
|
||||||
|
source: selectedRecordSource,
|
||||||
|
id: selectedRecord.id,
|
||||||
|
values,
|
||||||
|
})
|
||||||
|
}
|
||||||
|
onSaved={() => controller.setRecordDraft(undefined)}
|
||||||
|
/>
|
||||||
|
) : (
|
||||||
|
<Alert
|
||||||
|
type={recordQuery.isError ? "error" : "info"}
|
||||||
|
title={
|
||||||
|
recordQuery.isError
|
||||||
|
? "内容读取失败"
|
||||||
|
: recordQuery.isSuccess
|
||||||
|
? "对应内容已不存在,请重新选择页面中的区域"
|
||||||
|
: "正在读取对应内容"
|
||||||
|
}
|
||||||
|
{...(recordQuery.isError
|
||||||
|
? {
|
||||||
|
action: (
|
||||||
|
<Button size="small" onClick={() => void recordQuery.refetch()}>
|
||||||
|
重试
|
||||||
|
</Button>
|
||||||
|
),
|
||||||
|
}
|
||||||
|
: {})}
|
||||||
|
/>
|
||||||
|
)
|
||||||
|
) : selectedRecordSource ? (
|
||||||
|
<Alert
|
||||||
|
type="info"
|
||||||
|
showIcon
|
||||||
|
title="此区域尚未配置内容"
|
||||||
|
description="创建并启用对应内容后,即可在预览中点选编辑。"
|
||||||
|
action={
|
||||||
|
<Button
|
||||||
|
onClick={() =>
|
||||||
|
guardDraft(() => {
|
||||||
|
void controller.navigate(
|
||||||
|
selectedRecordSource === "trust"
|
||||||
|
? `/admin/trust?type=${encodeURIComponent(selectedTarget?.trustType ?? "")}`
|
||||||
|
: "/admin/services",
|
||||||
|
);
|
||||||
|
})
|
||||||
|
}
|
||||||
|
>
|
||||||
|
配置此区域内容
|
||||||
|
</Button>
|
||||||
|
}
|
||||||
|
/>
|
||||||
|
) : !selectedTarget ? (
|
||||||
|
<div className="presentation-editor__empty">
|
||||||
|
<h3 className="admin-subhead">选择要编辑的区域</h3>
|
||||||
|
<p className="admin-section-description">
|
||||||
|
点击预览中的内容,或使用区域列表,查看并修改对应字段。
|
||||||
|
</p>
|
||||||
|
</div>
|
||||||
|
) : (
|
||||||
|
<PresentationFormPanel
|
||||||
|
form={form}
|
||||||
|
initialValues={initialValues}
|
||||||
|
editableSections={editableSections}
|
||||||
|
relevantPageLinks={relevantPageLinks}
|
||||||
|
selectedTarget={selectedTarget}
|
||||||
|
save={{
|
||||||
|
error: save.error,
|
||||||
|
isPending: save.isPending,
|
||||||
|
mutate: (values) => save.mutate(values),
|
||||||
|
}}
|
||||||
|
onValuesChange={controller.handleValuesChange}
|
||||||
|
/>
|
||||||
|
)}
|
||||||
|
</div>
|
||||||
|
);
|
||||||
|
}
|
||||||
|
|
||||||
|
function visibleTarget(key: string, controller: PresentationEditorController) {
|
||||||
|
return controller.visibleTargets.find((item) => item.key === key);
|
||||||
|
}
|
||||||
@@ -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 (
|
||||||
|
<>
|
||||||
|
<div className="presentation-editor__preview">
|
||||||
|
<h3 className="admin-section-title admin-flush">页面预览</h3>
|
||||||
|
{selectedPage === "detail" && draftWorkspace?.offerings.length ? (
|
||||||
|
<Select
|
||||||
|
aria-label="选择详情页业务内容"
|
||||||
|
className="admin-block admin-offset-bottom-sm"
|
||||||
|
value={selectedDetailOfferingId ?? null}
|
||||||
|
options={draftWorkspace.offerings.map((item) => ({
|
||||||
|
value: item.id,
|
||||||
|
label: item.name,
|
||||||
|
}))}
|
||||||
|
onChange={onSelectDetailOffering}
|
||||||
|
/>
|
||||||
|
) : null}
|
||||||
|
<TemplatePreview
|
||||||
|
brand={previewBrand}
|
||||||
|
{...(draftWorkspace ? { workspace: draftWorkspace } : {})}
|
||||||
|
page={selectedPage ?? "home"}
|
||||||
|
{...(selectedPage === "detail" && selectedDetailOfferingId
|
||||||
|
? { offeringId: selectedDetailOfferingId }
|
||||||
|
: {})}
|
||||||
|
{...(activeTarget ? { targetKey: activeTarget } : {})}
|
||||||
|
{...(selection ? { selection } : {})}
|
||||||
|
targets={visibleTargets}
|
||||||
|
onTargetSelected={onTargetSelected}
|
||||||
|
/>
|
||||||
|
</div>
|
||||||
|
<aside className="presentation-editor__outline" aria-label="页面区域">
|
||||||
|
<h3 className="admin-section-title admin-flush">页面区域</h3>
|
||||||
|
<p className="admin-section-description">选择区域,编辑对应内容</p>
|
||||||
|
{visibleTargets.length ? (
|
||||||
|
<Space className="presentation-editor__regions admin-offset-top" wrap>
|
||||||
|
{visibleTargets.map((target) => (
|
||||||
|
<Button
|
||||||
|
key={target.key}
|
||||||
|
className={
|
||||||
|
visibleTargets.some((parent) => parent.children?.includes(target.key))
|
||||||
|
? "is-child-region"
|
||||||
|
: ""
|
||||||
|
}
|
||||||
|
aria-pressed={activeTarget === target.key}
|
||||||
|
type={activeTarget === target.key ? "primary" : "default"}
|
||||||
|
onClick={() => onSelectPreviewTarget(target)}
|
||||||
|
>
|
||||||
|
{target.label}
|
||||||
|
</Button>
|
||||||
|
))}
|
||||||
|
</Space>
|
||||||
|
) : null}
|
||||||
|
{!visibleTargets.length ? (
|
||||||
|
<Typography.Text type="secondary">此页为动态预览</Typography.Text>
|
||||||
|
) : null}
|
||||||
|
</aside>
|
||||||
|
</>
|
||||||
|
);
|
||||||
|
}
|
||||||
@@ -0,0 +1,417 @@
|
|||||||
|
import { useQueryClient } from "@tanstack/react-query";
|
||||||
|
import { useCallback, useEffect, useMemo, useState } from "react";
|
||||||
|
import { useNavigate, type NavigateFunction } from "react-router-dom";
|
||||||
|
import type {
|
||||||
|
AdminTemplateContent,
|
||||||
|
BrandProfile,
|
||||||
|
Offering,
|
||||||
|
RuntimeWorkspace,
|
||||||
|
TemplateAdminSection,
|
||||||
|
TrustContent,
|
||||||
|
} from "@personal-brand/api-client";
|
||||||
|
import type { FormInstance } from "antd";
|
||||||
|
import { filterSectionsForPreviewTarget } from "../../lib/presentation/presentation-bindings.js";
|
||||||
|
import type { PresentationValues } from "../../lib/presentation/presentation-bindings.js";
|
||||||
|
import {
|
||||||
|
resolvePreviewSelection,
|
||||||
|
previewTrustRecordId,
|
||||||
|
type PreviewSelection,
|
||||||
|
type PreviewTarget,
|
||||||
|
} from "./preview-selection.js";
|
||||||
|
import { useEditorDraft } from "./use-editor-draft.js";
|
||||||
|
import {
|
||||||
|
useBrandPresentationForm,
|
||||||
|
useBrandPresentationSections,
|
||||||
|
useBrandPreviewDraft,
|
||||||
|
useBrandSaveMutation,
|
||||||
|
usePresentationRecordQueries,
|
||||||
|
} from "./presentation-editor-state.js";
|
||||||
|
import { useRecordPreview, type RecordDraft } from "./presentation-editor-record-preview.js";
|
||||||
|
import { pageTitle, previewTargetSource, trustSection } from "./presentation-editor-helpers.js";
|
||||||
|
import { useDraftWorkspace } from "./use-draft-workspace.js";
|
||||||
|
|
||||||
|
export interface PresentationEditorControllerInput {
|
||||||
|
templateContent: AdminTemplateContent | null | undefined;
|
||||||
|
previewWorkspace?: RuntimeWorkspace | undefined;
|
||||||
|
config: Record<string, unknown> | null | undefined;
|
||||||
|
brand: BrandProfile;
|
||||||
|
initialSelection?: PreviewSelection;
|
||||||
|
}
|
||||||
|
|
||||||
|
export interface PresentationRecordQuery {
|
||||||
|
isError: boolean;
|
||||||
|
isSuccess: boolean;
|
||||||
|
refetch: () => unknown;
|
||||||
|
}
|
||||||
|
|
||||||
|
export interface PresentationEditorController {
|
||||||
|
pageOptions: { value: string; label: string }[];
|
||||||
|
selectedPage: string | undefined;
|
||||||
|
visibleTargets: PreviewTarget[];
|
||||||
|
selectedTarget: PreviewTarget | undefined;
|
||||||
|
selectedSection: TemplateAdminSection | undefined;
|
||||||
|
selectedRecordSource: "offering" | "trust" | undefined;
|
||||||
|
selectedRecord: Offering | TrustContent | undefined;
|
||||||
|
recordSection: TemplateAdminSection | undefined;
|
||||||
|
editableSections: TemplateAdminSection[];
|
||||||
|
relevantPageLinks: { target: string; label: string | undefined }[];
|
||||||
|
inquiryRuntime: RuntimeWorkspace["inquiryForms"][number] | undefined;
|
||||||
|
draftWorkspace: RuntimeWorkspace | undefined;
|
||||||
|
previewBrand: BrandProfile;
|
||||||
|
selectedDetailOfferingId: string | undefined;
|
||||||
|
form: FormInstance<PresentationValues>;
|
||||||
|
initialValues: PresentationValues;
|
||||||
|
save: {
|
||||||
|
error: unknown;
|
||||||
|
isError: boolean;
|
||||||
|
isPending: boolean;
|
||||||
|
mutate: (values: PresentationValues) => void;
|
||||||
|
};
|
||||||
|
offeringsQuery: PresentationRecordQuery;
|
||||||
|
trustQuery: PresentationRecordQuery;
|
||||||
|
recordDraft: RecordDraft | undefined;
|
||||||
|
inquiryDraft: RuntimeWorkspace["inquiryForms"][number]["fields"] | undefined;
|
||||||
|
brandDirty: boolean;
|
||||||
|
selection: PreviewSelection | undefined;
|
||||||
|
activeTarget: string | undefined;
|
||||||
|
guardDraft: (action: () => void) => void;
|
||||||
|
selectPage: (page: string) => void;
|
||||||
|
selectDetailOffering: (id: string) => void;
|
||||||
|
selectPreviewTarget: (target: PreviewTarget) => void;
|
||||||
|
handleTargetSelected: (next?: PreviewSelection) => void;
|
||||||
|
setRecordDraft: (draft: RecordDraft | undefined) => void;
|
||||||
|
setInquiryDraft: (fields: RuntimeWorkspace["inquiryForms"][number]["fields"] | undefined) => void;
|
||||||
|
handleValuesChange: (changed: PresentationValues, allValues: PresentationValues) => void;
|
||||||
|
navigate: NavigateFunction;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* PresentationEditor 的状态与派生数据集中在此处。
|
||||||
|
* 视图组件只负责渲染,避免页面组件继续膨胀。
|
||||||
|
*/
|
||||||
|
export function usePresentationEditorController({
|
||||||
|
templateContent,
|
||||||
|
previewWorkspace,
|
||||||
|
config,
|
||||||
|
brand,
|
||||||
|
initialSelection,
|
||||||
|
}: PresentationEditorControllerInput): PresentationEditorController {
|
||||||
|
const queryClient = useQueryClient();
|
||||||
|
const navigate = useNavigate();
|
||||||
|
const [activePage, setActivePage] = useState<string>();
|
||||||
|
const [selection, setSelection] = useState<PreviewSelection | undefined>(initialSelection);
|
||||||
|
const [hasInitializedSelection, setHasInitializedSelection] = useState(Boolean(initialSelection));
|
||||||
|
const [detailOfferingId, setDetailOfferingId] = useState<string>();
|
||||||
|
const [recordDraft, setRecordDraft] = useState<RecordDraft>();
|
||||||
|
const [inquiryDraft, setInquiryDraft] =
|
||||||
|
useState<RuntimeWorkspace["inquiryForms"][number]["fields"]>();
|
||||||
|
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<string, string>();
|
||||||
|
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<PreviewTarget[]>(
|
||||||
|
() =>
|
||||||
|
[...(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;
|
||||||
|
|
||||||
|
const selectPage = useCallback(
|
||||||
|
(page: string) => {
|
||||||
|
guardDraft(() => {
|
||||||
|
setBrandDirty(false);
|
||||||
|
form.setFieldsValue(initialValues);
|
||||||
|
resetPreview();
|
||||||
|
setRecordDraft(undefined);
|
||||||
|
setInquiryDraft(undefined);
|
||||||
|
setActivePage(page);
|
||||||
|
});
|
||||||
|
},
|
||||||
|
[guardDraft, form, initialValues, resetPreview],
|
||||||
|
);
|
||||||
|
const selectDetailOffering = useCallback((id: string) => {
|
||||||
|
setDetailOfferingId(id);
|
||||||
|
setSelection(undefined);
|
||||||
|
}, []);
|
||||||
|
const selectPreviewTarget = useCallback(
|
||||||
|
(target: PreviewTarget) => {
|
||||||
|
const recordId =
|
||||||
|
target.source === "offering"
|
||||||
|
? selectedPage === "detail"
|
||||||
|
? selectedDetailOfferingId
|
||||||
|
: draftWorkspace?.offerings[0]?.id
|
||||||
|
: target.source === "trust"
|
||||||
|
? previewTrustRecordId(draftWorkspace?.trustItems ?? [], target.trustType)
|
||||||
|
: undefined;
|
||||||
|
handleTargetSelected({
|
||||||
|
targetKey: target.key,
|
||||||
|
...(recordId ? { recordId } : {}),
|
||||||
|
});
|
||||||
|
},
|
||||||
|
[handleTargetSelected, selectedPage, selectedDetailOfferingId, draftWorkspace],
|
||||||
|
);
|
||||||
|
|
||||||
|
return {
|
||||||
|
pageOptions,
|
||||||
|
selectedPage,
|
||||||
|
visibleTargets,
|
||||||
|
selectedTarget,
|
||||||
|
selectedSection,
|
||||||
|
selectedRecordSource,
|
||||||
|
selectedRecord,
|
||||||
|
recordSection,
|
||||||
|
editableSections,
|
||||||
|
relevantPageLinks,
|
||||||
|
inquiryRuntime,
|
||||||
|
draftWorkspace,
|
||||||
|
previewBrand,
|
||||||
|
selectedDetailOfferingId,
|
||||||
|
form,
|
||||||
|
initialValues,
|
||||||
|
save,
|
||||||
|
offeringsQuery,
|
||||||
|
trustQuery,
|
||||||
|
recordDraft,
|
||||||
|
inquiryDraft,
|
||||||
|
brandDirty,
|
||||||
|
selection,
|
||||||
|
activeTarget,
|
||||||
|
guardDraft,
|
||||||
|
selectPage,
|
||||||
|
selectDetailOffering,
|
||||||
|
selectPreviewTarget,
|
||||||
|
handleTargetSelected,
|
||||||
|
setRecordDraft,
|
||||||
|
setInquiryDraft,
|
||||||
|
handleValuesChange,
|
||||||
|
navigate,
|
||||||
|
};
|
||||||
|
}
|
||||||
Reference in new issue
Block a user