refactor: 拆分品牌展示编辑器职责
This commit is contained in:
1 parent
b23120477f
commit
6f18c67315
5 files changed
+376
-315
No files matched your search
@@ -1,30 +1,13 @@
|
|||||||
import {
|
import { Alert, Button, Grid, Select, Segmented, Space, Typography } from "antd";
|
||||||
Alert,
|
|
||||||
Button,
|
|
||||||
Divider,
|
|
||||||
Col,
|
|
||||||
Form,
|
|
||||||
Grid,
|
|
||||||
Row,
|
|
||||||
Select,
|
|
||||||
Segmented,
|
|
||||||
Space,
|
|
||||||
Typography,
|
|
||||||
} from "antd";
|
|
||||||
import { useQueryClient } from "@tanstack/react-query";
|
import { useQueryClient } from "@tanstack/react-query";
|
||||||
import { useCallback, useEffect, useMemo, useState } from "react";
|
import { useCallback, useEffect, useMemo, useState } from "react";
|
||||||
import { useNavigate } from "react-router-dom";
|
import { useNavigate } from "react-router-dom";
|
||||||
import type {
|
import type {
|
||||||
AdminTemplateContent,
|
AdminTemplateContent,
|
||||||
RuntimeWorkspace,
|
RuntimeWorkspace,
|
||||||
TemplateAdminField,
|
|
||||||
TemplateAdminSection,
|
TemplateAdminSection,
|
||||||
} from "@personal-brand/api-client";
|
} from "@personal-brand/api-client";
|
||||||
import type { BrandProfile } from "@personal-brand/api-client";
|
import type { BrandProfile } from "@personal-brand/api-client";
|
||||||
import { projectBrandAliases, projectTrustPresentation } from "@personal-brand/api-client";
|
|
||||||
import { TemplateListField } from "../../components/TemplateListField.js";
|
|
||||||
import { TemplateFieldRenderer } from "../../components/TemplateFieldRenderer.js";
|
|
||||||
import { MutationError } from "../../components/QueryState.js";
|
|
||||||
import { StatusTag } from "../../components/StatusTag.js";
|
import { StatusTag } from "../../components/StatusTag.js";
|
||||||
import { RecordContentEditor } from "../../components/RecordContentEditor.js";
|
import { RecordContentEditor } from "../../components/RecordContentEditor.js";
|
||||||
import { TemplatePreview } from "./TemplatePreview.js";
|
import { TemplatePreview } from "./TemplatePreview.js";
|
||||||
@@ -37,7 +20,6 @@ import {
|
|||||||
} from "./preview-selection.js";
|
} from "./preview-selection.js";
|
||||||
import {
|
import {
|
||||||
filterSectionsForPreviewTarget,
|
filterSectionsForPreviewTarget,
|
||||||
presentationFieldName,
|
|
||||||
type PresentationValues,
|
type PresentationValues,
|
||||||
} from "../../lib/presentation/presentation-bindings.js";
|
} from "../../lib/presentation/presentation-bindings.js";
|
||||||
import "./PresentationEditor.css";
|
import "./PresentationEditor.css";
|
||||||
@@ -50,6 +32,10 @@ import {
|
|||||||
useBrandPreviewDraft,
|
useBrandPreviewDraft,
|
||||||
} from "./presentation-editor-state.js";
|
} from "./presentation-editor-state.js";
|
||||||
import { useRecordPreview } from "./presentation-editor-record-preview.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;
|
||||||
@@ -61,73 +47,6 @@ interface PresentationEditorProps {
|
|||||||
initialSelection?: PreviewSelection;
|
initialSelection?: PreviewSelection;
|
||||||
}
|
}
|
||||||
|
|
||||||
function trustSection(
|
|
||||||
declaration: NonNullable<AdminTemplateContent["trustContentTypes"]>[number],
|
|
||||||
page: string,
|
|
||||||
): TemplateAdminSection {
|
|
||||||
const key = `trust:${declaration.type}`;
|
|
||||||
return {
|
|
||||||
key,
|
|
||||||
title: declaration.label,
|
|
||||||
description: declaration.help ?? "编辑当前展示内容。",
|
|
||||||
page,
|
|
||||||
target: "trust",
|
|
||||||
previewTargets: [
|
|
||||||
{ key, label: declaration.label },
|
|
||||||
...(declaration.previewTargets ?? []).filter(
|
|
||||||
(target) => !target.page || target.page === page,
|
|
||||||
),
|
|
||||||
],
|
|
||||||
fields: [
|
|
||||||
{
|
|
||||||
key: "title",
|
|
||||||
binding: "trust.title",
|
|
||||||
label: declaration.adminFields.title,
|
|
||||||
input: "text" as const,
|
|
||||||
required: true,
|
|
||||||
},
|
|
||||||
...(["summary", "content"] as const)
|
|
||||||
.filter((field) => !declaration.hideStandardFields?.includes(field))
|
|
||||||
.map((field) => ({
|
|
||||||
key: field,
|
|
||||||
binding: `trust.${field}`,
|
|
||||||
label: declaration.adminFields[field],
|
|
||||||
input: "textarea" as const,
|
|
||||||
previewTarget: `trust:${declaration.type}`,
|
|
||||||
...(field === "summary" && declaration.adminFields.summaryHelp
|
|
||||||
? { help: declaration.adminFields.summaryHelp }
|
|
||||||
: {}),
|
|
||||||
...(field === "content" && declaration.required ? { required: true } : {}),
|
|
||||||
})),
|
|
||||||
...(declaration.configFields ?? [])
|
|
||||||
.filter((field) => {
|
|
||||||
const target = declaration.previewTargets?.find(
|
|
||||||
(target) => target.key === field.previewTarget,
|
|
||||||
);
|
|
||||||
return !target?.page || target.page === page;
|
|
||||||
})
|
|
||||||
.map((field) => ({
|
|
||||||
...field,
|
|
||||||
previewTarget: field.previewTarget ?? `trust:${declaration.type}`,
|
|
||||||
})),
|
|
||||||
].map((field) => ({
|
|
||||||
...field,
|
|
||||||
previewTarget: (field as TemplateAdminField).previewTarget ?? `trust:${declaration.type}`,
|
|
||||||
})),
|
|
||||||
};
|
|
||||||
}
|
|
||||||
|
|
||||||
function previewTargetSource(
|
|
||||||
section: TemplateAdminSection,
|
|
||||||
targetKey: string,
|
|
||||||
): "brand" | "offering" | "trust" | "inquiry" {
|
|
||||||
const fields = section.fields.filter((field) => field.previewTarget === targetKey);
|
|
||||||
if (fields.some((field) => field.binding.startsWith("offering."))) return "offering";
|
|
||||||
if (fields.some((field) => field.binding.startsWith("trust."))) return "trust";
|
|
||||||
if (fields.some((field) => field.binding.startsWith("brand."))) return "brand";
|
|
||||||
return section.target;
|
|
||||||
}
|
|
||||||
|
|
||||||
export function PresentationEditor({
|
export function PresentationEditor({
|
||||||
templateContent,
|
templateContent,
|
||||||
previewWorkspace,
|
previewWorkspace,
|
||||||
@@ -144,11 +63,7 @@ export function PresentationEditor({
|
|||||||
const [selection, setSelection] = useState<PreviewSelection | undefined>(initialSelection);
|
const [selection, setSelection] = useState<PreviewSelection | undefined>(initialSelection);
|
||||||
const [hasInitializedSelection, setHasInitializedSelection] = useState(Boolean(initialSelection));
|
const [hasInitializedSelection, setHasInitializedSelection] = useState(Boolean(initialSelection));
|
||||||
const [detailOfferingId, setDetailOfferingId] = useState<string>();
|
const [detailOfferingId, setDetailOfferingId] = useState<string>();
|
||||||
const [recordDraft, setRecordDraft] = useState<{
|
const [recordDraft, setRecordDraft] = useState<RecordDraft>();
|
||||||
source: "offering" | "trust";
|
|
||||||
id: string;
|
|
||||||
values: PresentationValues;
|
|
||||||
}>();
|
|
||||||
const [inquiryDraft, setInquiryDraft] =
|
const [inquiryDraft, setInquiryDraft] =
|
||||||
useState<RuntimeWorkspace["inquiryForms"][number]["fields"]>();
|
useState<RuntimeWorkspace["inquiryForms"][number]["fields"]>();
|
||||||
const [brandDirty, setBrandDirty] = useState(false);
|
const [brandDirty, setBrandDirty] = useState(false);
|
||||||
@@ -367,113 +282,17 @@ export function PresentationEditor({
|
|||||||
});
|
});
|
||||||
const recordPreview = useRecordPreview({ recordDraft, selectedRecord, recordSection });
|
const recordPreview = useRecordPreview({ recordDraft, selectedRecord, recordSection });
|
||||||
useEffect(() => resetPreview(), [config, initialValues, resetPreview]);
|
useEffect(() => resetPreview(), [config, initialValues, resetPreview]);
|
||||||
const previewConfig = previewBrand.config;
|
const draftWorkspace = useDraftWorkspace({
|
||||||
const draftWorkspace = useMemo(() => {
|
|
||||||
if (!previewWorkspace) return undefined;
|
|
||||||
const next: RuntimeWorkspace = {
|
|
||||||
...previewWorkspace,
|
|
||||||
templateData: {
|
|
||||||
...previewWorkspace.templateData,
|
|
||||||
...(previewConfig.templateData &&
|
|
||||||
typeof previewConfig.templateData === "object" &&
|
|
||||||
!Array.isArray(previewConfig.templateData)
|
|
||||||
? (previewConfig.templateData as Record<string, unknown>)
|
|
||||||
: {}),
|
|
||||||
},
|
|
||||||
inquiryForms: previewWorkspace.inquiryForms.map((item) =>
|
|
||||||
item.key === inquiryRuntime?.key && inquiryDraft ? { ...item, fields: inquiryDraft } : item,
|
|
||||||
),
|
|
||||||
brand: projectBrandAliases(
|
|
||||||
{
|
|
||||||
...previewWorkspace.brand,
|
|
||||||
...(previewBrand as unknown as Record<string, unknown>),
|
|
||||||
config: previewConfig,
|
|
||||||
},
|
|
||||||
previewConfig,
|
|
||||||
),
|
|
||||||
};
|
|
||||||
if (!recordDraft || !selectedRecord || !recordSection || recordDraft.id !== selectedRecord.id)
|
|
||||||
return next;
|
|
||||||
const config = recordPreview?.config;
|
|
||||||
const standard = recordPreview?.standard;
|
|
||||||
if (!config || !standard) return next;
|
|
||||||
if (recordDraft.source === "trust") {
|
|
||||||
next.trustItems = next.trustItems.map((item) =>
|
|
||||||
item.id === recordDraft.id
|
|
||||||
? {
|
|
||||||
...item,
|
|
||||||
title: typeof standard.title === "string" ? standard.title : item.title,
|
|
||||||
summary: typeof standard.summary === "string" ? standard.summary : item.summary,
|
|
||||||
content: typeof standard.content === "string" ? standard.content : item.content,
|
|
||||||
config,
|
|
||||||
}
|
|
||||||
: item,
|
|
||||||
);
|
|
||||||
next.trustPresentation = projectTrustPresentation(
|
|
||||||
next.trustItems,
|
|
||||||
templateContent?.trustContentTypes ?? [],
|
|
||||||
);
|
|
||||||
} else {
|
|
||||||
next.offerings = next.offerings.map((item) =>
|
|
||||||
item.id === recordDraft.id
|
|
||||||
? {
|
|
||||||
...item,
|
|
||||||
name:
|
|
||||||
(recordDraft.values[presentationFieldName(recordSection.key, "name")] as string) ||
|
|
||||||
item.name,
|
|
||||||
summary:
|
|
||||||
(recordDraft.values[
|
|
||||||
presentationFieldName(recordSection.key, "summary")
|
|
||||||
] as string) || item.summary,
|
|
||||||
priceText:
|
|
||||||
(recordDraft.values[
|
|
||||||
presentationFieldName(recordSection.key, "priceText")
|
|
||||||
] as string) || null,
|
|
||||||
coverUrl:
|
|
||||||
(recordDraft.values[
|
|
||||||
presentationFieldName(recordSection.key, "coverUrl")
|
|
||||||
] as string) || null,
|
|
||||||
presentation: {
|
|
||||||
...item.presentation,
|
|
||||||
...config,
|
|
||||||
...(config.presentation && typeof config.presentation === "object"
|
|
||||||
? (config.presentation as Record<string, unknown>)
|
|
||||||
: {}),
|
|
||||||
...(typeof config.area === "string" ? { area: config.area } : {}),
|
|
||||||
...(typeof config.series === "string" ? { series: config.series } : {}),
|
|
||||||
...(typeof config.spaceType === "string" ? { spaceType: config.spaceType } : {}),
|
|
||||||
heroImage:
|
|
||||||
typeof config.detailHero === "string"
|
|
||||||
? config.detailHero
|
|
||||||
: typeof standard.coverUrl === "string"
|
|
||||||
? standard.coverUrl
|
|
||||||
: null,
|
|
||||||
details: {
|
|
||||||
...item.presentation.details,
|
|
||||||
...(config.details &&
|
|
||||||
typeof config.details === "object" &&
|
|
||||||
"background" in config.details
|
|
||||||
? (config.details as RuntimeWorkspace["offerings"][number]["presentation"]["details"])
|
|
||||||
: {}),
|
|
||||||
},
|
|
||||||
},
|
|
||||||
}
|
|
||||||
: item,
|
|
||||||
);
|
|
||||||
}
|
|
||||||
return next;
|
|
||||||
}, [
|
|
||||||
previewWorkspace,
|
previewWorkspace,
|
||||||
previewBrand,
|
previewBrand,
|
||||||
previewConfig,
|
inquiryRuntime,
|
||||||
|
inquiryDraft,
|
||||||
recordDraft,
|
recordDraft,
|
||||||
selectedRecord,
|
selectedRecord,
|
||||||
recordSection,
|
recordSection,
|
||||||
recordPreview,
|
recordPreview,
|
||||||
templateContent,
|
templateContent,
|
||||||
inquiryDraft,
|
});
|
||||||
inquiryRuntime,
|
|
||||||
]);
|
|
||||||
const selectedDetailOfferingId =
|
const selectedDetailOfferingId =
|
||||||
detailOfferingId && draftWorkspace?.offerings.some((item) => item.id === detailOfferingId)
|
detailOfferingId && draftWorkspace?.offerings.some((item) => item.id === detailOfferingId)
|
||||||
? detailOfferingId
|
? detailOfferingId
|
||||||
@@ -790,110 +609,19 @@ export function PresentationEditor({
|
|||||||
</p>
|
</p>
|
||||||
</div>
|
</div>
|
||||||
) : (
|
) : (
|
||||||
<Form
|
<PresentationFormPanel
|
||||||
form={form}
|
form={form}
|
||||||
layout="vertical"
|
|
||||||
initialValues={initialValues}
|
initialValues={initialValues}
|
||||||
|
editableSections={editableSections}
|
||||||
|
relevantPageLinks={relevantPageLinks}
|
||||||
|
selectedTarget={selectedTarget}
|
||||||
|
save={{
|
||||||
|
error: save.error,
|
||||||
|
isPending: save.isPending,
|
||||||
|
mutate: (values) => save.mutate(values),
|
||||||
|
}}
|
||||||
onValuesChange={handleValuesChange}
|
onValuesChange={handleValuesChange}
|
||||||
onFinish={() => save.mutate(form.getFieldsValue(true) as PresentationValues)}
|
/>
|
||||||
className="presentation-editor__form"
|
|
||||||
>
|
|
||||||
<div className="presentation-editor__form-body">
|
|
||||||
{editableSections.map((section, index) => (
|
|
||||||
<section key={section.key} aria-labelledby={`section-${section.key}`}>
|
|
||||||
{selectedTarget && index > 0 ? <Divider /> : null}
|
|
||||||
<h4 id={`section-${section.key}`} className="admin-subhead">
|
|
||||||
{selectedTarget
|
|
||||||
? fieldTargetTitle(section, selectedTarget.label)
|
|
||||||
: section.title}
|
|
||||||
</h4>
|
|
||||||
{!selectedTarget ? (
|
|
||||||
<p className="admin-section-description admin-offset-bottom">
|
|
||||||
{section.description}
|
|
||||||
</p>
|
|
||||||
) : null}
|
|
||||||
<Row gutter={16}>
|
|
||||||
{section.fields.map((field) => (
|
|
||||||
<Col key={field.key} xs={24} md={24}>
|
|
||||||
{field.input === "list" ? (
|
|
||||||
<TemplateListField
|
|
||||||
field={field}
|
|
||||||
name={presentationFieldName(section.key, field.key)}
|
|
||||||
/>
|
|
||||||
) : (
|
|
||||||
<Form.Item
|
|
||||||
name={presentationFieldName(section.key, field.key)}
|
|
||||||
label={field.label}
|
|
||||||
{...(field.help ? { help: field.help } : {})}
|
|
||||||
{...(field.required
|
|
||||||
? {
|
|
||||||
rules: [
|
|
||||||
{ required: true, message: `请填写${field.label}` },
|
|
||||||
],
|
|
||||||
}
|
|
||||||
: {})}
|
|
||||||
>
|
|
||||||
<TemplateFieldRenderer field={field} />
|
|
||||||
</Form.Item>
|
|
||||||
)}
|
|
||||||
</Col>
|
|
||||||
))}
|
|
||||||
</Row>
|
|
||||||
</section>
|
|
||||||
))}
|
|
||||||
{relevantPageLinks.length ? (
|
|
||||||
<Alert
|
|
||||||
type="info"
|
|
||||||
showIcon
|
|
||||||
className="admin-offset-bottom-lg"
|
|
||||||
message="这个页面还有关联内容"
|
|
||||||
description={
|
|
||||||
<Space wrap>
|
|
||||||
{relevantPageLinks.map((item) => (
|
|
||||||
<Button
|
|
||||||
key={item.target}
|
|
||||||
type="link"
|
|
||||||
onClick={() => {
|
|
||||||
if (item.target.startsWith("trust:")) {
|
|
||||||
void navigate(
|
|
||||||
`/admin/trust?type=${encodeURIComponent(item.target.slice(6))}`,
|
|
||||||
);
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
void navigate(
|
|
||||||
item.target === "offering"
|
|
||||||
? "/admin/services"
|
|
||||||
: "/admin/workspace",
|
|
||||||
);
|
|
||||||
}}
|
|
||||||
className="admin-link-flush"
|
|
||||||
>
|
|
||||||
{item.label}
|
|
||||||
</Button>
|
|
||||||
))}
|
|
||||||
</Space>
|
|
||||||
}
|
|
||||||
/>
|
|
||||||
) : null}
|
|
||||||
{!editableSections.some((section) => section.fields.length) &&
|
|
||||||
!relevantPageLinks.length ? (
|
|
||||||
<p className="admin-section-description">这个页面没有单独的品牌文案配置。</p>
|
|
||||||
) : null}
|
|
||||||
{selectedTarget &&
|
|
||||||
!editableSections.some((section) => section.fields.length) &&
|
|
||||||
relevantPageLinks.length ? (
|
|
||||||
<p className="admin-section-description">
|
|
||||||
此区域内容由对应的业务管理页面维护。
|
|
||||||
</p>
|
|
||||||
) : null}
|
|
||||||
</div>
|
|
||||||
<div className="presentation-editor__form-actions admin-actions admin-offset-top-lg">
|
|
||||||
<MutationError error={save.error} />
|
|
||||||
<Button type="primary" htmlType="submit" loading={save.isPending}>
|
|
||||||
保存页面内容
|
|
||||||
</Button>
|
|
||||||
</div>
|
|
||||||
</Form>
|
|
||||||
)}
|
)}
|
||||||
</div>
|
</div>
|
||||||
</div>
|
</div>
|
||||||
@@ -902,24 +630,3 @@ export function PresentationEditor({
|
|||||||
</section>
|
</section>
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
|
||||||
function pageTitle(page: string): string {
|
|
||||||
return (
|
|
||||||
(
|
|
||||||
{
|
|
||||||
home: "首页",
|
|
||||||
offerings: "业务展示",
|
|
||||||
service: "服务说明",
|
|
||||||
detail: "详情页",
|
|
||||||
inquiry: "咨询页",
|
|
||||||
success: "提交成功页",
|
|
||||||
records: "记录页",
|
|
||||||
profile: "主理人页",
|
|
||||||
} as Record<string, string>
|
|
||||||
)[page] ?? page
|
|
||||||
);
|
|
||||||
}
|
|
||||||
|
|
||||||
function fieldTargetTitle(section: { title: string }, targetLabel: string): string {
|
|
||||||
return section.title === targetLabel ? targetLabel : `${targetLabel} · ${section.title}`;
|
|
||||||
}
|
|
||||||
@@ -0,0 +1,134 @@
|
|||||||
|
import { Alert, Button, Col, Divider, Form, Row, Space } from "antd";
|
||||||
|
import type { FormInstance } from "antd";
|
||||||
|
import { useNavigate } from "react-router-dom";
|
||||||
|
import type { TemplateAdminSection } from "@personal-brand/api-client";
|
||||||
|
import { TemplateFieldRenderer } from "../../components/TemplateFieldRenderer.js";
|
||||||
|
import { TemplateListField } from "../../components/TemplateListField.js";
|
||||||
|
import { MutationError } from "../../components/QueryState.js";
|
||||||
|
import {
|
||||||
|
presentationFieldName,
|
||||||
|
type PresentationValues,
|
||||||
|
} from "../../lib/presentation/presentation-bindings.js";
|
||||||
|
import { fieldTargetTitle } from "./presentation-editor-helpers.js";
|
||||||
|
import type { PreviewTarget } from "./preview-selection.js";
|
||||||
|
|
||||||
|
interface PresentationFormPanelProps {
|
||||||
|
form: FormInstance<PresentationValues>;
|
||||||
|
initialValues: PresentationValues;
|
||||||
|
editableSections: TemplateAdminSection[];
|
||||||
|
relevantPageLinks: { target: string; label: string | undefined }[];
|
||||||
|
selectedTarget: PreviewTarget | undefined;
|
||||||
|
save: {
|
||||||
|
error: unknown;
|
||||||
|
isPending: boolean;
|
||||||
|
mutate: (values: PresentationValues) => void;
|
||||||
|
};
|
||||||
|
onValuesChange: (changed: PresentationValues, allValues: PresentationValues) => void;
|
||||||
|
}
|
||||||
|
|
||||||
|
export function PresentationFormPanel({
|
||||||
|
form,
|
||||||
|
initialValues,
|
||||||
|
editableSections,
|
||||||
|
relevantPageLinks,
|
||||||
|
selectedTarget,
|
||||||
|
save,
|
||||||
|
onValuesChange,
|
||||||
|
}: PresentationFormPanelProps) {
|
||||||
|
const navigate = useNavigate();
|
||||||
|
const hasEditableFields = editableSections.some((section) => section.fields.length);
|
||||||
|
|
||||||
|
return (
|
||||||
|
<Form
|
||||||
|
form={form}
|
||||||
|
layout="vertical"
|
||||||
|
initialValues={initialValues}
|
||||||
|
onValuesChange={onValuesChange}
|
||||||
|
onFinish={() => save.mutate(form.getFieldsValue(true) as PresentationValues)}
|
||||||
|
className="presentation-editor__form"
|
||||||
|
>
|
||||||
|
<div className="presentation-editor__form-body">
|
||||||
|
{editableSections.map((section, index) => (
|
||||||
|
<section key={section.key} aria-labelledby={`section-${section.key}`}>
|
||||||
|
{selectedTarget && index > 0 ? <Divider /> : null}
|
||||||
|
<h4 id={`section-${section.key}`} className="admin-subhead">
|
||||||
|
{selectedTarget ? fieldTargetTitle(section, selectedTarget.label) : section.title}
|
||||||
|
</h4>
|
||||||
|
{!selectedTarget ? (
|
||||||
|
<p className="admin-section-description admin-offset-bottom">{section.description}</p>
|
||||||
|
) : null}
|
||||||
|
<Row gutter={16}>
|
||||||
|
{section.fields.map((field) => (
|
||||||
|
<Col key={field.key} xs={24} md={24}>
|
||||||
|
{field.input === "list" ? (
|
||||||
|
<TemplateListField
|
||||||
|
field={field}
|
||||||
|
name={presentationFieldName(section.key, field.key)}
|
||||||
|
/>
|
||||||
|
) : (
|
||||||
|
<Form.Item
|
||||||
|
name={presentationFieldName(section.key, field.key)}
|
||||||
|
label={field.label}
|
||||||
|
{...(field.help ? { help: field.help } : {})}
|
||||||
|
{...(field.required
|
||||||
|
? {
|
||||||
|
rules: [{ required: true, message: `请填写${field.label}` }],
|
||||||
|
}
|
||||||
|
: {})}
|
||||||
|
>
|
||||||
|
<TemplateFieldRenderer field={field} />
|
||||||
|
</Form.Item>
|
||||||
|
)}
|
||||||
|
</Col>
|
||||||
|
))}
|
||||||
|
</Row>
|
||||||
|
</section>
|
||||||
|
))}
|
||||||
|
{relevantPageLinks.length ? (
|
||||||
|
<Alert
|
||||||
|
type="info"
|
||||||
|
showIcon
|
||||||
|
className="admin-offset-bottom-lg"
|
||||||
|
message="这个页面还有关联内容"
|
||||||
|
description={
|
||||||
|
<Space wrap>
|
||||||
|
{relevantPageLinks.map((item) => (
|
||||||
|
<Button
|
||||||
|
key={item.target}
|
||||||
|
type="link"
|
||||||
|
onClick={() => {
|
||||||
|
if (item.target.startsWith("trust:")) {
|
||||||
|
void navigate(
|
||||||
|
`/admin/trust?type=${encodeURIComponent(item.target.slice(6))}`,
|
||||||
|
);
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
void navigate(
|
||||||
|
item.target === "offering" ? "/admin/services" : "/admin/workspace",
|
||||||
|
);
|
||||||
|
}}
|
||||||
|
className="admin-link-flush"
|
||||||
|
>
|
||||||
|
{item.label}
|
||||||
|
</Button>
|
||||||
|
))}
|
||||||
|
</Space>
|
||||||
|
}
|
||||||
|
/>
|
||||||
|
) : null}
|
||||||
|
{!hasEditableFields && !relevantPageLinks.length ? (
|
||||||
|
<p className="admin-section-description">这个页面没有单独的品牌文案配置。</p>
|
||||||
|
) : null}
|
||||||
|
{selectedTarget && !hasEditableFields && relevantPageLinks.length ? (
|
||||||
|
<p className="admin-section-description">此区域内容由对应的业务管理页面维护。</p>
|
||||||
|
) : null}
|
||||||
|
</div>
|
||||||
|
<div className="presentation-editor__form-actions admin-actions admin-offset-top-lg">
|
||||||
|
<MutationError error={save.error} />
|
||||||
|
<Button type="primary" htmlType="submit" loading={save.isPending}>
|
||||||
|
保存页面内容
|
||||||
|
</Button>
|
||||||
|
</div>
|
||||||
|
</Form>
|
||||||
|
);
|
||||||
|
}
|
||||||
@@ -0,0 +1,93 @@
|
|||||||
|
import type {
|
||||||
|
AdminTemplateContent,
|
||||||
|
TemplateAdminField,
|
||||||
|
TemplateAdminSection,
|
||||||
|
} from "@personal-brand/api-client";
|
||||||
|
|
||||||
|
export function trustSection(
|
||||||
|
declaration: NonNullable<AdminTemplateContent["trustContentTypes"]>[number],
|
||||||
|
page: string,
|
||||||
|
): TemplateAdminSection {
|
||||||
|
const key = `trust:${declaration.type}`;
|
||||||
|
return {
|
||||||
|
key,
|
||||||
|
title: declaration.label,
|
||||||
|
description: declaration.help ?? "编辑当前展示内容。",
|
||||||
|
page,
|
||||||
|
target: "trust",
|
||||||
|
previewTargets: [
|
||||||
|
{ key, label: declaration.label },
|
||||||
|
...(declaration.previewTargets ?? []).filter(
|
||||||
|
(target) => !target.page || target.page === page,
|
||||||
|
),
|
||||||
|
],
|
||||||
|
fields: [
|
||||||
|
{
|
||||||
|
key: "title",
|
||||||
|
binding: "trust.title",
|
||||||
|
label: declaration.adminFields.title,
|
||||||
|
input: "text" as const,
|
||||||
|
required: true,
|
||||||
|
},
|
||||||
|
...(["summary", "content"] as const)
|
||||||
|
.filter((field) => !declaration.hideStandardFields?.includes(field))
|
||||||
|
.map((field) => ({
|
||||||
|
key: field,
|
||||||
|
binding: `trust.${field}`,
|
||||||
|
label: declaration.adminFields[field],
|
||||||
|
input: "textarea" as const,
|
||||||
|
previewTarget: `trust:${declaration.type}`,
|
||||||
|
...(field === "summary" && declaration.adminFields.summaryHelp
|
||||||
|
? { help: declaration.adminFields.summaryHelp }
|
||||||
|
: {}),
|
||||||
|
...(field === "content" && declaration.required ? { required: true } : {}),
|
||||||
|
})),
|
||||||
|
...(declaration.configFields ?? [])
|
||||||
|
.filter((field) => {
|
||||||
|
const target = declaration.previewTargets?.find(
|
||||||
|
(target) => target.key === field.previewTarget,
|
||||||
|
);
|
||||||
|
return !target?.page || target.page === page;
|
||||||
|
})
|
||||||
|
.map((field) => ({
|
||||||
|
...field,
|
||||||
|
previewTarget: field.previewTarget ?? `trust:${declaration.type}`,
|
||||||
|
})),
|
||||||
|
].map((field) => ({
|
||||||
|
...field,
|
||||||
|
previewTarget: (field as TemplateAdminField).previewTarget ?? `trust:${declaration.type}`,
|
||||||
|
})),
|
||||||
|
};
|
||||||
|
}
|
||||||
|
|
||||||
|
export function previewTargetSource(
|
||||||
|
section: TemplateAdminSection,
|
||||||
|
targetKey: string,
|
||||||
|
): "brand" | "offering" | "trust" | "inquiry" {
|
||||||
|
const fields = section.fields.filter((field) => field.previewTarget === targetKey);
|
||||||
|
if (fields.some((field) => field.binding.startsWith("offering."))) return "offering";
|
||||||
|
if (fields.some((field) => field.binding.startsWith("trust."))) return "trust";
|
||||||
|
if (fields.some((field) => field.binding.startsWith("brand."))) return "brand";
|
||||||
|
return section.target;
|
||||||
|
}
|
||||||
|
|
||||||
|
export function pageTitle(page: string): string {
|
||||||
|
return (
|
||||||
|
(
|
||||||
|
{
|
||||||
|
home: "首页",
|
||||||
|
offerings: "业务展示",
|
||||||
|
service: "服务说明",
|
||||||
|
detail: "详情页",
|
||||||
|
inquiry: "咨询页",
|
||||||
|
success: "提交成功页",
|
||||||
|
records: "记录页",
|
||||||
|
profile: "主理人页",
|
||||||
|
} as Record<string, string>
|
||||||
|
)[page] ?? page
|
||||||
|
);
|
||||||
|
}
|
||||||
|
|
||||||
|
export function fieldTargetTitle(section: { title: string }, targetLabel: string): string {
|
||||||
|
return section.title === targetLabel ? targetLabel : `${targetLabel} · ${section.title}`;
|
||||||
|
}
|
||||||
@@ -1,6 +1,5 @@
|
|||||||
import { useMemo } from "react";
|
import { useMemo } from "react";
|
||||||
import type { Offering, TrustContent } from "@personal-brand/api-client";
|
import type { Offering, TemplateAdminSection, TrustContent } from "@personal-brand/api-client";
|
||||||
import type { TemplateAdminSection } from "@personal-brand/api-client";
|
|
||||||
import {
|
import {
|
||||||
applyPresentationValues,
|
applyPresentationValues,
|
||||||
applyStandardEntityValues,
|
applyStandardEntityValues,
|
||||||
|
|||||||
@@ -0,0 +1,128 @@
|
|||||||
|
import { useMemo } from "react";
|
||||||
|
import type {
|
||||||
|
AdminTemplateContent,
|
||||||
|
BrandProfile,
|
||||||
|
Offering,
|
||||||
|
RuntimeWorkspace,
|
||||||
|
TemplateAdminSection,
|
||||||
|
TrustContent,
|
||||||
|
} from "@personal-brand/api-client";
|
||||||
|
import { projectBrandAliases, projectTrustPresentation } from "@personal-brand/api-client";
|
||||||
|
import {
|
||||||
|
presentationFieldName,
|
||||||
|
type PresentationValues,
|
||||||
|
} from "../../lib/presentation/presentation-bindings.js";
|
||||||
|
import type { RecordDraft } from "./presentation-editor-record-preview.js";
|
||||||
|
|
||||||
|
interface DraftWorkspaceInput {
|
||||||
|
previewWorkspace: RuntimeWorkspace | undefined;
|
||||||
|
previewBrand: BrandProfile;
|
||||||
|
inquiryRuntime: RuntimeWorkspace["inquiryForms"][number] | undefined;
|
||||||
|
inquiryDraft: RuntimeWorkspace["inquiryForms"][number]["fields"] | undefined;
|
||||||
|
recordDraft: RecordDraft | undefined;
|
||||||
|
selectedRecord: Offering | TrustContent | undefined;
|
||||||
|
recordSection: TemplateAdminSection | undefined;
|
||||||
|
recordPreview: { config: Record<string, unknown>; standard: Record<string, unknown> } | undefined;
|
||||||
|
templateContent: AdminTemplateContent | null | undefined;
|
||||||
|
}
|
||||||
|
|
||||||
|
export function useDraftWorkspace(input: DraftWorkspaceInput): RuntimeWorkspace | undefined {
|
||||||
|
const { previewWorkspace, previewBrand, inquiryRuntime, inquiryDraft } = input;
|
||||||
|
return useMemo(() => {
|
||||||
|
if (!previewWorkspace) return undefined;
|
||||||
|
const previewConfig = previewBrand.config ?? {};
|
||||||
|
const next: RuntimeWorkspace = {
|
||||||
|
...previewWorkspace,
|
||||||
|
templateData: {
|
||||||
|
...previewWorkspace.templateData,
|
||||||
|
...(previewConfig.templateData &&
|
||||||
|
typeof previewConfig.templateData === "object" &&
|
||||||
|
!Array.isArray(previewConfig.templateData)
|
||||||
|
? (previewConfig.templateData as Record<string, unknown>)
|
||||||
|
: {}),
|
||||||
|
},
|
||||||
|
inquiryForms: previewWorkspace.inquiryForms.map((item) =>
|
||||||
|
item.key === inquiryRuntime?.key && inquiryDraft ? { ...item, fields: inquiryDraft } : item,
|
||||||
|
),
|
||||||
|
brand: projectBrandAliases(
|
||||||
|
{
|
||||||
|
...previewWorkspace.brand,
|
||||||
|
...(previewBrand as unknown as Record<string, unknown>),
|
||||||
|
config: previewConfig,
|
||||||
|
},
|
||||||
|
previewConfig,
|
||||||
|
),
|
||||||
|
};
|
||||||
|
const { recordDraft, selectedRecord, recordSection, recordPreview } = input;
|
||||||
|
if (!recordDraft || !selectedRecord || !recordSection || recordDraft.id !== selectedRecord.id)
|
||||||
|
return next;
|
||||||
|
const config = recordPreview?.config;
|
||||||
|
const standard = recordPreview?.standard;
|
||||||
|
if (!config || !standard) return next;
|
||||||
|
if (recordDraft.source === "trust") {
|
||||||
|
next.trustItems = next.trustItems.map((item) =>
|
||||||
|
item.id === recordDraft.id
|
||||||
|
? {
|
||||||
|
...item,
|
||||||
|
title: typeof standard.title === "string" ? standard.title : item.title,
|
||||||
|
summary: typeof standard.summary === "string" ? standard.summary : item.summary,
|
||||||
|
content: typeof standard.content === "string" ? standard.content : item.content,
|
||||||
|
config,
|
||||||
|
}
|
||||||
|
: item,
|
||||||
|
);
|
||||||
|
next.trustPresentation = projectTrustPresentation(
|
||||||
|
next.trustItems,
|
||||||
|
input.templateContent?.trustContentTypes ?? [],
|
||||||
|
);
|
||||||
|
} else {
|
||||||
|
next.offerings = next.offerings.map((item) =>
|
||||||
|
item.id === recordDraft.id
|
||||||
|
? {
|
||||||
|
...item,
|
||||||
|
name:
|
||||||
|
(recordDraft.values[presentationFieldName(recordSection.key, "name")] as string) ||
|
||||||
|
item.name,
|
||||||
|
summary:
|
||||||
|
(recordDraft.values[
|
||||||
|
presentationFieldName(recordSection.key, "summary")
|
||||||
|
] as string) || item.summary,
|
||||||
|
priceText:
|
||||||
|
(recordDraft.values[
|
||||||
|
presentationFieldName(recordSection.key, "priceText")
|
||||||
|
] as string) || null,
|
||||||
|
coverUrl:
|
||||||
|
(recordDraft.values[
|
||||||
|
presentationFieldName(recordSection.key, "coverUrl")
|
||||||
|
] as string) || null,
|
||||||
|
presentation: {
|
||||||
|
...item.presentation,
|
||||||
|
...config,
|
||||||
|
...(config.presentation && typeof config.presentation === "object"
|
||||||
|
? (config.presentation as Record<string, unknown>)
|
||||||
|
: {}),
|
||||||
|
...(typeof config.area === "string" ? { area: config.area } : {}),
|
||||||
|
...(typeof config.series === "string" ? { series: config.series } : {}),
|
||||||
|
...(typeof config.spaceType === "string" ? { spaceType: config.spaceType } : {}),
|
||||||
|
heroImage:
|
||||||
|
typeof config.detailHero === "string"
|
||||||
|
? config.detailHero
|
||||||
|
: typeof standard.coverUrl === "string"
|
||||||
|
? standard.coverUrl
|
||||||
|
: null,
|
||||||
|
details: {
|
||||||
|
...item.presentation.details,
|
||||||
|
...(config.details &&
|
||||||
|
typeof config.details === "object" &&
|
||||||
|
"background" in config.details
|
||||||
|
? (config.details as RuntimeWorkspace["offerings"][number]["presentation"]["details"])
|
||||||
|
: {}),
|
||||||
|
},
|
||||||
|
},
|
||||||
|
}
|
||||||
|
: item,
|
||||||
|
);
|
||||||
|
}
|
||||||
|
return next;
|
||||||
|
}, [input, previewWorkspace, previewBrand, inquiryRuntime, inquiryDraft]);
|
||||||
|
}
|
||||||
Reference in new issue
Block a user