refactor: 抽取业务与信任内容公共编辑器

This commit is contained in:
que01 committed 2026-10-10 17:03:41 +08:00
1 parent d2ce2d2a84
commit d0ef25cfb3
3 files changed
+221 -137

No files matched your search

@@ -23,8 +23,6 @@ import type {
RuntimeWorkspace,
TemplateAdminField,
TemplateAdminSection,
Offering,
TrustContent,
} from "@personal-brand/api-client";
import type { BrandProfile } from "@personal-brand/api-client";
import { projectBrandAliases, projectTrustPresentation } from "@personal-brand/api-client";
@@ -32,6 +30,7 @@ import { createApiClient } from "../../lib/api.js";
import { ImageUrlField } from "../../components/ImageUrlField.js";
import { TemplateListField } from "../../components/TemplateListField.js";
import { MutationError } from "../../components/QueryState.js";
import { RecordContentEditor } from "../../components/RecordContentEditor.js";
import { TemplatePreview } from "./TemplatePreview.js";
import { InquiryPreviewEditor } from "./InquiryPreviewEditor.js";
import {
@@ -809,11 +808,12 @@ export function PresentationEditor({
/>
) : selectedRecordSource && selection?.recordId && selectedSection ? (
selectedRecord ? (
<RecordEditor
<RecordContentEditor
key={`${selectedRecordSource}:${selectedRecord.id}:${selectedSection.key}`}
source={selectedRecordSource}
record={selectedRecord}
section={recordSection!}
context="visual"
onDraft={(values) =>
setRecordDraft({
source: selectedRecordSource,
@@ -1016,113 +1016,6 @@ export function PresentationEditor({
);
}
function RecordEditor({
source,
record,
section,
onDraft,
onSaved,
}: {
source: "offering" | "trust";
record: Offering | TrustContent;
section: TemplateAdminSection;
onDraft: (values: PresentationValues) => void;
onSaved: () => void;
}) {
const [form] = Form.useForm<PresentationValues>();
const queryClient = useQueryClient();
const initialValues = useMemo(
() =>
Object.fromEntries(
section.fields.map((field) => [
presentationFieldName(section.key, field.key),
readPresentationValue(
record.config,
field.binding,
field,
record as unknown as Record<string, unknown>,
),
]),
),
[record, section],
);
useEffect(() => form.setFieldsValue(initialValues), [form, initialValues]);
const save = useMutation({
mutationFn: async (values: PresentationValues): Promise<void> => {
const config = applyPresentationValues(record.config, [section], values);
const standard = applyStandardEntityValues(
record as unknown as Record<string, unknown>,
source,
[section],
values,
);
const changed = Object.fromEntries(
Object.entries(standard).filter(
([key, value]) =>
key !== "config" && value !== (record as unknown as Record<string, unknown>)[key],
),
);
const client = createApiClient();
if (source === "offering")
await client.updateAdminOffering(record.id, { ...changed, config });
else await client.updateAdminTrustContent(record.id, { ...changed, config });
},
onSuccess: () => {
message.success("内容已保存为草稿,发布后访客才会看到更新");
onSaved();
void queryClient.invalidateQueries({
queryKey: [source === "offering" ? "admin-offerings" : "trust-content"],
});
void queryClient.invalidateQueries({ queryKey: ["admin-workspace"] });
void queryClient.invalidateQueries({ queryKey: ["admin-preview-workspace"] });
},
});
return (
<Form
form={form}
layout="vertical"
initialValues={initialValues}
onValuesChange={(_, values) => onDraft(values)}
onFinish={(values) => save.mutate(values)}
style={{ minHeight: 0, display: "flex", flexDirection: "column" }}
>
{save.isPending ? <Typography.Text type="secondary">正在保存内容…</Typography.Text> : null}
<Typography.Paragraph type="secondary">{section.description}</Typography.Paragraph>
<div style={{ flex: 1 }}>
<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}` }] }
: {})}
>
{renderField(field)}
</Form.Item>
)}
</Col>
))}
</Row>
</div>
<Space direction="vertical" style={{ width: "100%", marginTop: 24 }}>
<MutationError error={save.error} />
<Button type="primary" htmlType="submit" loading={save.isPending}>
保存此项内容
</Button>
</Space>
</Form>
);
}
function pageTitle(page: string): string {
return (
(
@@ -12,12 +12,16 @@ import {
message,
} from "antd";
import { useMutation, useQuery, useQueryClient } from "@tanstack/react-query";
import type { Offering, TemplateAdminField } from "@personal-brand/api-client";
import { useState } from "react";
import type {
Offering,
TemplateAdminField,
TemplateAdminSection,
} from "@personal-brand/api-client";
import { useMemo, useState } from "react";
import { createApiClient } from "../../lib/api.js";
import { AdminPageHeader } from "../../components/AdminPageHeader.js";
import { ImageUrlField } from "../../components/ImageUrlField.js";
import { TemplateListField } from "../../components/TemplateListField.js";
import { RecordContentFields } from "../../components/RecordContentEditor.js";
import {
applyPresentationValues,
presentationFieldName,
@@ -46,6 +50,17 @@ export function ServicesPage() {
.flatMap((section) =>
section.fields.filter((field) => field.binding.startsWith("offering.config.")),
);
const templateFieldSection = useMemo<TemplateAdminSection>(
() => ({
key: "offering",
title: "模板内容",
description: "",
page: "detail",
target: "offering",
fields: templateFields,
}),
[templateFields],
);
const offeringPageLabel =
workspace.data?.templateContent?.navigation.find((item) => item.page === "offerings")?.label ??
"业务展示页";
@@ -299,30 +314,14 @@ export function ServicesPage() {
]}
/>
</Form.Item>
{templateFields.map((field) =>
field.input === "list" ? (
<TemplateListField
key={field.key}
field={field}
name={presentationFieldName("offering", field.key)}
/>
) : (
<Form.Item
key={field.key}
name={presentationFieldName("offering", field.key)}
label={field.label}
help={field.help}
>
{field.input === "textarea" ? (
<Input.TextArea rows={3} />
) : field.input === "image" ? (
<ImageUrlField />
) : (
<Input />
)}
</Form.Item>
),
)}
{templateFields.length ? (
<>
<Typography.Paragraph type="secondary">
以下模板字段与小程序页面编辑的是同一份业务数据;可在此集中修改,也可到页面编辑中点选对应卡片。
</Typography.Paragraph>
<RecordContentFields section={templateFieldSection} />
</>
) : null}
</Form>
</Modal>
</QueryState>