feat: 完成模板目录导入与生命周期管理
This commit is contained in:
1 parent
553a07c4ec
commit
413413efda
25 files changed
+1313
-24
No files matched your search
@@ -21,6 +21,7 @@ import {
|
||||
FeatureMembersPage,
|
||||
FeatureOverviewPage,
|
||||
FeaturePublicationPage,
|
||||
FeatureTemplateManagementPage,
|
||||
FeatureServicesPage,
|
||||
FeatureTrustContentPage,
|
||||
FeatureWorkspacePage,
|
||||
@@ -35,7 +36,15 @@ import { Route, Routes } from "react-router-dom";
|
||||
import { AdminOverviewRedirect } from "./AdminRouter.js";
|
||||
import { Suspense } from "react";
|
||||
|
||||
export function Dashboard({ onLogout, role }: { onLogout: () => void; role: string }) {
|
||||
export function Dashboard({
|
||||
onLogout,
|
||||
role,
|
||||
platformTemplateRole,
|
||||
}: {
|
||||
onLogout: () => void;
|
||||
role: string;
|
||||
platformTemplateRole: string;
|
||||
}) {
|
||||
const navigate = useNavigate();
|
||||
const location = useLocation();
|
||||
const brand = useQuery({
|
||||
@@ -79,6 +88,9 @@ export function Dashboard({ onLogout, role }: { onLogout: () => void; role: stri
|
||||
children: [
|
||||
{ key: "capabilities", label: "能力插件", icon: <ThunderboltOutlined /> },
|
||||
{ key: "members", label: "团队成员", icon: <TeamOutlined /> },
|
||||
...(platformTemplateRole === "template_admin"
|
||||
? [{ key: "templates", label: "模板管理", icon: <AppstoreOutlined /> }]
|
||||
: []),
|
||||
],
|
||||
},
|
||||
]),
|
||||
@@ -143,6 +155,9 @@ export function Dashboard({ onLogout, role }: { onLogout: () => void; role: stri
|
||||
<>
|
||||
<Route path="capabilities" element={<FeatureCapabilitiesPage />} />
|
||||
<Route path="members" element={<FeatureMembersPage />} />
|
||||
{platformTemplateRole === "template_admin" ? (
|
||||
<Route path="templates" element={<FeatureTemplateManagementPage />} />
|
||||
) : null}
|
||||
</>
|
||||
)}
|
||||
<Route path="*" element={<AdminOverviewRedirect />} />
|
||||
|
||||
@@ -7,6 +7,9 @@ import { adminMessage as message } from "../components/AdminMessage.js";
|
||||
export function Root() {
|
||||
const [loggedIn, setLoggedIn] = useState(Boolean(localStorage.getItem("adminToken")));
|
||||
const [role, setRole] = useState(localStorage.getItem("adminRole") || "owner");
|
||||
const [platformTemplateRole, setPlatformTemplateRole] = useState(
|
||||
localStorage.getItem("adminPlatformTemplateRole") || "none",
|
||||
);
|
||||
const [mustChangePassword, setMustChangePassword] = useState(
|
||||
localStorage.getItem("adminMustChangePassword") === "true",
|
||||
);
|
||||
@@ -21,6 +24,7 @@ export function Root() {
|
||||
const logout = () => {
|
||||
localStorage.removeItem("adminToken");
|
||||
localStorage.removeItem("adminRole");
|
||||
localStorage.removeItem("adminPlatformTemplateRole");
|
||||
localStorage.removeItem("adminMustChangePassword");
|
||||
setLoggedIn(false);
|
||||
};
|
||||
@@ -29,6 +33,7 @@ export function Root() {
|
||||
<Login
|
||||
onLogin={(session) => {
|
||||
setRole(session.role);
|
||||
setPlatformTemplateRole(session.platformTemplateRole);
|
||||
setMustChangePassword(session.mustChangePassword);
|
||||
setLoggedIn(true);
|
||||
}}
|
||||
@@ -37,7 +42,7 @@ export function Root() {
|
||||
if (mustChangePassword) return <ChangePassword onComplete={() => setMustChangePassword(false)} />;
|
||||
return (
|
||||
<AdminRouter>
|
||||
<Dashboard onLogout={logout} role={role} />
|
||||
<Dashboard onLogout={logout} role={role} platformTemplateRole={platformTemplateRole} />
|
||||
</AdminRouter>
|
||||
);
|
||||
}
|
||||
@@ -43,3 +43,8 @@ export const FeaturePublicationPage = React.lazy(() =>
|
||||
default: module.PublicationPage,
|
||||
})),
|
||||
);
|
||||
export const FeatureTemplateManagementPage = React.lazy(() =>
|
||||
import("../features/template-management/TemplateManagementPage.js").then((module) => ({
|
||||
default: module.TemplateManagementPage,
|
||||
})),
|
||||
);
|
||||
@@ -9,7 +9,11 @@ import { AdminForm, AdminInput } from "../../components/AdminForm.js";
|
||||
export function Login({
|
||||
onLogin,
|
||||
}: {
|
||||
onLogin: (session: { role: string; mustChangePassword: boolean }) => void;
|
||||
onLogin: (session: {
|
||||
role: string;
|
||||
mustChangePassword: boolean;
|
||||
platformTemplateRole: "none" | "template_admin";
|
||||
}) => void;
|
||||
}) {
|
||||
const [form] = AdminForm.useForm();
|
||||
const [loading, setLoading] = useState(false);
|
||||
@@ -19,6 +23,7 @@ export function Login({
|
||||
const result = await createApiClient().loginAdmin(values.username, values.password);
|
||||
localStorage.setItem("adminToken", result.token);
|
||||
localStorage.setItem("adminRole", result.role);
|
||||
localStorage.setItem("adminPlatformTemplateRole", result.platformTemplateRole);
|
||||
localStorage.setItem("adminMustChangePassword", String(result.mustChangePassword));
|
||||
onLogin(result);
|
||||
} catch (error) {
|
||||
|
||||
@@ -0,0 +1,252 @@
|
||||
import { useRef, useState } from "react";
|
||||
import { useMutation, useQuery, useQueryClient } from "@tanstack/react-query";
|
||||
import { Alert, Button, List, Modal, Table, Tag, Typography, message } from "antd";
|
||||
import { ReloadOutlined, UploadOutlined } from "@ant-design/icons";
|
||||
import type { TemplatePackageVersion } from "@personal-brand/api-client";
|
||||
import { createApiClient } from "../../lib/api.js";
|
||||
import { AdminPageHeader } from "../../components/AdminPageHeader.js";
|
||||
import { AdminSection } from "../../components/AdminSection.js";
|
||||
import { EmptyState } from "../../components/EmptyState.js";
|
||||
import { ErrorNote, QueryState } from "../../components/QueryState.js";
|
||||
|
||||
const client = createApiClient();
|
||||
|
||||
function formatBytes(value: unknown): string {
|
||||
const bytes = typeof value === "number" ? value : 0;
|
||||
if (bytes < 1024) return `${bytes} B`;
|
||||
if (bytes < 1024 * 1024) return `${(bytes / 1024).toFixed(1)} KB`;
|
||||
return `${(bytes / 1024 / 1024).toFixed(1)} MB`;
|
||||
}
|
||||
|
||||
export function TemplateManagementPage() {
|
||||
const inputRef = useRef<HTMLInputElement>(null);
|
||||
const [page, setPage] = useState(1);
|
||||
const [pageSize, setPageSize] = useState(20);
|
||||
const [auditId, setAuditId] = useState<string | null>(null);
|
||||
const queryClient = useQueryClient();
|
||||
const packages = useQuery({
|
||||
queryKey: ["admin-template-packages", page, pageSize],
|
||||
queryFn: () => client.listAdminTemplatePackages({ page, pageSize }),
|
||||
});
|
||||
const audit = useQuery({
|
||||
queryKey: ["admin-template-package-audit", auditId],
|
||||
queryFn: () => client.listAdminTemplatePackageAudit(auditId as string),
|
||||
enabled: Boolean(auditId),
|
||||
});
|
||||
const lifecycle = useMutation({
|
||||
mutationFn: async ({ id, action }: { id: string; action: "publish" | "retire" }) =>
|
||||
action === "publish"
|
||||
? client.publishAdminTemplatePackage(id)
|
||||
: client.retireAdminTemplatePackage(id),
|
||||
onSuccess: (result) => {
|
||||
void queryClient.invalidateQueries({ queryKey: ["admin-template-packages"] });
|
||||
message.success(result.changed ? "模板目录状态已更新" : "模板目录状态未变化");
|
||||
},
|
||||
});
|
||||
const importing = useMutation({
|
||||
mutationFn: (file: File) => {
|
||||
const form = new FormData();
|
||||
form.append("archive", file, file.name);
|
||||
return client.importAdminTemplatePackage(form);
|
||||
},
|
||||
onSuccess: () => {
|
||||
void queryClient.invalidateQueries({ queryKey: ["admin-template-packages"] });
|
||||
},
|
||||
});
|
||||
const chooseFile = () => inputRef.current?.click();
|
||||
const onFileChange = (event: React.ChangeEvent<HTMLInputElement>) => {
|
||||
const file = event.target.files?.[0];
|
||||
event.target.value = "";
|
||||
if (!file) return;
|
||||
if (!file.name.endsWith(".tar.gz")) {
|
||||
importing.reset();
|
||||
return;
|
||||
}
|
||||
importing.mutate(file);
|
||||
};
|
||||
const rows = packages.data?.items ?? [];
|
||||
return (
|
||||
<div className="admin-stack admin-stack-lg">
|
||||
<AdminPageHeader
|
||||
title="模板管理"
|
||||
description="导入通过本地模板检查的确定性 archive,登记版本、校验摘要并供后续预览与安装使用。"
|
||||
extra={
|
||||
<>
|
||||
<input
|
||||
ref={inputRef}
|
||||
type="file"
|
||||
accept=".tar.gz,application/gzip"
|
||||
onChange={onFileChange}
|
||||
hidden
|
||||
/>
|
||||
<Button
|
||||
type="primary"
|
||||
icon={<UploadOutlined />}
|
||||
loading={importing.isPending}
|
||||
onClick={chooseFile}
|
||||
>
|
||||
导入模板包
|
||||
</Button>
|
||||
</>
|
||||
}
|
||||
/>
|
||||
<Alert
|
||||
type="info"
|
||||
showIcon
|
||||
message="导入规则"
|
||||
description="系统会校验文件路径、链接、文件数量、解压体积、template.json、package.json 和 checksums.json;包内脚本不会被执行。"
|
||||
/>
|
||||
{importing.isError ? <ErrorNote error={importing.error} /> : null}
|
||||
{importing.isSuccess ? (
|
||||
<Alert
|
||||
type="success"
|
||||
showIcon
|
||||
message={importing.data.idempotent ? "模板版本已存在,未重复写入" : "模板版本导入成功"}
|
||||
description={`${importing.data.version.packageKey}@${importing.data.version.version}`}
|
||||
/>
|
||||
) : null}
|
||||
{lifecycle.isError ? <ErrorNote error={lifecycle.error} /> : null}
|
||||
<AdminSection
|
||||
title="已导入版本"
|
||||
description="同一 package key 的不同版本并列保留,发布品牌时再选择安装版本。"
|
||||
>
|
||||
<QueryState
|
||||
loading={packages.isLoading}
|
||||
error={packages.error}
|
||||
onRetry={() => void packages.refetch()}
|
||||
>
|
||||
{rows.length ? (
|
||||
<Table<TemplatePackageVersion>
|
||||
rowKey="id"
|
||||
dataSource={rows}
|
||||
pagination={{
|
||||
current: packages.data?.page ?? page,
|
||||
pageSize: packages.data?.pageSize ?? pageSize,
|
||||
total: packages.data?.total ?? 0,
|
||||
showSizeChanger: true,
|
||||
onChange: (nextPage, nextPageSize) => {
|
||||
setPage(nextPageSize === pageSize ? nextPage : 1);
|
||||
setPageSize(nextPageSize);
|
||||
},
|
||||
}}
|
||||
scroll={{ x: 980 }}
|
||||
columns={[
|
||||
{
|
||||
title: "模板",
|
||||
key: "template",
|
||||
render: (_, item) => (
|
||||
<div>
|
||||
<Typography.Text strong>{item.displayName}</Typography.Text>
|
||||
<div className="admin-muted-text">{item.packageKey}</div>
|
||||
</div>
|
||||
),
|
||||
},
|
||||
{ title: "版本", dataIndex: "version", key: "version" },
|
||||
{
|
||||
title: "状态",
|
||||
dataIndex: "status",
|
||||
key: "status",
|
||||
render: (value: TemplatePackageVersion["status"]) => (
|
||||
<Tag
|
||||
color={
|
||||
value === "published" ? "green" : value === "imported" ? "blue" : "default"
|
||||
}
|
||||
>
|
||||
{value === "published"
|
||||
? "已发布"
|
||||
: value === "imported"
|
||||
? "待发布"
|
||||
: value === "retired"
|
||||
? "已撤回"
|
||||
: "已停用"}
|
||||
</Tag>
|
||||
),
|
||||
},
|
||||
{
|
||||
title: "文件",
|
||||
key: "report",
|
||||
render: (_, item) => {
|
||||
const report = item.validationReport;
|
||||
return `${String(report.fileCount ?? "-")} 个,${formatBytes(report.extractedBytes)}`;
|
||||
},
|
||||
},
|
||||
{
|
||||
title: "内容 checksum",
|
||||
dataIndex: "contentChecksum",
|
||||
key: "contentChecksum",
|
||||
render: (value: string) => (
|
||||
<Typography.Text copyable>{value.slice(0, 16)}…</Typography.Text>
|
||||
),
|
||||
},
|
||||
{
|
||||
title: "导入人",
|
||||
key: "importedBy",
|
||||
render: (_, item) => item.importedBy.displayName,
|
||||
},
|
||||
{
|
||||
title: "操作",
|
||||
key: "actions",
|
||||
render: (_, item) => (
|
||||
<span>
|
||||
<Button
|
||||
type="link"
|
||||
loading={lifecycle.isPending && lifecycle.variables?.id === item.id}
|
||||
onClick={() =>
|
||||
lifecycle.mutate({
|
||||
id: item.id,
|
||||
action: item.status === "published" ? "retire" : "publish",
|
||||
})
|
||||
}
|
||||
disabled={item.status === "disabled"}
|
||||
>
|
||||
{item.status === "published" ? "撤回" : "发布"}
|
||||
</Button>
|
||||
<Button type="link" onClick={() => setAuditId(item.id)}>
|
||||
审计
|
||||
</Button>
|
||||
</span>
|
||||
),
|
||||
},
|
||||
]}
|
||||
/>
|
||||
) : (
|
||||
<EmptyState
|
||||
title="还没有导入模板"
|
||||
description="从本地执行 template:pack 后导入 tar.gz。"
|
||||
/>
|
||||
)}
|
||||
</QueryState>
|
||||
</AdminSection>
|
||||
{packages.isError ? (
|
||||
<Button icon={<ReloadOutlined />} onClick={() => void packages.refetch()}>
|
||||
重试加载
|
||||
</Button>
|
||||
) : null}
|
||||
<Modal
|
||||
title="模板版本审计"
|
||||
open={Boolean(auditId)}
|
||||
footer={null}
|
||||
onCancel={() => setAuditId(null)}
|
||||
>
|
||||
<QueryState
|
||||
loading={audit.isLoading}
|
||||
error={audit.error}
|
||||
onRetry={() => void audit.refetch()}
|
||||
>
|
||||
<List
|
||||
dataSource={audit.data ?? []}
|
||||
locale={{ emptyText: "暂无审计记录" }}
|
||||
renderItem={(item) => (
|
||||
<List.Item>
|
||||
<List.Item.Meta
|
||||
title={`${item.action} · ${item.actor.displayName}`}
|
||||
description={`${item.fromStatus ?? "-"} -> ${item.toStatus ?? "-"} · ${new Date(item.createdAt).toLocaleString()}`}
|
||||
/>
|
||||
</List.Item>
|
||||
)}
|
||||
/>
|
||||
</QueryState>
|
||||
</Modal>
|
||||
</div>
|
||||
);
|
||||
}
|
||||
Reference in new issue
Block a user