ClassyGitshadowzzh

JobCard.tsx

JobCard.tsx
import { useState } from "react"
import { AudioLines, Check, ChevronDown, Download, Ellipsis, Heart, LoaderCircle, RotateCcw, Square, Trash2, Undo2 } from "lucide-react"
import { Badge } from "@/components/ui/badge"
import { Button } from "@/components/ui/button"
import { DropdownMenu, DropdownMenuContent, DropdownMenuItem, DropdownMenuSeparator, DropdownMenuTrigger } from "@/components/ui/dropdown-menu"
import { Collapsible, CollapsibleContent, CollapsibleTrigger } from "@/components/ui/collapsible"
import { AudioPlayer } from "@/components/AudioPlayer"
import { activeMessage, canCancel, dateTime, duration, isActive, languageLabels, statusLabel, taskLabels, timingsText } from "@/lib/format"
import { cn } from "@/lib/utils"
import type { Job } from "@/types"
export interface JobCallbacks {
  onRestore: (job: Job) => void; onRetry: (job: Job) => void; onFavorite: (job: Job) => void;
  onDelete: (job: Job) => void; onCancel: (job: Job) => void; onRename?: (job: Job) => void;
}
export function JobCard({ job, compact = false, busy = false, ...callbacks }: JobCallbacks & { job: Job; compact?: boolean; busy?: boolean }) {
  const [open, setOpen] = useState(false)
  const active = isActive(job)
  const cancellable = canCancel(job)
  const failed = job.status === "failed"
  return <article className={cn("min-w-0 rounded-xl border bg-card", active && "border-primary/25", !compact && "shadow-xs")}>
    <div className={cn("flex items-start gap-3", compact ? "p-4" : "p-5")}>
      <div className={cn("flex size-10 shrink-0 items-center justify-center rounded-xl", active ? "bg-primary/10 text-primary" : failed ? "bg-destructive/8 text-destructive" : "bg-muted text-muted-foreground")}>{active ? <LoaderCircle className={cn("size-4", job.status === "running" && "animate-spin")} /> : <AudioLines className="size-4" />}</div>
      <div className="min-w-0 flex-1"><div className="flex flex-wrap items-center gap-2"><h3 className="max-w-full truncate text-sm font-medium">{job.title || "未命名音频"}</h3><Badge variant="secondary" className={cn("rounded-md px-1.5 text-[10px] font-normal", active && "bg-primary/8 text-primary", failed && "bg-destructive/8 text-destructive")}>{statusLabel(job)}</Badge></div><p className="mt-1.5 line-clamp-2 break-words text-xs leading-relaxed text-muted-foreground">{job.text}</p><p className="mt-2 flex flex-wrap items-center gap-x-2 gap-y-1 text-[10px] text-muted-foreground"><span>{dateTime(job.created_at)}</span><span>·</span><span>{job.voice_name || taskLabels[job.parameters.task] || "自定义声音"}</span>{job.duration_seconds != null && <><span>·</span><span>{duration(job.duration_seconds)}</span></>}</p></div>
      <div className="flex shrink-0 items-center gap-0.5"><Button type="button" size="icon-sm" variant="ghost" disabled={busy} aria-label={job.favorite ? "取消收藏音频" : "收藏音频"} aria-pressed={job.favorite} onClick={() => callbacks.onFavorite(job)}><Heart className={cn("size-3.5", job.favorite && "fill-primary text-primary")} /></Button><DropdownMenu><DropdownMenuTrigger asChild><Button type="button" size="icon-sm" variant="ghost" aria-label="音频更多操作" disabled={busy}><Ellipsis className="size-4" /></Button></DropdownMenuTrigger><DropdownMenuContent align="end"><DropdownMenuItem onSelect={() => callbacks.onRestore(job)}><Undo2 />恢复到工作台</DropdownMenuItem>{callbacks.onRename && <DropdownMenuItem onSelect={() => callbacks.onRename?.(job)}><Check />修改标题</DropdownMenuItem>}{failed && <DropdownMenuItem onSelect={() => callbacks.onRetry(job)}><RotateCcw />重新生成</DropdownMenuItem>}{cancellable && <DropdownMenuItem onSelect={() => callbacks.onCancel(job)}><Square />{job.status === "queued" ? "取消排队" : "停止生成"}</DropdownMenuItem>}{!active && <><DropdownMenuSeparator /><DropdownMenuItem className="text-destructive focus:text-destructive" onSelect={() => callbacks.onDelete(job)}><Trash2 />删除记录</DropdownMenuItem></>}</DropdownMenuContent></DropdownMenu></div>
    </div>
    {active && <div className="mx-4 mb-4 rounded-lg bg-primary/5 px-3 py-2.5"><p role="status" className="text-xs leading-relaxed text-primary">{activeMessage(job)}</p>{job.phase === "cancelling" ? <p className="mt-1 text-[11px] text-muted-foreground">已请求停止,正在等待确认。</p> : <div className="mt-1 flex flex-wrap items-center gap-x-3 gap-y-1"><p className="text-[11px] text-muted-foreground">可切换页面等待,后台会继续执行。</p>{cancellable && <Button type="button" size="sm" variant="link" className="h-auto px-0 text-xs" disabled={busy} onClick={() => callbacks.onCancel(job)}>{job.status === "queued" ? "取消排队" : "停止生成"}</Button>}</div>}</div>}
    {job.error && <p role="alert" className="mx-4 mb-4 break-words rounded-lg border border-destructive/15 bg-destructive/5 p-3 text-xs leading-relaxed text-destructive">{job.error}</p>}
    {job.audio_url && <div className="space-y-2 px-4 pb-4"><AudioPlayer key={job.audio_url} src={job.audio_url} label={`试听:${job.title || "生成音频"}`} /><div className="flex items-center justify-between gap-2"><span className="text-[10px] text-muted-foreground">{job.sample_rate ? `${(job.sample_rate / 1000).toLocaleString("zh-CN")} kHz · WAV` : "WAV 音频"}{job.elapsed_seconds != null ? ` · 生成耗时 ${job.elapsed_seconds.toFixed(1)} 秒` : ""}{timingsText(job) ? ` · ${timingsText(job)}` : ""}</span>{job.download_url && <Button asChild variant="ghost" size="sm" className="h-7 text-xs"><a href={job.download_url} download><Download className="size-3" />下载</a></Button>}</div></div>}
    {!compact && <Collapsible open={open} onOpenChange={setOpen} className="border-t"><CollapsibleTrigger asChild><Button type="button" variant="ghost" className="h-9 w-full justify-between rounded-t-none px-5 text-xs text-muted-foreground">文本与参数<ChevronDown className={cn("size-3.5 transition-transform", open && "rotate-180")} /></Button></CollapsibleTrigger><CollapsibleContent className="space-y-3 border-t px-5 py-4"><p className="whitespace-pre-wrap break-words text-sm leading-relaxed">{job.text}</p><dl className="grid grid-cols-2 gap-x-5 gap-y-2 text-xs"><dt className="text-muted-foreground">生成模式</dt><dd>{taskLabels[job.parameters.task]}</dd><dt className="text-muted-foreground">语言</dt><dd>{languageLabels[job.parameters.language_id ?? ""] ?? "自动识别"}</dd><dt className="text-muted-foreground">随机种子</dt><dd>{job.parameters.seed ?? "模型默认"}</dd>{job.parameters.style && <><dt className="text-muted-foreground">声音描述</dt><dd className="break-words">{job.parameters.style}</dd></>}{job.parameters.instruction && <><dt className="text-muted-foreground">演绎指令</dt><dd className="break-words">{job.parameters.instruction}</dd></>}</dl><Button type="button" variant="outline" size="sm" onClick={() => callbacks.onRestore(job)}><Undo2 className="size-3" />使用这些参数继续创作</Button></CollapsibleContent></Collapsible>}
  </article>
}