206 lines
7.9 KiB
TypeScript
206 lines
7.9 KiB
TypeScript
import { useEffect, useRef, useState } from "react";
|
|
import { useQuery } from "@tanstack/react-query";
|
|
import { Alert, Collapse, List, Modal, Select, Tag, Typography } from "antd";
|
|
import type { TemplatePackageVersion } from "@personal-brand/api-client";
|
|
import { createApiClient } from "../../lib/api.js";
|
|
import { QueryState } from "../../components/QueryState.js";
|
|
import { EmptyState } from "../../components/EmptyState.js";
|
|
import { ComponentTemplatePreview } from "../brand/ComponentTemplatePreview.js";
|
|
|
|
const pageLabels: Record<string, string> = {
|
|
home: "首页",
|
|
offerings: "业务列表",
|
|
service: "服务",
|
|
profile: "我的 / 主理人",
|
|
detail: "业务详情",
|
|
inquiry: "咨询表单",
|
|
records: "咨询记录",
|
|
success: "提交结果",
|
|
};
|
|
|
|
export function TemplatePackagePreviewModal({
|
|
version,
|
|
onClose,
|
|
}: {
|
|
version: TemplatePackageVersion;
|
|
onClose: () => void;
|
|
}) {
|
|
const [industry, setIndustry] = useState(version.manifestSummary?.supportedIndustryTemplates[0]);
|
|
const [page, setPage] = useState("home");
|
|
const [offeringId, setOfferingId] = useState<string>();
|
|
const client = createApiClient();
|
|
// 按 key 保存短期 token,避免 queryFn 引用自身 query 造成循环初始化。
|
|
const sessionTokens = useRef(new Map<string, string>());
|
|
const preview = useQuery({
|
|
queryKey: ["admin-template-package-preview", version.id, industry],
|
|
queryFn: () =>
|
|
client.previewAdminTemplatePackage(
|
|
version.id,
|
|
industry!,
|
|
sessionTokens.current.get(industry!),
|
|
),
|
|
enabled: Boolean(industry),
|
|
retry: false,
|
|
gcTime: 0,
|
|
});
|
|
const data = preview.data;
|
|
useEffect(() => {
|
|
if (preview.isError) {
|
|
sessionTokens.current.delete(industry ?? "");
|
|
}
|
|
if (data && industry) {
|
|
sessionTokens.current.set(industry, data.preview.session.token);
|
|
}
|
|
}, [data, industry, preview.isError]);
|
|
return (
|
|
<Modal
|
|
title={`${version.displayName} · ${version.version} 版本预览`}
|
|
open
|
|
footer={null}
|
|
width={960}
|
|
onCancel={onClose}
|
|
>
|
|
<div className="admin-stack">
|
|
<Select
|
|
aria-label="预览行业"
|
|
value={industry}
|
|
placeholder="选择行业"
|
|
options={(version.manifestSummary?.supportedIndustryTemplates ?? []).map((value) => ({
|
|
label: value,
|
|
value,
|
|
}))}
|
|
onChange={(value) => {
|
|
setIndustry(value);
|
|
setPage("home");
|
|
setOfferingId(undefined);
|
|
}}
|
|
/>
|
|
<Alert
|
|
type="info"
|
|
showIcon
|
|
title="模板演示预览"
|
|
description="演示内容不属于真实品牌,咨询提交和客户记录读取已禁用。"
|
|
/>
|
|
{!industry ? (
|
|
<EmptyState title="没有可预览的行业" description="请检查模板包的行业兼容声明。" />
|
|
) : (
|
|
<QueryState
|
|
loading={preview.isLoading}
|
|
error={preview.error}
|
|
onRetry={() => void preview.refetch()}
|
|
>
|
|
{data ? (
|
|
<>
|
|
{data.preview.diagnostics.map((diagnostic) => (
|
|
<Alert key={diagnostic.code} type="warning" showIcon title={diagnostic.message} />
|
|
))}
|
|
<Typography.Text type="secondary">
|
|
演示数据:
|
|
{data.preview.fixtureSource === "previewFixture"
|
|
? "独立预览内容"
|
|
: "兼容起始内容"}
|
|
</Typography.Text>
|
|
<Typography.Paragraph type="secondary">
|
|
{data.industry.name} · Runtime {data.compatibility.runtimeContract} · renderer{" "}
|
|
{data.compatibility.renderer.key}@{data.compatibility.renderer.version}
|
|
</Typography.Paragraph>
|
|
<Select
|
|
aria-label="预览页面"
|
|
value={page}
|
|
options={data.template.pages.map((value) => ({
|
|
value,
|
|
label:
|
|
data.template.navigation.find((tab) => tab.page === value)?.label ??
|
|
pageLabels[value] ??
|
|
value,
|
|
}))}
|
|
onChange={setPage}
|
|
/>
|
|
{page === "detail" ? (
|
|
<Select
|
|
aria-label="预览业务记录"
|
|
value={offeringId ?? data.workspace.offerings[0]?.id}
|
|
options={data.workspace.offerings.map((item) => ({
|
|
label: item.name,
|
|
value: item.id,
|
|
}))}
|
|
onChange={setOfferingId}
|
|
/>
|
|
) : null}
|
|
<Collapse
|
|
items={[
|
|
{
|
|
key: "modules",
|
|
label: "页面模块与字段来源",
|
|
children: (
|
|
<List
|
|
dataSource={
|
|
data.preview.pages.find((item) => item.key === page)?.modules ?? []
|
|
}
|
|
locale={{ emptyText: "此页面由 Runtime 提供,不包含模板编辑模块" }}
|
|
renderItem={(item) => (
|
|
<List.Item>
|
|
<div>
|
|
<Typography.Text strong>{item.title}</Typography.Text>
|
|
<Typography.Paragraph type="secondary">
|
|
{item.bindings.join("、")}
|
|
</Typography.Paragraph>
|
|
</div>
|
|
</List.Item>
|
|
)}
|
|
/>
|
|
),
|
|
},
|
|
{
|
|
key: "assets",
|
|
label: "资源诊断",
|
|
children: (
|
|
<List
|
|
dataSource={data.preview.assets}
|
|
locale={{ emptyText: "模板未声明资源槽位" }}
|
|
renderItem={(item) => (
|
|
<List.Item>
|
|
<div>
|
|
{item.label}{" "}
|
|
<Tag color={item.status === "missing" ? "warning" : "default"}>
|
|
{item.status === "missing"
|
|
? "内容缺失"
|
|
: item.status === "ready"
|
|
? "包内资源已验证"
|
|
: "可用性待验证"}
|
|
</Tag>
|
|
{item.required ? "必需" : "可选"} · 已配置 {item.populated}/
|
|
{item.total}
|
|
<Typography.Paragraph type="secondary">
|
|
{item.binding}
|
|
</Typography.Paragraph>
|
|
</div>
|
|
</List.Item>
|
|
)}
|
|
/>
|
|
),
|
|
},
|
|
]}
|
|
/>
|
|
<ComponentTemplatePreview
|
|
key={`${version.id}:${industry}:${page}`}
|
|
workspace={data.workspace}
|
|
assetFiles={data.preview.assetFiles}
|
|
page={page}
|
|
{...((offeringId ?? data.workspace.offerings[0]?.id)
|
|
? { offeringId: offeringId ?? data.workspace.offerings[0]!.id }
|
|
: {})}
|
|
targets={[]}
|
|
onTargetSelected={() => undefined}
|
|
onPageSelected={setPage}
|
|
onOfferingSelected={setOfferingId}
|
|
/>
|
|
</>
|
|
) : null}
|
|
</QueryState>
|
|
)}
|
|
</div>
|
|
</Modal>
|
|
);
|
|
}
|