components/linyi/approval-card.tsx
lucide-reactmotion
"use client"; import * as React from "react";import { AnimatePresence, motion } from "motion/react";import { Check, ChevronDown, FilePenLine, Loader2, RotateCcw, ShieldAlert, Terminal, X } from "lucide-react";import { cn } from "@/lib/cn";import { SPRING_SMOOTH, SPRING_SNAPPY } from "@/lib/motion"; export type ApprovalRisk = "low" | "medium" | "high";export type ApprovalStatus = "pending" | "approved" | "approved_session" | "denied"; const RISK_META: Record<ApprovalRisk, { label: string; shortLabel: string; className: string }> = { low: { label: "低风险", shortLabel: "低", className: "bg-success-soft text-success border-success-border", }, medium: { label: "中等风险", shortLabel: "中等", className: "bg-warning-soft text-warning border-warning-border", }, high: { label: "高风险", shortLabel: "高", className: "bg-danger-soft text-danger border-danger-border", },}; export interface ApprovalCardProps { tool?: "run_command" | "write_file" | string; risk?: ApprovalRisk; command: string; description?: string; cwd?: string; args?: Record<string, string>; status?: ApprovalStatus; defaultStatus?: ApprovalStatus; loading?: boolean; allowReset?: boolean; onDecision?: (decision: Exclude<ApprovalStatus, "pending">) => void; onApprove?: () => void; onApproveSession?: () => void; onDeny?: () => void; onReset?: () => void; compact?: boolean; className?: string;} export function ApprovalCard({ tool = "run_command", risk = "medium", command, description, cwd, args, status: controlledStatus, defaultStatus = "pending", loading = false, allowReset = true, onDecision, onApprove, onApproveSession, onDeny, onReset, compact = false, className,}: ApprovalCardProps) { const [uncontrolledStatus, setUncontrolledStatus] = React.useState<ApprovalStatus>(defaultStatus); const [detailsOpen, setDetailsOpen] = React.useState(false); const status = controlledStatus ?? uncontrolledStatus; const riskInfo = RISK_META[risk]; const hasDetails = Boolean(!compact && (cwd || (args && Object.keys(args).length > 0))); const ToolIcon = tool === "write_file" ? FilePenLine : Terminal; const decide = (next: Exclude<ApprovalStatus, "pending">) => { if (loading) return; if (controlledStatus === undefined) setUncontrolledStatus(next); onDecision?.(next); if (next === "approved") onApprove?.(); else if (next === "approved_session") onApproveSession?.(); else if (next === "denied") onDeny?.(); }; const reset = () => { if (loading) return; if (controlledStatus === undefined) setUncontrolledStatus("pending"); onReset?.(); }; const statusSummary = status === "approved" ? { label: "已允许本次执行", tone: "text-success bg-success-soft border-success-border" } : status === "approved_session" ? { label: "本次会话始终允许", tone: "text-success bg-success-soft border-success-border" } : { label: "已拒绝该工具调用", tone: "text-danger bg-danger-soft border-danger-border" }; return ( <motion.div layout transition={SPRING_SNAPPY} data-status={status} className={cn( "w-full rounded-[14px] bg-surface border border-line-strong shadow-panel", compact ? "p-3 space-y-2.5" : "p-4 space-y-3", className )} > {/* Header row */} <div className="flex items-center justify-between gap-2"> <span className="inline-flex items-center gap-1.5 font-mono-num text-[11px] text-fg min-w-0"> <ToolIcon className="w-3.5 h-3.5 text-accent flex-shrink-0" aria-hidden="true" /> <span className="truncate">{tool}</span> </span> <span className={cn( "inline-flex items-center gap-1 px-2 py-0.5 rounded-pill border text-[10px] font-medium flex-shrink-0", riskInfo.className )} > <ShieldAlert className="w-2.5 h-2.5" aria-hidden="true" /> <span>{compact ? riskInfo.shortLabel : riskInfo.label}</span> </span> </div> {description && !compact && ( <p className="m-0 text-xs text-fg-secondary leading-relaxed">{description}</p> )} {/* Monospace command well */} <div className="rounded-control bg-well border border-line-well px-3 py-2 font-mono-num text-[11px] text-fg overflow-x-auto no-scrollbar"> <code className="whitespace-nowrap">{command}</code> </div> {/* Collapsible context details */} {hasDetails && ( <div> <button type="button" aria-expanded={detailsOpen} onClick={() => setDetailsOpen((o) => !o)} className="inline-flex items-center gap-1 text-[11px] text-fg-secondary hover:text-fg cursor-pointer transition-colors" > <ChevronDown className={cn("w-3.5 h-3.5 transition-transform duration-200", detailsOpen && "rotate-180")} aria-hidden="true" /> <span>执行参数与目录</span> </button> <AnimatePresence initial={false}> {detailsOpen && ( <motion.div initial={{ height: 0, opacity: 0 }} animate={{ height: "auto", opacity: 1 }} exit={{ height: 0, opacity: 0 }} transition={SPRING_SMOOTH} className="overflow-hidden" > <dl className="mt-2 rounded-control bg-surface-2 border border-line p-2.5 space-y-1.5 font-mono-num text-[11px]"> {cwd && ( <div className="flex items-center justify-between gap-3"> <dt className="text-fg-muted">cwd</dt> <dd className="text-fg-secondary truncate">{cwd}</dd> </div> )} {args && Object.entries(args).map(([k, v]) => ( <div key={k} className="flex items-center justify-between gap-3"> <dt className="text-fg-muted">{k}</dt> <dd className="text-fg-secondary truncate">{v}</dd> </div> ))} </dl> </motion.div> )} </AnimatePresence> </div> )} {/* Decision controls or result */} <AnimatePresence mode="wait" initial={false}> {status === "pending" ? ( <motion.div key="pending-actions" initial={{ opacity: 0, y: 4 }} animate={{ opacity: 1, y: 0 }} exit={{ opacity: 0, y: -4 }} transition={SPRING_SNAPPY} className="space-y-2" > <div className="flex items-center gap-2"> <button type="button" disabled={loading} onClick={() => decide("denied")} className="ui-btn ui-btn-default flex-1 h-8 px-3 text-xs disabled:opacity-45" > <X className="w-3.5 h-3.5" aria-hidden="true" /> <span>拒绝</span> </button> <button type="button" disabled={loading} onClick={() => decide("approved")} className="ui-btn ui-btn-accent flex-1 h-8 px-3 text-xs disabled:opacity-45" > {loading ? ( <Loader2 className="w-3.5 h-3.5 animate-spin" aria-hidden="true" /> ) : ( <Check className="w-3.5 h-3.5" aria-hidden="true" /> )} <span>允许</span> </button> </div> {!compact && ( <button type="button" disabled={loading} onClick={() => decide("approved_session")} className="w-full h-7 rounded-control bg-well/70 hover:bg-well border border-line text-[11px] text-fg-secondary hover:text-fg cursor-pointer transition-colors disabled:opacity-45" > 本次会话始终允许 </button> )} </motion.div> ) : ( <motion.div key="decided-status" role="status" aria-live="polite" initial={{ opacity: 0, scale: 0.97 }} animate={{ opacity: 1, scale: 1 }} exit={{ opacity: 0, scale: 0.97 }} transition={SPRING_SNAPPY} className="flex items-center justify-between gap-2" > <span className={cn( "inline-flex items-center gap-1.5 px-2.5 h-8 rounded-control border text-[11px] font-medium flex-1", statusSummary.tone )} > {status === "denied" ? ( <X className="w-3.5 h-3.5 flex-shrink-0" aria-hidden="true" /> ) : ( <Check className="w-3.5 h-3.5 flex-shrink-0" aria-hidden="true" /> )} <span className="truncate">{statusSummary.label}</span> </span> {allowReset && ( <button type="button" disabled={loading} onClick={reset} className="inline-flex items-center gap-1 h-8 px-2.5 rounded-control bg-well hover:bg-well/80 border border-line text-[11px] text-fg-secondary hover:text-fg cursor-pointer transition-colors flex-shrink-0" aria-label="重置审批状态" > <RotateCcw className="w-3 h-3" aria-hidden="true" /> <span>重置</span> </button> )} </motion.div> )} </AnimatePresence> </motion.div> );}