refactor: 模板注册与默认模板收敛到目录驱动

This commit is contained in:
que01 committed 2026-10-12 10:00:46 +08:00
1 parent f87b26b201
commit 89fdfd7d83
25 files changed
+122 -96

No files matched your search

+8 -4
View File
@@ -48,6 +48,10 @@ export function BrandPage() {
queryFn: () => client.getPublicTemplate(brand.data!.templateKey),
enabled: Boolean(brand.data?.templateKey),
});
const templateCatalog = useQuery({
queryKey: ["template-catalog"],
queryFn: () => client.listPublicTemplates(),
});
const save = useMutation({
mutationFn: (values: Partial<BrandProfile>) => client.updateAdminBrand(values),
onSuccess: () => {
@@ -190,10 +194,10 @@ export function BrandPage() {
>
<Select
disabled
options={[
{ value: "personal", label: "个人品牌" },
{ value: "food", label: "餐饮模板" },
]}
options={(templateCatalog.data?.industries ?? []).map((item) => ({
value: item.key,
label: item.name,
}))}
/>
</Form.Item>
<Row gutter={16}>
@@ -3,8 +3,6 @@ import type { RuntimeWorkspace } from "@personal-brand/api-client";
import { Alert } from "antd";
import { resolveTemplateRenderer } from "./component-renderers.jsx";
import type { PreviewSelection, PreviewTarget } from "./preview-selection.js";
import "../../../../../packages/template-acheng-campus-food/taro/style.css";
import "../../../../../packages/template-linjing-editorial/taro/style.css";
import "./ComponentTemplatePreview.css";
type PreviewProps = {
@@ -1,11 +1,13 @@
import { TemplatePage as FoodTemplatePage } from "../../../../../packages/template-acheng-campus-food/taro/index.tsx";
import { TemplatePage as EditorialTemplatePage } from "../../../../../packages/template-linjing-editorial/taro/index.tsx";
import React from "react";
import { Inquiry } from "../../../../taro/src/components/Inquiry.tsx";
import { Records } from "../../../../taro/src/components/Records.tsx";
import { Success } from "../../../../taro/src/components/Success.tsx";
import { TemplateAssetContext } from "./taro-web-components.jsx";
const templateRenderers = import.meta.glob("../../../../../packages/template-*/taro/index.tsx", {
eager: true,
});
function pageContent({ workspace, page }) {
if (page === "inquiry") return <Inquiry workspace={workspace} preview />;
if (page === "records") return <Records workspace={workspace} preview />;
@@ -13,28 +15,22 @@ function pageContent({ workspace, page }) {
return null;
}
function FoodPreview(props) {
return (
<TemplateAssetContext.Provider value="acheng-campus-food">
<FoodTemplatePage {...props}>{pageContent(props)}</FoodTemplatePage>
</TemplateAssetContext.Provider>
);
}
function EditorialPreview(props) {
return (
<TemplateAssetContext.Provider value="linjing-editorial">
<EditorialTemplatePage {...props}>{pageContent(props)}</EditorialTemplatePage>
</TemplateAssetContext.Provider>
);
}
export function resolveTemplateRenderer(workspace) {
const config = workspace?.brand?.config;
const key =
config && typeof config === "object" && !Array.isArray(config)
? config.visualTemplateKey
: undefined;
if (key === "acheng-campus-food") return FoodPreview;
if (key === "linjing-editorial") return EditorialPreview;
return null;
if (typeof key !== "string") return null;
const suffix = `/template-${key}/taro/index.tsx`;
const entry = Object.entries(templateRenderers).find(([path]) => path.endsWith(suffix));
const TemplatePage = entry?.[1]?.TemplatePage;
if (!TemplatePage) return null;
return function TemplatePreview(props) {
return (
<TemplateAssetContext.Provider value={key}>
<TemplatePage {...props}>{pageContent(props)}</TemplatePage>
</TemplateAssetContext.Provider>
);
};
}
@@ -147,14 +147,16 @@ export function PublicationPage({ canManageTemplate }: { canManageTemplate: bool
workspace.data?.installations[0]?.templateKey ??
workspace.data?.brand?.templateKey ??
brand.data?.templateKey ??
"personal";
const visuals = (catalog.data?.visuals ?? []).filter((item) =>
item.supportedIndustryTemplates.includes(industryKey),
catalog.data?.industries[0]?.key ??
null;
const visuals = (catalog.data?.visuals ?? []).filter(
(item) =>
typeof industryKey === "string" && item.supportedIndustryTemplates.includes(industryKey),
);
const visualKey =
typeof brand.data?.config?.visualTemplateKey === "string"
? brand.data.config.visualTemplateKey
: "linjing-editorial";
: (visuals[0]?.key ?? null);
const currentRelease = releases.data?.find((item) => item.status === "published");
const publishedHistory = (releases.data ?? []).filter(
(item) => item.status === "published" || item.status === "superseded",
@@ -31,7 +31,7 @@ export function ServicesPage() {
const listTarget = previewTargets.offerings.find((target) =>
templateFields.some((field) => field.previewTarget === target.key),
) ?? {
key: "linjing-editorial.offerings.item",
key: "offering.list.item",
label: "业务卡片",
page: "offerings",
source: "offering",
@@ -51,8 +51,8 @@ export function WorkspacePage() {
const [editingForm, setEditingForm] = useState<InquiryForm | null>(null);
const [visualTarget, setVisualTarget] = useState<string | null>(null);
const [industryTarget, setIndustryTarget] = useState<IndustryTarget | null>(null);
const [previewIndustry, setPreviewIndustry] = useState<BrandTemplateKey>("personal");
const [previewVisual, setPreviewVisual] = useState("linjing-editorial");
const [previewIndustry, setPreviewIndustry] = useState<BrandTemplateKey | null>(null);
const [previewVisual, setPreviewVisual] = useState<string | null>(null);
const [form] = Form.useForm<Record<string, unknown>>();
const [inquiryForm] = Form.useForm<Record<string, unknown>>();
const openNewInquiryForm = () => {
@@ -69,14 +69,28 @@ export function WorkspacePage() {
setFormOpen(true);
};
const { query, runtimeQuery, templateCatalog, forms, templatePreview } = useWorkspaceQueries();
const previewIndustryKey =
previewIndustry ??
query.data?.installations[0]?.templateKey ??
query.data?.brand.templateKey ??
templateCatalog.data?.industries[0]?.key;
const previewVisualKey =
previewVisual ??
runtimeQuery.data?.visualTemplate?.key ??
(templateCatalog.data?.visuals ?? []).find((visual) =>
supportsIndustry(visual, previewIndustryKey ?? ""),
)?.key;
const combinationPreview = useQuery({
queryKey: ["template-combination-preview", previewIndustry, previewVisual],
queryKey: ["template-combination-preview", previewIndustryKey, previewVisualKey],
queryFn: () =>
createApiClient().previewPublicTemplateCombination(previewIndustry, previewVisual),
enabled: Boolean(templateCatalog.data),
createApiClient().previewPublicTemplateCombination(
previewIndustryKey ?? "",
previewVisualKey ?? "",
),
enabled: Boolean(templateCatalog.data && previewIndustryKey && previewVisualKey),
});
const compatibleVisuals = (templateCatalog.data?.visuals ?? []).filter((visual) =>
supportsIndustry(visual, query.data?.brand?.templateKey ?? "personal"),
supportsIndustry(visual, query.data?.brand?.templateKey ?? ""),
);
const { visualTemplate, rollbackVisual, install, upgrade, rollback } = useTemplateMutations({
runtimeConfig: runtimeQuery.data?.brand?.config ?? undefined,
@@ -185,9 +199,9 @@ export function WorkspacePage() {
<Space wrap>
<Typography.Text strong>视觉模板</Typography.Text>
<Select
value={runtimeQuery.data?.visualTemplate?.key ?? "linjing-editorial"}
value={runtimeQuery.data?.visualTemplate?.key}
loading={visualTemplate.isPending}
onChange={(key) => setVisualTarget(key)}
onChange={(key) => setVisualTarget(key ?? null)}
options={[
...compatibleVisuals.map((visual) => ({ value: visual.key, label: visual.name })),
]}
@@ -354,18 +368,18 @@ export function WorkspacePage() {
</Typography.Paragraph>
<Space wrap>
<Select
value={previewIndustry}
onChange={setPreviewIndustry}
value={previewIndustryKey ?? null}
onChange={(key) => setPreviewIndustry(key)}
options={(templateCatalog.data?.industries ?? []).map((item) => ({
value: item.key,
label: item.name,
}))}
/>
<Select
value={previewVisual}
onChange={setPreviewVisual}
value={previewVisualKey ?? null}
onChange={(key) => setPreviewVisual(key)}
options={(templateCatalog.data?.visuals ?? [])
.filter((item) => supportsIndustry(item, previewIndustry))
.filter((item) => supportsIndustry(item, previewIndustryKey ?? ""))
.map((item) => ({ value: item.key, label: item.name }))}
/>
</Space>