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 (
(