Files
persionalBrand/apps/admin/src/features/publication/MiniAppReleaseCard.tsx
T
que01 ac3f433ee6 feat: 重做小程序发布任务卡片并支持配置请求地址
- 发布任务卡片拆为 MiniAppReleaseCard,状态用中文映射并附下一步说明,构建日志收进折叠面板,构建中自动滚动到底部
- 小程序配置增加“小程序请求地址”字段,展示当前生效域名,未配置时直接提示原因与修复位置
- 顶部上传按钮在无任务时自动创建发布任务,不再报“还没有可发布的内容版本”
- 构建轮询在任务被判定为中断后停止,避免页面空转请求
2026-10-13 21:59:00 +08:00

186 lines
6.7 KiB
TypeScript

import { useEffect, useRef } from "react";
import { Alert, Button, Card, Collapse, Flex, Space, Tag, Tooltip, Typography, theme } from "antd";
import { LoadingOutlined } from "@ant-design/icons";
import type { MiniAppReleaseTask } from "@personal-brand/api-client";
type ReleaseStatus = MiniAppReleaseTask["status"];
const STALE_BUILDING_MS = 15 * 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; color: string; hint: string }> = {
built: { label: "待上传", color: "default", hint: "代码已构建,可上传到微信。" },
uploaded: { label: "已上传微信", color: "blue", hint: "请在微信后台上传体验版并提交审核。" },
under_review: {
label: "审核中",
color: "processing",
hint: "等待微信审核结果,通过后回到本页标记。",
},
approved: { label: "审核通过", color: "cyan", hint: "审核已通过,确认无误后标记为已发布。" },
published: { label: "已发布", color: "green", hint: "该版本已发布,可继续编辑内容后再次上传。" },
rejected: { label: "已驳回", color: "volcano", hint: "微信驳回后需修改内容并重新构建上传。" },
failed: { label: "失败", color: "red", hint: "上次构建或上传失败,可直接重试。" },
};
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 { token } = theme.useToken();
const logRef = useRef<HTMLPreElement>(null);
const logs = task.buildLogs ?? [];
const status = statusView[task.status];
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 direction="vertical" size={4}>
<Space wrap size={8} align="center">
<Typography.Text strong>品牌内容 v{task.version}</Typography.Text>
<Tag color={status.color}>{status.label}</Tag>
{building && (
<Tag icon={<LoadingOutlined />} color="orange">
{staleBuilding ? "构建已中断" : "构建中"}
</Tag>
)}
{task.weappUploadId && (
<Typography.Text type="secondary">微信版本 {task.weappUploadId}</Typography.Text>
)}
</Space>
<Typography.Text type="secondary" style={{ fontSize: 12 }}>
{staleBuilding ? "超过 15 分钟没有新的构建输出,可能已中断;可直接重试。" : status.hint}
</Typography.Text>
<Tooltip title={`创建于 ${formatTime(task.createdAt)}`}>
<Typography.Text type="secondary" style={{ fontSize: 12 }}>
更新于 {formatTime(task.updatedAt)}
</Typography.Text>
</Tooltip>
</Space>
{actions}
</Flex>
{task.error && <Alert type="error" showIcon message={task.error} style={{ marginTop: 12 }} />}
{logs.length > 0 && (
<Collapse
ghost
size="small"
style={{ marginTop: 8 }}
{...(building || task.buildStatus === "failed" ? { defaultActiveKey: ["logs"] } : {})}
items={[
{
key: "logs",
label: (
<Typography.Text type="secondary" style={{ fontSize: 12 }}>
构建日志({logs.length} 行)
</Typography.Text>
),
children: (
<pre
ref={logRef}
style={{
margin: 0,
padding: 12,
maxHeight: 220,
overflow: "auto",
fontSize: 12,
lineHeight: 1.7,
borderRadius: token.borderRadius,
border: `1px solid ${token.colorBorderSecondary}`,
background: token.colorFillQuaternary,
}}
>
{logs.map((log, index) => (
<div
key={`${log.at}-${index}`}
style={{
color:
log.stream === "stderr" ? token.colorError : token.colorTextSecondary,
whiteSpace: "pre-wrap",
wordBreak: "break-all",
}}
>
<Typography.Text type="secondary" style={{ fontSize: 12 }}>
{formatLogTime(log.at)}
</Typography.Text>
{" "}
{log.message}
</div>
))}
</pre>
),
},
]}
/>
)}
</Card>
);
}