diff --git a/apps/admin/src/components/RecordContentEditor.tsx b/apps/admin/src/components/RecordContentEditor.tsx index 66cfe8f..59dd01a 100644 --- a/apps/admin/src/components/RecordContentEditor.tsx +++ b/apps/admin/src/components/RecordContentEditor.tsx @@ -1,31 +1,14 @@ -import { Alert, Button, Col, Form, Row, Space, Typography, message } from "antd"; -import { useMutation, useQueryClient } from "@tanstack/react-query"; -import { useEffect, useMemo, useState } from "react"; +import { Alert, Button, Col, Form, Row, Space, Typography } from "antd"; import { useNavigate } from "react-router-dom"; -import type { Offering, TemplateAdminSection, TrustContent } from "@personal-brand/api-client"; -import { createApiClient } from "../lib/api.js"; +import type { TemplateAdminSection } from "@personal-brand/api-client"; import { TemplateListField } from "./TemplateListField.js"; import { TemplateFieldRenderer } from "./TemplateFieldRenderer.js"; import { MutationError } from "./QueryState.js"; -import { - applyPresentationValues, - applyStandardEntityValues, - presentationFieldName, - readPresentationValue, - type PresentationValues, -} from "../features/brand/presentation-bindings.js"; +import { presentationFieldName } from "../features/brand/presentation-bindings.js"; +import { useRecordContentEditorState } from "./record-content/useRecordContentEditorState.js"; +import type { RecordContentEditorProps, RecordContentSource } from "./record-content/types.js"; -export type RecordContentSource = "offering" | "trust"; - -export interface RecordContentEditorProps { - source: RecordContentSource; - record: Offering | TrustContent; - section: TemplateAdminSection; - context: "visual" | "services"; - onDraft?: (values: PresentationValues) => void; - onSaved?: () => void; - visualSelection?: { targetKey: string; page: string }; -} +export type { RecordContentEditorProps, RecordContentSource }; export function RecordContentFields({ section, @@ -72,95 +55,22 @@ export function RecordContentEditor({ visualSelection, }: RecordContentEditorProps) { const navigate = useNavigate(); - const [form] = Form.useForm(); - const queryClient = useQueryClient(); const recordName = "name" in record ? record.name : record.title; - const [draftValues, setDraftValues] = useState(() => - Object.fromEntries( - section.fields.map((field) => [ - presentationFieldName(section.key, field.key), - readPresentationValue( - record.config, - field.binding, - field, - record as unknown as Record, - ), - ]), - ), - ); - 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, - ), - ]), - ), - [record, section], - ); - useEffect(() => form.setFieldsValue(initialValues), [form, initialValues]); - const save = useMutation({ - mutationFn: async (values: PresentationValues): Promise => { - const config = applyPresentationValues(record.config, [section], values); - const standard = applyStandardEntityValues( - record as unknown as Record, - source, - [section], - values, - ); - const changed = Object.fromEntries( - Object.entries(standard).filter( - ([key, value]) => - key !== "config" && - (key in (record as unknown as Record) || - value !== (record as unknown as Record)[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"] }); - if (source === "offering") - void queryClient.invalidateQueries({ queryKey: ["admin-workspace-offering-fields"] }); - }, + const { form, handleValuesChange, initialValues, save, submit } = useRecordContentEditorState({ + source, + record, + section, + ...(onDraft ? { onDraft } : {}), + ...(onSaved ? { onSaved } : {}), }); + return (
{ - const currentValues = form.getFieldsValue(true) as PresentationValues; - const next = { - ...draftValues, - ...values, - ...currentValues, - }; - setDraftValues(next); - onDraft?.(next); - }} - onFinish={(values) => { - const currentValues = form.getFieldsValue(true) as PresentationValues; - save.mutate({ - ...draftValues, - ...values, - ...currentValues, - }); - }} + onValuesChange={handleValuesChange} + onFinish={submit} style={{ minHeight: 0, display: "flex", flexDirection: "column" }} > void; + onSaved?: () => void; + visualSelection?: { targetKey: string; page: string }; +} diff --git a/apps/admin/src/components/record-content/useRecordContentEditorState.ts b/apps/admin/src/components/record-content/useRecordContentEditorState.ts new file mode 100644 index 0000000..b90a141 --- /dev/null +++ b/apps/admin/src/components/record-content/useRecordContentEditorState.ts @@ -0,0 +1,117 @@ +import { Form, message } from "antd"; +import { useMutation, useQueryClient } from "@tanstack/react-query"; +import { useEffect, useMemo, useState } from "react"; +import type { TemplateAdminSection } from "@personal-brand/api-client"; +import type { Offering, TrustContent } from "@personal-brand/api-client"; +import { createApiClient } from "../../lib/api.js"; +import { + applyPresentationValues, + applyStandardEntityValues, + presentationFieldName, + readPresentationValue, + type PresentationValues, +} from "../../features/brand/presentation-bindings.js"; +import type { RecordContentSource } from "./types.js"; + +function createInitialValues( + record: Offering | TrustContent, + section: TemplateAdminSection, +): PresentationValues { + return Object.fromEntries( + section.fields.map((field) => [ + presentationFieldName(section.key, field.key), + readPresentationValue( + record.config, + field.binding, + field, + record as unknown as Record, + ), + ]), + ); +} + +function createChangedStandardValues( + record: Offering | TrustContent, + source: RecordContentSource, + section: TemplateAdminSection, + values: PresentationValues, +) { + const entity = record as unknown as Record; + const standard = applyStandardEntityValues(entity, source, [section], values); + + return Object.fromEntries( + Object.entries(standard).filter( + ([key, value]) => key !== "config" && (key in entity || value !== entity[key]), + ), + ); +} + +export function useRecordContentEditorState({ + source, + record, + section, + onDraft, + onSaved, +}: { + source: RecordContentSource; + record: Offering | TrustContent; + section: TemplateAdminSection; + onDraft?: ((values: PresentationValues) => void) | undefined; + onSaved?: (() => void) | undefined; +}) { + const [form] = Form.useForm(); + const queryClient = useQueryClient(); + const [draftValues, setDraftValues] = useState(() => + createInitialValues(record, section), + ); + const initialValues = useMemo(() => createInitialValues(record, section), [record, section]); + + useEffect(() => form.setFieldsValue(initialValues), [form, initialValues]); + + const save = useMutation({ + mutationFn: async (values: PresentationValues): Promise => { + const config = applyPresentationValues(record.config, [section], values); + const changed = createChangedStandardValues(record, source, section, values); + 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"] }); + if (source === "offering") { + void queryClient.invalidateQueries({ queryKey: ["admin-workspace-offering-fields"] }); + } + }, + }); + + const submit = (values: PresentationValues) => { + const currentValues = form.getFieldsValue(true) as PresentationValues; + save.mutate({ ...draftValues, ...values, ...currentValues }); + }; + + const handleValuesChange = (_values: PresentationValues) => { + const currentValues = form.getFieldsValue(true) as PresentationValues; + const next = { ...draftValues, ..._values, ...currentValues }; + setDraftValues(next); + onDraft?.(next); + }; + + return { + draftValues, + form, + handleValuesChange, + initialValues, + save, + submit, + }; +} diff --git a/apps/admin/src/features/brand/PresentationEditor.tsx b/apps/admin/src/features/brand/PresentationEditor.tsx index 2dfb633..646c56e 100644 --- a/apps/admin/src/features/brand/PresentationEditor.tsx +++ b/apps/admin/src/features/brand/PresentationEditor.tsx @@ -11,9 +11,8 @@ import { Space, Tag, Typography, - message, } from "antd"; -import { useMutation, useQuery, useQueryClient } from "@tanstack/react-query"; +import { useQueryClient } from "@tanstack/react-query"; import { useCallback, useEffect, useMemo, useState } from "react"; import { useNavigate } from "react-router-dom"; import type { @@ -24,7 +23,6 @@ import type { } from "@personal-brand/api-client"; import type { BrandProfile } from "@personal-brand/api-client"; import { projectBrandAliases, projectTrustPresentation } from "@personal-brand/api-client"; -import { createApiClient } from "../../lib/api.js"; import { TemplateListField } from "../../components/TemplateListField.js"; import { TemplateFieldRenderer } from "../../components/TemplateFieldRenderer.js"; import { MutationError } from "../../components/QueryState.js"; @@ -38,17 +36,20 @@ import { type PreviewTarget, } from "./preview-selection.js"; import { - applyBrandPreviewValues, - applyStandardBrandValues, - applyStandardEntityValues, - applyPresentationValues, filterSectionsForPreviewTarget, presentationFieldName, - readPresentationValue, type PresentationValues, } from "./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"; interface PresentationEditorProps { templateContent: AdminTemplateContent | null | undefined; @@ -136,7 +137,6 @@ export function PresentationEditor({ error, initialSelection, }: PresentationEditorProps) { - const [form] = Form.useForm(); const queryClient = useQueryClient(); const navigate = useNavigate(); const breakpoints = Grid.useBreakpoint(); @@ -149,45 +149,12 @@ export function PresentationEditor({ id: string; values: PresentationValues; }>(); - const [previewConfig, setPreviewConfig] = useState(config ?? {}); - const [previewBrandValues, setPreviewBrandValues] = useState>({}); const [inquiryDraft, setInquiryDraft] = useState(); const [brandDirty, setBrandDirty] = useState(false); const guardDraft = useEditorDraft(brandDirty || !!recordDraft || !!inquiryDraft); const inquiryRuntime = previewWorkspace?.inquiryForms.find((item) => item.target === "brand"); - const sections = useMemo(() => { - const declaredTemplateFields = (templateContent?.templateFields ?? []).map((field) => ({ - ...field, - binding: field.binding ?? `brand.config.templateData.${field.key}`, - })); - return [ - ...[...new Set(declaredTemplateFields.map((field) => field.page ?? "home"))].map((page) => ({ - key: `template-data:${page}`, - title: "模板专属内容", - description: "这些字段由当前小程序模板定义,保存后发布才会对外展示。", - page, - target: "brand" as const, - fields: declaredTemplateFields.filter((field) => (field.page ?? "home") === page), - })), - ...(templateContent?.admin.sections ?? []), - ].map((section) => ({ - ...section, - fields: section.fields.filter( - (field) => - field.binding.startsWith("brand.config.") || - [ - "brand.name", - "brand.tagline", - "brand.avatarUrl", - "brand.description", - "brand.contactPhone", - "brand.contactWechat", - "brand.address", - ].includes(field.binding), - ), - })); - }, [templateContent]); + const sections = useBrandPresentationSections(templateContent); const pageOptions = useMemo(() => { const pages = new Map(); for (const item of templateContent?.navigation ?? []) pages.set(item.page, item.label); @@ -248,23 +215,10 @@ export function PresentationEditor({ selectedTarget?.source === "offering" || selectedTarget?.source === "trust" ? selectedTarget.source : undefined; - const offeringsQuery = useQuery({ - queryKey: ["admin-offerings"], - queryFn: () => createApiClient().listAdminOfferings(), - enabled: selectedRecordSource === "offering", - refetchOnMount: "always", - }); - const trustQuery = useQuery({ - queryKey: ["trust-content"], - queryFn: () => createApiClient().listAdminTrustContent(), - enabled: selectedRecordSource === "trust", - }); - const selectedRecord = - selectedRecordSource === "offering" - ? offeringsQuery.data?.find((item) => item.id === selection?.recordId) - : selectedRecordSource === "trust" - ? trustQuery.data?.find((item) => item.id === selection?.recordId) - : undefined; + const { offeringsQuery, trustQuery, selectedRecord } = usePresentationRecordQueries( + selectedRecordSource, + selection?.recordId, + ); const editableSections = filterSectionsForPreviewTarget( visibleSections.filter((section) => section.fields.some((field) => field.binding.startsWith("brand.")), @@ -342,58 +296,9 @@ export function PresentationEditor({ return item.target === selectedSection.target; }) : pageLinks; - const initialValues = useMemo(() => { - const values: PresentationValues = {}; - for (const section of sections) { - for (const field of section.fields) { - values[presentationFieldName(section.key, field.key)] = readPresentationValue( - config, - field.binding, - field, - brand as unknown as Record, - ); - } - } - return values; - }, [config, sections]); - - const save = useMutation({ - mutationFn: (values: PresentationValues) => { - const nextConfig = applyPresentationValues(config, sections, values); - // 页面内容编辑不提交模板选择,避免触发切换模板的权限校验。 - const contentConfig = { ...nextConfig }; - delete contentConfig.visualTemplateKey; - const standard = applyStandardBrandValues( - brand as unknown as Record, - sections, - values, - ); - const changed = Object.fromEntries( - Object.entries(standard).filter( - ([key, value]) => - key !== "config" && value !== (brand as unknown as Record)[key], - ), - ); - return createApiClient().updateAdminBrand({ - ...changed, - config: contentConfig, - }); - }, - onSuccess: () => { - setBrandDirty(false); - message.success("页面内容已保存为草稿,发布后小程序才会更新"); - window.dispatchEvent(new Event("template-preview-refresh")); - void queryClient.invalidateQueries({ queryKey: ["admin-brand"] }); - void queryClient.invalidateQueries({ queryKey: ["admin-workspace"] }); - void queryClient.invalidateQueries({ queryKey: ["admin-preview-workspace"] }); - void queryClient.invalidateQueries({ queryKey: ["public-runtime-workspace"] }); - void queryClient.invalidateQueries({ queryKey: ["publication-readiness"] }); - }, - }); - - useEffect(() => { - form.setFieldsValue(initialValues); - }, [form, initialValues]); + const formState = useBrandPresentationForm({ brand, config, sections }); + const { form, initialValues } = formState; + const save = useBrandSaveMutation({ ...formState, brand, config, sections, queryClient }); useEffect(() => { const initialPage = @@ -453,62 +358,16 @@ export function PresentationEditor({ () => setRecordDraft(undefined), [selection?.targetKey, selection?.recordId, selectedPage], ); - useEffect(() => { - setPreviewConfig(config ?? {}); - setPreviewBrandValues({}); - }, [config, initialValues]); - const previewBrand = useMemo( - () => ({ ...brand, ...previewBrandValues, config: previewConfig }), - [brand, previewBrandValues, previewConfig], - ); - const recordPreview = useMemo(() => { - if (!recordDraft || !selectedRecord || !recordSection) return undefined; - const config = applyPresentationValues( - selectedRecord.config, - [recordSection], - recordDraft.values, - ); - return { - config, - standard: applyStandardEntityValues( - selectedRecord as unknown as Record, - recordDraft.source, - [recordSection], - recordDraft.values, - ), - }; - }, [recordDraft, selectedRecord, recordSection]); - const handleValuesChange = useCallback( - (changed: PresentationValues, allValues: PresentationValues) => { - setBrandDirty(true); - const synchronized: PresentationValues = { - ...(form.getFieldsValue(true) as PresentationValues), - ...allValues, - }; - for (const section of sections) { - for (const field of section.fields) { - const name = presentationFieldName(section.key, field.key); - if (!Object.prototype.hasOwnProperty.call(changed, name)) continue; - for (const peer of sections) { - for (const item of peer.fields) { - if (item.binding === field.binding) - synchronized[presentationFieldName(peer.key, item.key)] = changed[name]; - } - } - } - } - form.setFieldsValue(synchronized); - const next = applyBrandPreviewValues( - brand as unknown as Record, - config, - sections, - synchronized, - ); - setPreviewConfig((next.config as Record) ?? {}); - setPreviewBrandValues(next); - }, - [brand, config, sections, form], - ); + const { previewBrand, handleValuesChange, resetPreview } = useBrandPreviewDraft({ + brand, + config, + sections, + form, + onDirty: () => setBrandDirty(true), + }); + const recordPreview = useRecordPreview({ recordDraft, selectedRecord, recordSection }); + useEffect(() => resetPreview(), [config, initialValues, resetPreview]); + const previewConfig = previewBrand.config; const draftWorkspace = useMemo(() => { if (!previewWorkspace) return undefined; const next: RuntimeWorkspace = { @@ -652,8 +511,7 @@ export function PresentationEditor({ guardDraft(() => { setBrandDirty(false); form.setFieldsValue(initialValues); - setPreviewConfig(config ?? {}); - setPreviewBrandValues({}); + resetPreview(); setRecordDraft(undefined); setInquiryDraft(undefined); setActivePage(value); @@ -669,8 +527,7 @@ export function PresentationEditor({ guardDraft(() => { setBrandDirty(false); form.setFieldsValue(initialValues); - setPreviewConfig(config ?? {}); - setPreviewBrandValues({}); + resetPreview(); setRecordDraft(undefined); setInquiryDraft(undefined); setActivePage(String(value)); diff --git a/apps/admin/src/features/brand/presentation-editor-record-preview.ts b/apps/admin/src/features/brand/presentation-editor-record-preview.ts new file mode 100644 index 0000000..533ac9e --- /dev/null +++ b/apps/admin/src/features/brand/presentation-editor-record-preview.ts @@ -0,0 +1,39 @@ +import { useMemo } from "react"; +import type { Offering, TrustContent } from "@personal-brand/api-client"; +import type { TemplateAdminSection } from "@personal-brand/api-client"; +import { applyPresentationValues, applyStandardEntityValues } from "./presentation-bindings.js"; +import type { PresentationValues } from "./presentation-bindings.js"; + +export interface RecordDraft { + source: "offering" | "trust"; + id: string; + values: PresentationValues; +} + +export function useRecordPreview({ + recordDraft, + selectedRecord, + recordSection, +}: { + recordDraft?: RecordDraft | undefined; + selectedRecord?: Offering | TrustContent | undefined; + recordSection?: TemplateAdminSection | undefined; +}) { + return useMemo(() => { + if (!recordDraft || !selectedRecord || !recordSection) return undefined; + const config = applyPresentationValues( + selectedRecord.config, + [recordSection], + recordDraft.values, + ); + return { + config, + standard: applyStandardEntityValues( + selectedRecord as unknown as Record, + recordDraft.source, + [recordSection], + recordDraft.values, + ), + }; + }, [recordDraft, selectedRecord, recordSection]); +} diff --git a/apps/admin/src/features/brand/presentation-editor-state.ts b/apps/admin/src/features/brand/presentation-editor-state.ts new file mode 100644 index 0000000..dee7a9a --- /dev/null +++ b/apps/admin/src/features/brand/presentation-editor-state.ts @@ -0,0 +1,226 @@ +import { Form, message } from "antd"; +import { useMutation, useQuery } from "@tanstack/react-query"; +import type { QueryClient } from "@tanstack/react-query"; +import { useEffect, useMemo, useState } from "react"; +import type { FormInstance } from "antd"; +import type { + AdminTemplateContent, + BrandProfile, + Offering, + TemplateAdminSection, + TrustContent, +} from "@personal-brand/api-client"; +import { createApiClient } from "../../lib/api.js"; +import { + applyBrandPreviewValues, + applyPresentationValues, + applyStandardBrandValues, + presentationFieldName, + readPresentationValue, + type PresentationValues, +} from "./presentation-bindings.js"; + +const standardBrandBindings = [ + "brand.name", + "brand.tagline", + "brand.avatarUrl", + "brand.description", + "brand.contactPhone", + "brand.contactWechat", + "brand.address", +] as const; + +export interface BrandPresentationFormState { + form: FormInstance; + initialValues: PresentationValues; +} + +export function useBrandPresentationSections( + templateContent: AdminTemplateContent | null | undefined, +): TemplateAdminSection[] { + return useMemo(() => { + const declaredTemplateFields = (templateContent?.templateFields ?? []).map((field) => ({ + ...field, + binding: field.binding ?? `brand.config.templateData.${field.key}`, + })); + return [ + ...[...new Set(declaredTemplateFields.map((field) => field.page ?? "home"))].map((page) => ({ + key: `template-data:${page}`, + title: "模板专属内容", + description: "这些字段由当前小程序模板定义,保存后发布才会对外展示。", + page, + target: "brand" as const, + fields: declaredTemplateFields.filter((field) => (field.page ?? "home") === page), + })), + ...(templateContent?.admin.sections ?? []), + ].map((section) => ({ + ...section, + fields: section.fields.filter( + (field) => + field.binding.startsWith("brand.config.") || + (standardBrandBindings as readonly string[]).includes(field.binding), + ), + })); + }, [templateContent]); +} + +export function useBrandPresentationForm({ + brand, + config, + sections, +}: { + brand: BrandProfile; + config: Record | null | undefined; + sections: TemplateAdminSection[]; +}): BrandPresentationFormState { + const [form] = Form.useForm(); + const initialValues = useMemo(() => { + const values: PresentationValues = {}; + for (const section of sections) { + for (const field of section.fields) { + values[presentationFieldName(section.key, field.key)] = readPresentationValue( + config, + field.binding, + field, + brand as unknown as Record, + ); + } + } + return values; + }, [brand, config, sections]); + + useEffect(() => form.setFieldsValue(initialValues), [form, initialValues]); + return { form, initialValues }; +} + +interface BrandSaveMutationOptions extends BrandPresentationFormState { + brand: BrandProfile; + config: Record | null | undefined; + sections: TemplateAdminSection[]; + queryClient: QueryClient; +} + +export function useBrandSaveMutation({ + brand, + config, + sections, + queryClient, +}: BrandSaveMutationOptions) { + return useMutation({ + mutationFn: (values: PresentationValues) => { + const nextConfig = applyPresentationValues(config, sections, values); + const contentConfig = { ...nextConfig }; + delete contentConfig.visualTemplateKey; + const standard = applyStandardBrandValues( + brand as unknown as Record, + sections, + values, + ); + const changed = Object.fromEntries( + Object.entries(standard).filter( + ([key, value]) => + key !== "config" && value !== (brand as unknown as Record)[key], + ), + ); + return createApiClient().updateAdminBrand({ ...changed, config: contentConfig }); + }, + onSuccess: () => { + message.success("页面内容已保存为草稿,发布后小程序才会更新"); + window.dispatchEvent(new Event("template-preview-refresh")); + void queryClient.invalidateQueries({ queryKey: ["admin-brand"] }); + void queryClient.invalidateQueries({ queryKey: ["admin-workspace"] }); + void queryClient.invalidateQueries({ queryKey: ["admin-preview-workspace"] }); + void queryClient.invalidateQueries({ queryKey: ["public-runtime-workspace"] }); + void queryClient.invalidateQueries({ queryKey: ["publication-readiness"] }); + }, + }); +} + +export function useBrandPreviewDraft({ + brand, + config, + sections, + form, + onDirty, +}: { + brand: BrandProfile; + config: Record | null | undefined; + sections: TemplateAdminSection[]; + form: BrandPresentationFormState["form"]; + onDirty: () => void; +}) { + const [previewConfig, setPreviewConfig] = useState(config ?? {}); + const [previewBrandValues, setPreviewBrandValues] = useState>({}); + + useEffect(() => { + setPreviewConfig(config ?? {}); + setPreviewBrandValues({}); + }, [config, form]); + + const handleValuesChange = (changed: PresentationValues, allValues: PresentationValues) => { + onDirty(); + const synchronized: PresentationValues = { + ...(form.getFieldsValue(true) as PresentationValues), + ...allValues, + }; + for (const section of sections) { + for (const field of section.fields) { + const name = presentationFieldName(section.key, field.key); + if (!Object.prototype.hasOwnProperty.call(changed, name)) continue; + for (const peer of sections) { + for (const item of peer.fields) { + if (item.binding === field.binding) + synchronized[presentationFieldName(peer.key, item.key)] = changed[name]; + } + } + } + } + form.setFieldsValue(synchronized); + const next = applyBrandPreviewValues( + brand as unknown as Record, + config, + sections, + synchronized, + ); + setPreviewConfig((next.config as Record) ?? {}); + setPreviewBrandValues(next); + }; + + const previewBrand = useMemo( + () => ({ ...brand, ...previewBrandValues, config: previewConfig }), + [brand, previewBrandValues, previewConfig], + ); + + return { + previewBrand, + handleValuesChange, + resetPreview: () => { + setPreviewConfig(config ?? {}); + setPreviewBrandValues({}); + }, + }; +} + +export function usePresentationRecordQueries( + source: "offering" | "trust" | undefined, + recordId: string | undefined, +) { + const offeringsQuery = useQuery({ + queryKey: ["admin-offerings"], + queryFn: () => createApiClient().listAdminOfferings(), + enabled: source === "offering", + refetchOnMount: "always", + }); + const trustQuery = useQuery({ + queryKey: ["trust-content"], + queryFn: () => createApiClient().listAdminTrustContent(), + enabled: source === "trust", + }); + const selectedRecord = + source === "offering" + ? offeringsQuery.data?.find((item: Offering) => item.id === recordId) + : source === "trust" + ? trustQuery.data?.find((item: TrustContent) => item.id === recordId) + : undefined; + return { offeringsQuery, trustQuery, selectedRecord }; +} diff --git a/apps/admin/src/features/services/OfferingEditorModal.tsx b/apps/admin/src/features/services/OfferingEditorModal.tsx new file mode 100644 index 0000000..e0a5d17 --- /dev/null +++ b/apps/admin/src/features/services/OfferingEditorModal.tsx @@ -0,0 +1,181 @@ +import { Alert, Button, Form, Input, Modal, Segmented, Select, Typography } from "antd"; +import type { + Offering, + RuntimeWorkspace, + TemplateAdminField, + TemplateAdminSection, +} from "@personal-brand/api-client"; +import { ImageUrlField } from "../../components/ImageUrlField.js"; +import { RecordContentFields } from "../../components/RecordContentEditor.js"; +import { ComponentTemplatePreview } from "../brand/ComponentTemplatePreview.js"; +import type { PreviewTarget } from "../brand/preview-selection.js"; +import type { FormInstance } from "antd"; +import "./ServicesPage.css"; + +type OfferingEditorModalProps = { + open: boolean; + editingOffering: Offering | null; + form: FormInstance>; + previewPage: "offerings" | "detail"; + previewWorkspace: RuntimeWorkspace | undefined; + previewTargets: Record<"offerings" | "detail", PreviewTarget[]>; + activeTemplateFields: TemplateAdminField[]; + templateFieldSection: TemplateAdminSection; + hasTemplateDetailCopy: boolean; + listTarget: PreviewTarget; + detailTarget: PreviewTarget | undefined; + saving: boolean; + onClose: () => void; + onPreviewPageChange: (page: "offerings" | "detail") => void; + onValuesChange: (values: Record) => void; + onFinish: (values: Record) => void; + onEditInBrand: () => void; +}; + +export function OfferingEditorModal({ + open, + editingOffering, + form, + previewPage, + previewWorkspace, + previewTargets, + activeTemplateFields, + templateFieldSection, + hasTemplateDetailCopy, + listTarget, + detailTarget, + saving, + onClose, + onPreviewPageChange, + onValuesChange, + onFinish, + onEditInBrand, +}: OfferingEditorModalProps) { + return ( + form.submit()} + confirmLoading={saving} + > +
+ {editingOffering && previewWorkspace ? ( +
+ + + {}} + /> +
+ ) : null} + onValuesChange(allValues)} + onFinish={onFinish} + > +
+ 基础信息 + + + + + + + {!hasTemplateDetailCopy ? ( + + + + ) : null} + + + + + + + + + + + - - - - - - - - {!hasTemplateDetailCopy ? ( - - - - ) : null} - - - - - - - - - - - ({ value: item.type, label: item.label }))} - /> - - {selectedTypeMeta?.pages?.length ? ( - - 这类内容会显示在: - {selectedTypeMeta.pages.map(pageLabel).join("、")} - - ) : null} - - - - {!selectedTypeMeta?.hideStandardFields?.includes("summary") && ( - - - - )} - {!selectedTypeMeta?.hideStandardFields?.includes("content") && ( - - - - )} - {selectedTypeMeta?.configFields?.map((field) => - field.input === "list" ? ( - - ) : ( - - {field.input === "textarea" ? : } - - ), - )} - - - - - ({ value: item.type, label: item.label }))} + /> + + {selectedTypeMeta?.pages?.length ? ( + + 这类内容会显示在:{selectedTypeMeta.pages.map(pageLabel).join("、")} + + ) : null} + + + + {!selectedTypeMeta?.hideStandardFields?.includes("summary") && ( + + + + )} + {!selectedTypeMeta?.hideStandardFields?.includes("content") && ( + + + + )} + {selectedTypeMeta?.configFields?.map((field) => + field.input === "list" ? ( + + ) : ( + + {field.input === "textarea" ? : } + + ), + )} + + + + +