Files
UHD_Media_Tool/apps/web/app/components/downloads/download-detail.tsx
T
rootandClaude Opus 5.5 46d37d87fe fix: replace raw codes and internal IDs in the console with Chinese labels
Found in v0.2 visual QA (docs/plan/09-visual-qa.md):
- actor strings (admin_session:<uuid>, user:<uuid>, system) -> 管理员/系统/API Key
  in policy revisions, subscriptions, inbox, trash, business tasks, organize detail
- cache volume without key shows the downloader name instead of instance:<uuid>
- download timeline/plan: task and organize statuses, plan purpose, priority and
  status reason localized; audit gets labels for capability/files/site/feed actions
- organize record reason uses conflict detail (没有可入库的主视频) instead of
  blocked:no_main_video; slot/candidate reasons drop inflight:<uuid>
- identity decision summary no longer shows no_viable_candidate
- upstream test: 'Connection error.' classified as network, with a Chinese hint
- new policy draft prefills key default (placeholder looked like a value)
- dialog/sheet close sr-only text in Chinese

Co-Authored-By: Claude Opus 5.5 <[email protected]>
2026-09-28 15:22:15 +08:00

205 lines
8.5 KiB
TypeScript

import type { DownloadTask } from "@umt/contracts";
import { Bot, FolderInput, Pause, Play, ShieldAlert } from "lucide-react";
import { useState } from "react";
import { toast } from "sonner";
import {
DOWNLOAD_STATUS_LABEL,
DOWNLOAD_STATUS_TONE,
PAUSE_REASON_LABEL,
PLAN_STATUS_LABEL,
useDownloadAction,
useDownloaderNames,
usePlan,
} from "~/api/downloads.ts";
import { useDownloadTrace } from "~/api/tasks.ts";
import { openAgentDrawer } from "~/components/agent/agent-store.ts";
import { DetailSection } from "~/components/console/detail-sheet.tsx";
import { JsonView } from "~/components/console/json-view.tsx";
import { KvList } from "~/components/console/kv-list.tsx";
import { StatusBadge } from "~/components/console/status-badge.tsx";
import { Button } from "~/components/ui/button.tsx";
import { relTime } from "~/components/workbench/release-fields.ts";
import { errorMessage } from "~/lib/api.ts";
import { planReasonText } from "~/lib/labels.ts";
import { formatBytes, formatDateTime } from "~/lib/utils.ts";
import { BusinessTimeline } from "./business-timeline.tsx";
import { OrganizeDialog } from "./organize-dialog.tsx";
const PLAN_PURPOSE_LABEL: Record<string, string> = { fill: "补缺", upgrade: "升级", replace_source: "换源" };
/** 进度:细线 + 文字;总量未知时不伪造百分比 */
export function ProgressLine({ t }: { t: DownloadTask }) {
const known = t.sizeBytes !== null;
return (
<div className="min-w-24">
<div className="h-1 overflow-hidden rounded-full bg-foreground/[0.08]">
<div className="h-full rounded-full bg-foreground/60" style={{ width: `${Math.round(t.progress * 100)}%` }} />
</div>
<p className="mt-0.5 text-meta text-muted-foreground tabular-nums">
{known ? `${(t.progress * 100).toFixed(1)}%` : `已下 ${formatBytes(t.downloadedBytes, 1)}`}
</p>
</div>
);
}
export function DownloadStatusBadges({ t }: { t: DownloadTask }) {
return (
<span className="flex flex-wrap gap-1">
<StatusBadge tone={DOWNLOAD_STATUS_TONE[t.status]}>{DOWNLOAD_STATUS_LABEL[t.status]}</StatusBadge>
{t.pauseReason === "threshold" ? (
<StatusBadge tone="warning">阈值暂停</StatusBadge>
) : t.pauseReason ? (
<StatusBadge>{PAUSE_REASON_LABEL[t.pauseReason]}</StatusBadge>
) : null}
{!t.managed ? <StatusBadge>未纳管</StatusBadge> : null}
{t.origin === "adopted" ? <StatusBadge>接管</StatusBadge> : null}
</span>
);
}
/** 暂停/恢复:阈值暂停在水位回落前不可手动恢复(服务端强制) */
export function PauseResume({ t, size = "sm" }: { t: DownloadTask; size?: "sm" | "xs" }) {
const act = useDownloadAction();
const canPause = ["queued", "downloading", "stalled"].includes(t.status);
const canResume = t.status === "paused";
if (!canPause && !canResume) return null;
const threshold = t.pauseReason === "threshold";
return (
<Button
variant="outline"
size={size}
disabled={act.isPending}
title={threshold ? "阈值暂停,缓存回落到恢复水位后自动恢复" : undefined}
onKeyDown={(e) => e.stopPropagation()}
onClick={(e) => {
e.stopPropagation();
act.mutate(
{ id: t.id, action: canResume ? "resume" : "pause" },
{
onSuccess: () => toast.success(canResume ? "已恢复" : "已暂停。阈值回落时不会自动恢复"),
onError: (err) => toast.error(errorMessage(err)),
},
);
}}
>
{canResume ? <Play /> : <Pause />}
{canResume ? "恢复" : "暂停"}
</Button>
);
}
/** 下载任务详情:状态、阈值说明、业务时间线、文件、关联计划 */
export function DownloadDetail({ task }: { task: DownloadTask }) {
const plan = usePlan(task.acquisitionPlanId);
const names = useDownloaderNames();
const trace = useDownloadTrace(task, plan.data);
const [organizing, setOrganizing] = useState(false);
const canOrganize = ["completed", "importing", "imported"].includes(task.status);
return (
<div className="space-y-5">
<div className="flex flex-wrap items-center gap-2">
<DownloadStatusBadges t={task} />
<div className="ml-auto flex flex-wrap gap-2">
<PauseResume t={task} />
<Button
variant="outline"
size="sm"
disabled={!canOrganize}
title={canOrganize ? undefined : "下载完成后才能整理"}
onClick={() => setOrganizing(true)}
>
<FolderInput />
整理
</Button>
<Button
variant="ghost"
size="sm"
onClick={() =>
openAgentDrawer({
label: `下载:${task.name}`,
downloadTaskId: task.id,
goal: task.status === "stalled" || task.status === "failed" ? "排查这个下载为什么没有进展" : "",
})
}
>
<Bot />问 Agent
</Button>
</div>
</div>
{task.pauseReason === "threshold" ? (
<p className="flex items-start gap-2 rounded-lg border border-warning/40 bg-warning/10 px-3 py-2 text-caption text-warning">
<ShieldAlert className="mt-0.5 h-3.5 w-3.5 shrink-0" />
缓存卷达到硬阈值,任务已暂停,水位回落到恢复线后自动恢复。手动恢复和 Agent 都不能绕过阈值。
</p>
) : null}
{task.noProgressSince ? (
<p className="rounded-lg border border-warning/40 bg-warning/10 px-3 py-2 text-caption text-warning">
自 {formatDateTime(task.noProgressSince)} 起无进展,已记入待处理,不会自动换源。
</p>
) : null}
<DetailSection title="进度">
<ProgressLine t={task} />
<KvList
className="mt-3"
items={[
{ label: "下载器", value: names.data?.get(task.instanceId) ?? task.instanceId.slice(0, 8) },
{ label: "大小", value: task.sizeBytes === null ? "未知" : formatBytes(task.sizeBytes, 2) },
{ label: "剩余", value: task.remainingBytes === null ? "未知" : formatBytes(task.remainingBytes, 2) },
{ label: "分享率", value: task.ratio === null ? "—" : task.ratio.toFixed(2) },
{ label: "最近进展", value: relTime(task.lastProgressAt) },
{ label: "最近同步", value: relTime(task.lastSyncedAt) },
{ label: "标签", value: task.tags.join(", ") },
{ label: "分类", value: task.category },
{ label: "保存路径", value: task.savePath, mono: true, wide: true },
{ label: "内容路径", value: task.contentPath, mono: true, wide: true },
{ label: "torrent hash", value: task.torrentHash, mono: true, wide: true },
]}
/>
</DetailSection>
<DetailSection title="业务时间线">
<BusinessTimeline events={trace.events} loading={trace.loading} />
</DetailSection>
{plan.data ? (
<DetailSection title="下载计划">
<KvList
items={[
{ label: "状态", value: PLAN_STATUS_LABEL[plan.data.status] },
{ label: "用途", value: PLAN_PURPOSE_LABEL[plan.data.purpose] ?? plan.data.purpose },
{ label: "预留空间", value: formatBytes(plan.data.reservedBytes, 1) },
{ label: "说明", value: planReasonText(plan.data.statusReason), wide: true },
]}
/>
<JsonView className="mt-2" label="评估依据" value={plan.data.evaluation} />
</DetailSection>
) : null}
{task.fileList?.length ? (
<DetailSection title={`文件(${task.fileList.length})`}>
<ul className="max-h-60 space-y-1 overflow-auto text-meta">
{task.fileList.map((f) => (
<li key={f.path} className="flex justify-between gap-3">
<span className="min-w-0 font-mono break-all text-foreground">{f.path}</span>
<span className="shrink-0 text-muted-foreground tabular-nums">
{formatBytes(f.size, 1)}
{f.progress !== undefined ? ` · ${Math.round(f.progress * 100)}%` : ""}
</span>
</li>
))}
</ul>
</DetailSection>
) : null}
{task.lastError ? <JsonView label="下载器错误" value={task.lastError} tone="destructive" defaultOpen /> : null}
{organizing ? (
<OrganizeDialog open onOpenChange={(o) => !o && setOrganizing(false)} downloadTaskId={task.id} />
) : null}
</div>
);
}