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(null); const [page, setPage] = useState(1); const [pageSize, setPageSize] = useState(20); const [auditId, setAuditId] = useState(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) => { 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 (
} /> {importing.isError ? : null} {importing.isSuccess ? ( ) : null} {lifecycle.isError ? : null} void packages.refetch()} > {rows.length ? ( 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) => (
{item.displayName}
{item.packageKey}
), }, { title: "版本", dataIndex: "version", key: "version" }, { title: "状态", dataIndex: "status", key: "status", render: (value: TemplatePackageVersion["status"]) => ( {value === "published" ? "已发布" : value === "imported" ? "待发布" : value === "retired" ? "已撤回" : "已停用"} ), }, { 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) => ( {value.slice(0, 16)}… ), }, { title: "导入人", key: "importedBy", render: (_, item) => item.importedBy.displayName, }, { title: "操作", key: "actions", render: (_, item) => ( ), }, ]} /> ) : ( )}
{packages.isError ? ( ) : null} setAuditId(null)} > void audit.refetch()} > ( ${item.toStatus ?? "-"} · ${new Date(item.createdAt).toLocaleString()}`} /> )} />
); }