components/linyi/todo-list.tsx
lucide-reactmotion
"use client"; import * as React from "react";import { motion } from "motion/react";import { Check, Circle, Loader2 } from "lucide-react";import { cn } from "@/lib/cn";import { SPRING_BOUNCY, SPRING_PROGRESS } from "@/lib/motion"; export type AgentTodoStatus = "done" | "in_progress" | "pending"; export interface AgentTodoItem { id: string; label: string; status: AgentTodoStatus; detail?: string;} export interface AgentTodoListProps { items?: AgentTodoItem[]; defaultItems?: AgentTodoItem[]; onItemsChange?: (items: AgentTodoItem[]) => void; title?: string; compact?: boolean; className?: string;} const NEXT_STATUS: Record<AgentTodoStatus, AgentTodoStatus> = { pending: "in_progress", in_progress: "done", done: "pending",}; const STATUS_LABEL: Record<AgentTodoStatus, string> = { done: "已完成", in_progress: "进行中", pending: "待执行",}; const FALLBACK_ITEMS: AgentTodoItem[] = [ { id: "1", label: "读取设计规范与令牌", status: "done" }, { id: "2", label: "生成独立注册表组件", status: "in_progress" }, { id: "3", label: "校验深浅主题对比度", status: "pending" },]; export function AgentTodoList({ items: controlledItems, defaultItems = FALLBACK_ITEMS, onItemsChange, title = "执行计划", compact = false, className,}: AgentTodoListProps) { const [uncontrolledItems, setUncontrolledItems] = React.useState<AgentTodoItem[]>(defaultItems); const items = controlledItems ?? uncontrolledItems; const doneCount = items.filter((i) => i.status === "done").length; const ratio = items.length > 0 ? doneCount / items.length : 0; const cycleItem = (id: string) => { const next = items.map((item) => item.id === id ? { ...item, status: NEXT_STATUS[item.status] } : item ); if (controlledItems === undefined) setUncontrolledItems(next); onItemsChange?.(next); }; return ( <div className={cn( "w-full rounded-[14px] bg-surface border border-line shadow-panel", compact ? "p-2.5 space-y-2" : "p-3.5 space-y-3", className )} > {/* Progress header */} <div className="space-y-1.5"> <div className="flex items-center justify-between gap-2 px-0.5"> <span className="text-[11px] font-medium text-fg">{title}</span> <span className="font-mono-num text-[10px] text-fg-muted"> {doneCount}/{items.length} </span> </div> <div role="progressbar" aria-valuemin={0} aria-valuemax={100} aria-valuenow={Math.round(ratio * 100)} aria-label={`${title}完成进度`} className="h-1.5 w-full rounded-pill bg-well overflow-hidden" > <motion.div initial={false} animate={{ scaleX: ratio }} transition={SPRING_PROGRESS} className="h-full w-full origin-left rounded-pill bg-accent" /> </div> </div> {/* Checklist */} <ul className="space-y-0.5 m-0 p-0 list-none"> {items.map((item) => { const isDone = item.status === "done"; const isRunning = item.status === "in_progress"; return ( <li key={item.id}> <button type="button" onClick={() => cycleItem(item.id)} aria-label={`${item.label},当前状态:${STATUS_LABEL[item.status]},点击切换`} className={cn( "w-full rounded-[9px] flex items-start gap-2.5 text-left cursor-pointer transition-colors hover:bg-well/70", compact ? "px-2 py-1.5 text-[11px]" : "px-2.5 py-2 text-xs" )} > <motion.span initial={false} animate={{ scale: isDone ? 1 : 0.94 }} transition={SPRING_BOUNCY} className={cn( "mt-[1px] w-4 h-4 rounded-[5px] flex items-center justify-center flex-shrink-0 border transition-colors", isDone ? "bg-accent border-accent text-accent-fg" : isRunning ? "bg-accent-soft border-accent-border text-accent" : "bg-well border-line-strong text-fg-muted" )} > {isDone ? ( <motion.span initial={{ scale: 0.5, opacity: 0 }} animate={{ scale: 1, opacity: 1 }} transition={SPRING_BOUNCY} className="flex items-center justify-center" > <Check className="w-2.5 h-2.5" aria-hidden="true" /> </motion.span> ) : isRunning ? ( <Loader2 className="w-2.5 h-2.5 animate-spin" aria-hidden="true" /> ) : ( <Circle className="w-2 h-2 opacity-40" aria-hidden="true" /> )} </motion.span> <span className="min-w-0 flex-1"> <span className={cn( "block leading-snug transition-opacity", isDone ? "line-through text-fg-muted" : "text-fg" )} > {item.label} </span> {!compact && item.detail && ( <span className="block text-[11px] text-fg-muted mt-0.5 truncate">{item.detail}</span> )} </span> {!compact && ( <span className={cn( "font-mono-num text-[10px] px-1.5 py-0.5 rounded-badge border flex-shrink-0", isDone ? "bg-success-soft text-success border-success-border" : isRunning ? "bg-accent-soft text-accent border-accent-border" : "bg-well text-fg-muted border-line" )} > {STATUS_LABEL[item.status]} </span> )} </button> </li> ); })} </ul> </div> );}