统一模板包 visual 契约并补齐完成证据;修复 release 版本一致性、管理员 token 撤销、CORS 与默认品牌配置;收敛依赖安全风险并完成全量回归验证。
126 lines
5.1 KiB
TypeScript
126 lines
5.1 KiB
TypeScript
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 } 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]);
|
|
}
|