Files
UHD_Media_Tool/apps/web/app/components/downloads/business-tasks.tsx
T
rootandClaude Opus 5.5 21c55dfb3a feat(web): 记录抽屉与两层导航标准
控制台导航最多两层(侧栏一项 = 一页,页内最多一层页签 / 分段);从列表打开的
记录改成列表页上的右侧抽屉,打开的记录写在 ?open=,刷新、分享、前进后退都能还原,
后退就是关抽屉。标准写在 docs/design/console-ia.md §9,ui-controls.md 引用。

- DetailSheet 加 xl 宽度、标题下操作行;抽屉里再开的 DetailSheet 在同一抽屉里换成子视图(返回 / Esc)
- record-sheet.tsx:useRecordSheet / RecordLink / useOpenRecord / recordPath / taskPath
- 整理记录、订阅、站点(含页签)、下载、日志任务改成抽屉;旧业务任务 / 下载任务 id 在任务抽屉里换算
- 回收站并进文件管理右侧视图(?view=trash);设置左栏只列分区
- 规则组、策略版本、媒体库未识别 / 外部身份、设置实例编辑走 ?open=
- 旧地址 /console/{organize,subscriptions,sites,downloads}/:id、/tasks/:id、/files/trash 跳到列表页抽屉
- 站内链接、铃铛、命令面板、任务时间线改拼新地址;冒烟按 ?open= 检查抽屉

Co-Authored-By: Claude Opus 5.5 <[email protected]>
Claude-Session: https://claude.ai/code/session_01Cxwgnn2pbzQ7rXv3rq7fei
2026-10-08 14:03:47 +08:00

216 lines
7.5 KiB
TypeScript

import { useMutation, useQueryClient } from "@tanstack/react-query";
import { cursorPage, Task, type TaskKind, type TaskStatus } from "@umt/contracts";
import { RotateCcw, Square } from "lucide-react";
import { Link } from "react-router";
import { toast } from "sonner";
import { apiMutate } from "~/api/parse.ts";
import { DetailSection } from "~/components/console/detail-sheet.tsx";
import { KvList } from "~/components/console/kv-list.tsx";
import { consolePath } from "~/components/console/nav.ts";
import { recordPath, taskPath } from "~/components/console/record-sheet.tsx";
import { StatusBadge } from "~/components/console/status-badge.tsx";
import { Button } from "~/components/ui/button.tsx";
import { errorMessage } from "~/lib/api.ts";
import { actorText } from "~/lib/labels.ts";
import { formatDateTime } from "~/lib/utils.ts";
// 旧业务任务(task 表)的展示零件:引擎只负责推进,task 表是面向用户的投影;取消只停后续步骤,重试只对失败任务。
// 只剩日志页任务抽屉在打开旧任务 id 时用(components/tasks/legacy-task.tsx)。
const _TaskPage = cursorPage(Task);
export const TASK_KIND_LABEL: Record<TaskKind, string> = {
ingestFeed: "抓取 RSS",
resolveObservation: "识别资源",
evaluateAcquisition: "评估资源",
submitDownload: "提交下载",
verifyAndImport: "核验整理",
refreshMetadata: "刷新元数据",
reconcileLibrary: "文件核对",
cleanupDownloadCache: "清理缓存",
resolveDecision: "处理事项",
agentRun: "智能体运行",
};
export const TASK_STATUS: Record<
TaskStatus,
{ label: string; tone: "success" | "warning" | "destructive" | "neutral" }
> = {
pending: { label: "排队中", tone: "neutral" },
running: { label: "运行中", tone: "neutral" },
waiting: { label: "等待中", tone: "warning" },
succeeded: { label: "成功", tone: "success" },
failed: { label: "失败", tone: "destructive" },
cancelled: { label: "已取消", tone: "neutral" },
dead: { label: "已放弃", tone: "destructive" },
};
export const ACTIVE_TASK: TaskStatus[] = ["pending", "running", "waiting"];
export const SUBJECT_LABEL: Record<string, string> = {
download_task: "下载任务",
acquisition_plan: "下载",
acquisition_slot: "收录版本",
organization_record: "整理记录",
organize_request: "整理请求",
media_entity: "作品",
media_asset: "文件",
source_release: "资源",
feed_source: "RSS",
site: "站点",
library_root: "媒体库目录",
cache_volume: "下载盘",
};
/** 业务任务的对象 → 它的"家"页面 */
export function subjectLink(t: Pick<Task, "subjectType" | "subjectId">): string | null {
if (!t.subjectId) return null;
switch (t.subjectType) {
case "download_task":
return recordPath("downloads", t.subjectId);
case "acquisition_plan":
return `${consolePath("downloads")}?plan=${encodeURIComponent(t.subjectId)}`;
case "organization_record":
return recordPath("organize", t.subjectId);
case "media_entity":
return consolePath("library", t.subjectId);
case "site":
return recordPath("sites", t.subjectId);
case "feed_source":
// RSS 属于站点(IA-2):按 ?feed= 查所属站点后跳到站点的 RSS Tab
return `${consolePath("sources")}?feed=${encodeURIComponent(t.subjectId)}`;
default:
return null;
}
}
export function subjectText(t: Pick<Task, "subjectType" | "subjectId">): string {
if (!t.subjectType) return "—";
const label = SUBJECT_LABEL[t.subjectType] ?? t.subjectType;
return t.subjectId ? `${label} · ${t.subjectId.slice(0, 8)}` : label;
}
/** 取消 / 重试 */
export function useTaskAction() {
const qc = useQueryClient();
return useMutation({
mutationFn: ({ id, op }: { id: string; op: "cancel" | "retry" }) =>
apiMutate(`/api/v1/tasks/:id/${op}`, {
pathParams: { id },
body: op === "cancel" ? { reason: "控制台取消" } : {},
}).then((d) => Task.parse(d)),
onSuccess: (_t, v) => {
toast.success(v.op === "cancel" ? "已请求取消,后续步骤不再执行" : "已重新排队");
void qc.invalidateQueries({ queryKey: ["tasks"] });
},
onError: (e) => toast.error(errorMessage(e)),
});
}
/** 取消 / 重试按钮(按状态二选一或都不显示) */
export function TaskActions({ t, size = "sm" }: { t: Task; size?: "sm" | "xs" }) {
const action = useTaskAction();
if (ACTIVE_TASK.includes(t.status))
return (
<Button
variant="outline"
size={size}
disabled={action.isPending}
onClick={(e) => {
e.stopPropagation();
action.mutate({ id: t.id, op: "cancel" });
}}
>
<Square />
取消
</Button>
);
if (t.status === "failed" || t.status === "dead")
return (
<Button
variant="outline"
size={size}
disabled={action.isPending}
onClick={(e) => {
e.stopPropagation();
action.mutate({ id: t.id, op: "retry" });
}}
>
<RotateCcw />
重试
</Button>
);
return null;
}
export function TaskStatusLine({ t }: { t: Task }) {
return (
<span className="flex flex-wrap items-center gap-1.5">
<StatusBadge tone={TASK_STATUS[t.status].tone}>{TASK_STATUS[t.status].label}</StatusBadge>
{t.waitReason ? <span className="text-meta text-muted-foreground">{t.waitReason}</span> : null}
</span>
);
}
/** 概况 + 关联对象 */
export function TaskSummary({ t }: { t: Task }) {
const href = subjectLink(t);
const err = t.lastError
? String((t.lastError as { message?: unknown }).message ?? JSON.stringify(t.lastError))
: null;
return (
<div className="space-y-5">
{err ? (
<p className="rounded-lg border border-destructive/40 bg-destructive/10 px-3 py-2 text-caption break-words text-destructive">
{err}
</p>
) : null}
<DetailSection title="概况">
<KvList
items={[
{ label: "尝试", value: `${t.attempts}/${t.maxAttempts}` },
{ label: "触发", value: actorText(t.triggeredBy) },
{ label: "创建", value: formatDateTime(t.createdAt) },
{ label: "开始", value: formatDateTime(t.startedAt) },
{ label: "结束", value: formatDateTime(t.finishedAt) },
{ label: "下次执行", value: t.status === "pending" ? formatDateTime(t.nextRunAt) : null },
{ label: "取消原因", value: t.cancelReason, wide: true },
]}
/>
</DetailSection>
<DetailSection title="关联对象">
<KvList
items={[
{
label: "对象",
value: href ? (
<Link to={href} className="underline underline-offset-4">
{subjectText(t)}
</Link>
) : (
subjectText(t)
),
},
{
label: "上级任务",
value: t.parentTaskId ? (
<Link to={taskPath(t.parentTaskId)} className="font-mono underline underline-offset-4">
{t.parentTaskId.slice(0, 8)}
</Link>
) : null,
},
{
label: "引擎运行",
value: t.engineRunId ? (
<Link to={taskPath(t.engineRunId)} className="font-mono underline underline-offset-4">
{t.engineRunId.slice(0, 12)}
</Link>
) : null,
},
]}
/>
</DetailSection>
</div>
);
}