194 lines
7.2 KiB
TypeScript
194 lines
7.2 KiB
TypeScript
import { useEffect, useRef } from "react";
|
|
import { Button, Card, Collapse, Flex, Space, Tooltip, Typography } from "antd";
|
|
import type { MiniAppReleaseTask } from "@personal-brand/api-client";
|
|
import { ErrorNote } from "../../components/QueryState.js";
|
|
import { StatusTag } from "../../components/StatusTag.js";
|
|
import type { StatusTone } from "../../components/StatusTag.js";
|
|
|
|
type ReleaseStatus = MiniAppReleaseTask["status"];
|
|
|
|
const STALE_BUILDING_MS = 30 * 60 * 1000;
|
|
|
|
function isStaleBuilding(task: MiniAppReleaseTask): boolean {
|
|
if (task.buildStatus !== "building") return false;
|
|
const updatedAt = new Date(task.updatedAt).getTime();
|
|
return !Number.isNaN(updatedAt) && Date.now() - updatedAt >= STALE_BUILDING_MS;
|
|
}
|
|
|
|
const statusView: Record<ReleaseStatus, { label: string; tone: StatusTone; hint: string }> = {
|
|
built: { label: "待上传", tone: "neutral", hint: "代码已构建,可上传到微信。" },
|
|
uploaded: { label: "已上传微信", tone: "info", hint: "请在微信后台上传体验版并提交审核。" },
|
|
under_review: {
|
|
label: "审核中",
|
|
tone: "warning",
|
|
hint: "等待微信审核结果,通过后回到本页标记。",
|
|
},
|
|
approved: { label: "审核通过", tone: "success", hint: "审核已通过,确认无误后标记为已发布。" },
|
|
published: { label: "已发布", tone: "success", hint: "该版本已发布,可继续编辑内容后再次上传。" },
|
|
rejected: { label: "已驳回", tone: "error", hint: "微信驳回后需修改内容并重新构建上传。" },
|
|
failed: { label: "失败", tone: "error", hint: "上次构建或上传失败,可直接重试。" },
|
|
};
|
|
|
|
const uploadStatusView: Record<
|
|
NonNullable<MiniAppReleaseTask["uploadStatus"]>,
|
|
{ label: string; tone: StatusTone }
|
|
> = {
|
|
idle: { label: "未上传", tone: "neutral" },
|
|
uploading: { label: "上传中", tone: "warning" },
|
|
uploaded: { label: "上传完成", tone: "success" },
|
|
failed: { label: "上传失败", tone: "error" },
|
|
timed_out: { label: "上传超时", tone: "error" },
|
|
};
|
|
|
|
const reviewStatusView: Record<
|
|
NonNullable<MiniAppReleaseTask["reviewStatus"]>,
|
|
{ label: string; tone: StatusTone }
|
|
> = {
|
|
not_submitted: { label: "未提交审核", tone: "neutral" },
|
|
under_review: { label: "审核中", tone: "warning" },
|
|
approved: { label: "审核通过", tone: "success" },
|
|
rejected: { label: "审核驳回", tone: "error" },
|
|
};
|
|
|
|
function formatTime(value: string): string {
|
|
const date = new Date(value);
|
|
if (Number.isNaN(date.getTime())) return value;
|
|
return date.toLocaleString("zh-CN", { hour12: false });
|
|
}
|
|
|
|
function formatLogTime(value: string): string {
|
|
const date = new Date(value);
|
|
if (Number.isNaN(date.getTime())) return "";
|
|
return date.toLocaleTimeString("zh-CN", { hour12: false });
|
|
}
|
|
|
|
export function MiniAppReleaseCard({
|
|
task,
|
|
uploading,
|
|
onUpload,
|
|
onStatusChange,
|
|
}: {
|
|
task: MiniAppReleaseTask;
|
|
uploading: boolean;
|
|
onUpload: () => void;
|
|
onStatusChange: (status: ReleaseStatus) => void;
|
|
}) {
|
|
const logRef = useRef<HTMLPreElement>(null);
|
|
const logs = task.buildLogs ?? [];
|
|
const status = statusView[task.status];
|
|
const uploadStatus = task.uploadStatus ? uploadStatusView[task.uploadStatus] : null;
|
|
const reviewStatus = task.reviewStatus ? reviewStatusView[task.reviewStatus] : null;
|
|
const building = task.buildStatus === "building";
|
|
const staleBuilding = isStaleBuilding(task);
|
|
|
|
useEffect(() => {
|
|
if (!building) return;
|
|
const node = logRef.current;
|
|
if (node) node.scrollTop = node.scrollHeight;
|
|
}, [building, logs.length]);
|
|
|
|
const actions = (
|
|
<Space wrap>
|
|
{(task.status === "built" || task.status === "failed" || staleBuilding) && (
|
|
<Button
|
|
type={task.status === "built" && !staleBuilding ? "primary" : "default"}
|
|
size="small"
|
|
loading={uploading}
|
|
onClick={onUpload}
|
|
>
|
|
{task.status === "built" && !staleBuilding ? "上传微信" : "重试上传微信"}
|
|
</Button>
|
|
)}
|
|
{task.status === "uploaded" && (
|
|
<Button size="small" onClick={() => onStatusChange("under_review")}>
|
|
标记提交审核
|
|
</Button>
|
|
)}
|
|
{task.status === "under_review" && (
|
|
<>
|
|
<Button size="small" onClick={() => onStatusChange("approved")}>
|
|
审核通过
|
|
</Button>
|
|
<Button size="small" danger onClick={() => onStatusChange("rejected")}>
|
|
审核驳回
|
|
</Button>
|
|
</>
|
|
)}
|
|
{task.status === "approved" && (
|
|
<Button size="small" type="primary" onClick={() => onStatusChange("published")}>
|
|
标记已发布
|
|
</Button>
|
|
)}
|
|
</Space>
|
|
);
|
|
|
|
return (
|
|
<Card size="small" styles={{ body: { padding: 16 } }}>
|
|
<Flex justify="space-between" align="flex-start" gap={12} wrap>
|
|
<Space orientation="vertical" size={4}>
|
|
<Space wrap size={8} align="center">
|
|
<Typography.Text strong>品牌内容 v{task.version}</Typography.Text>
|
|
<StatusTag label={status.label} tone={status.tone} />
|
|
{building && (
|
|
<StatusTag tone="warning" label={staleBuilding ? "构建已中断" : "构建中"} />
|
|
)}
|
|
{!building && task.buildStatus === "success" && (
|
|
<StatusTag tone="success" label="构建完成" />
|
|
)}
|
|
{uploadStatus && <StatusTag tone={uploadStatus.tone} label={uploadStatus.label} />}
|
|
{reviewStatus && reviewStatusView[task.reviewStatus ?? "not_submitted"] && (
|
|
<StatusTag tone={reviewStatus.tone} label={reviewStatus.label} />
|
|
)}
|
|
{task.weappUploadId && (
|
|
<Typography.Text type="secondary">微信版本 {task.weappUploadId}</Typography.Text>
|
|
)}
|
|
</Space>
|
|
<span className="admin-text-xs">
|
|
{staleBuilding ? "超过 30 分钟没有新的构建输出,可能已中断;可直接重试。" : status.hint}
|
|
</span>
|
|
<Tooltip title={`创建于 ${formatTime(task.createdAt)}`}>
|
|
<span className="admin-text-xs-quiet">更新于 {formatTime(task.updatedAt)}</span>
|
|
</Tooltip>
|
|
</Space>
|
|
{actions}
|
|
</Flex>
|
|
|
|
{task.error && (
|
|
<div className="admin-offset-top-md">
|
|
<ErrorNote error={new Error(task.error)} />
|
|
</div>
|
|
)}
|
|
|
|
{logs.length > 0 && (
|
|
<Collapse
|
|
ghost
|
|
size="small"
|
|
className="admin-offset-top-sm"
|
|
{...(building || task.buildStatus === "failed" ? { defaultActiveKey: ["logs"] } : {})}
|
|
items={[
|
|
{
|
|
key: "logs",
|
|
label: <span className="admin-text-xs">构建日志({logs.length} 行)</span>,
|
|
children: (
|
|
<pre ref={logRef} className="admin-log">
|
|
{logs.map((log, index) => (
|
|
<div
|
|
key={`${log.at}-${index}`}
|
|
className="admin-log__line"
|
|
data-stream={log.stream}
|
|
>
|
|
<span className="admin-log__time">{formatLogTime(log.at)}</span>
|
|
{" "}
|
|
{log.message}
|
|
</div>
|
|
))}
|
|
</pre>
|
|
),
|
|
},
|
|
]}
|
|
/>
|
|
)}
|
|
</Card>
|
|
);
|
|
}
|