refactor: 拆分品牌展示编辑器职责
This commit is contained in:
1 parent
b23120477f
commit
6f18c67315
5 files changed
+376
-315
No files matched your search
@@ -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