CI / Static checks (push) Has been cancelled
CI / Production build (push) Has been cancelled
CI / API integration (push) Has been cancelled
CI / Taro builds (acheng-campus-food) (push) Has been cancelled
CI / Taro builds (atelier-solopreneur) (push) Has been cancelled
CI / Taro builds (linjing-editorial) (push) Has been cancelled
CI / Browser regression (push) Has been cancelled
633 lines
25 KiB
TypeScript
633 lines
25 KiB
TypeScript
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<string, unknown> | 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<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;
|
|
|
|
return (
|
|
<section className="presentation-editor" aria-labelledby="presentation-editor-title">
|
|
<div className="presentation-editor__header">
|
|
<div>
|
|
<h2 id="presentation-editor-title" className="admin-section-title">
|
|
小程序页面配置
|
|
</h2>
|
|
<p className="admin-section-description">
|
|
按页面查找内容。这里修改的是草稿,发布后访客才会看到更新。
|
|
</p>
|
|
</div>
|
|
{templateContent ? <StatusTag tone="info" label={templateContent.displayName} /> : null}
|
|
</div>
|
|
{loading ? <Alert type="info" showIcon message="正在读取当前页面方案的编辑项目" /> : null}
|
|
{error ? (
|
|
<Alert type="warning" showIcon message="页面方案编辑项目读取失败,请刷新后重试。" />
|
|
) : null}
|
|
{!loading && !error && !templateContent ? (
|
|
<Alert type="warning" showIcon message="当前页面方案暂未提供可编辑的页面内容。" />
|
|
) : null}
|
|
{templateContent ? (
|
|
<>
|
|
<Space className="presentation-editor__navigation" wrap>
|
|
{breakpoints.xs ? (
|
|
<Select
|
|
aria-label="选择小程序页面"
|
|
value={selectedPage}
|
|
options={pageOptions}
|
|
onChange={(value) =>
|
|
guardDraft(() => {
|
|
setBrandDirty(false);
|
|
form.setFieldsValue(initialValues);
|
|
resetPreview();
|
|
setRecordDraft(undefined);
|
|
setInquiryDraft(undefined);
|
|
setActivePage(value);
|
|
})
|
|
}
|
|
className="admin-field-w-2xl"
|
|
/>
|
|
) : (
|
|
<Segmented
|
|
value={selectedPage}
|
|
options={pageOptions}
|
|
onChange={(value) =>
|
|
guardDraft(() => {
|
|
setBrandDirty(false);
|
|
form.setFieldsValue(initialValues);
|
|
resetPreview();
|
|
setRecordDraft(undefined);
|
|
setInquiryDraft(undefined);
|
|
setActivePage(String(value));
|
|
})
|
|
}
|
|
/>
|
|
)}
|
|
</Space>
|
|
<div className="presentation-editor__workspace">
|
|
<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}
|
|
options={draftWorkspace.offerings.map((item) => ({
|
|
value: item.id,
|
|
label: item.name,
|
|
}))}
|
|
onChange={(value) => {
|
|
setDetailOfferingId(value);
|
|
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
|
|
message="详情页由所选业务内容动态生成"
|
|
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
|
|
message="这是一个组合区域"
|
|
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"
|
|
}
|
|
message={
|
|
(
|
|
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
|
|
message="此区域尚未配置内容"
|
|
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>
|
|
</>
|
|
) : null}
|
|
</section>
|
|
);
|
|
}
|