Press n or j to go to the next uncovered block, b, p or k for the previous block.
| 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 | /** * ApprovalQueue - Approval Management Component * * Displays pending approvals for hand executions that require * human approval, with approve/reject actions. * * @module components/Automation/ApprovalQueue */ import { useState, useEffect, useCallback } from 'react'; import { useHandStore } from '../../store/handStore'; import type { Approval, ApprovalStatus } from '../../store/handStore'; import { Clock, CheckCircle, XCircle, AlertTriangle, RefreshCw, } from 'lucide-react'; import { useToast } from '../ui/Toast'; // === Status Config === const STATUS_CONFIG: Record<ApprovalStatus, { label: string; className: string; icon: typeof CheckCircle; }> = { pending: { label: '待处理', className: 'bg-yellow-100 text-yellow-700 dark:bg-yellow-900/30 dark:text-yellow-400', icon: Clock, }, approved: { label: '已批准', className: 'bg-green-100 text-green-700 dark:bg-green-900/30 dark:text-green-400', icon: CheckCircle, }, rejected: { label: '已拒绝', className: 'bg-red-100 text-red-700 dark:bg-red-900/30 dark:text-red-400', icon: XCircle, }, expired: { label: '已过期', className: 'bg-gray-100 text-gray-500 dark:bg-gray-800 dark:text-gray-400', icon: AlertTriangle, }, }; // === Component Props === interface ApprovalQueueProps { showFilters?: boolean; maxHeight?: string; onApprove?: (approval: Approval) => void; onReject?: (approval: Approval) => void; } // === Approval Card Component === interface ApprovalCardProps { approval: Approval; onApprove: () => Promise<void>; onReject: (reason: string) => Promise<void>; isProcessing: boolean; } function ApprovalCard({ approval, onApprove, onReject, isProcessing }: ApprovalCardProps) { const [showRejectInput, setShowRejectInput] = useState(false); const [rejectReason, setRejectReason] = useState(''); const StatusIcon = STATUS_CONFIG[approval.status].icon; const handleReject = useCallback(async () => { if (!rejectReason.trim()) { setShowRejectInput(true); return; } await onReject(rejectReason); setShowRejectInput(false); setRejectReason(''); }, [rejectReason, onReject]); const timeAgo = useCallback((dateStr: string) => { const date = new Date(dateStr); const now = new Date(); const diffMs = now.getTime() - date.getTime(); const diffMins = Math.floor(diffMs / 60000); const diffHours = Math.floor(diffMins / 60); const diffDays = Math.floor(diffHours / 24); if (diffMins < 1) return '刚刚'; if (diffMins < 60) return `${diffMins} 分钟前`; if (diffHours < 24) return `${diffHours} 小时前`; return `${diffDays} 天前`; }, []); return ( <div className="bg-white dark:bg-gray-800 rounded-lg border border-gray-200 dark:border-gray-700 p-4"> {/* Header */} <div className="flex items-start justify-between gap-3 mb-3"> <div className="flex items-center gap-2"> <span className={`px-2 py-0.5 rounded text-xs ${STATUS_CONFIG[approval.status].className}`}> <StatusIcon className="w-3 h-3 inline mr-1" /> {STATUS_CONFIG[approval.status].label} </span> <span className="text-xs text-gray-500 dark:text-gray-400"> {timeAgo(approval.requestedAt)} </span> </div> </div> {/* Content */} <div className="mb-3"> <h4 className="font-medium text-gray-900 dark:text-white mb-1"> {approval.handName} </h4> {approval.reason && ( <p className="text-sm text-gray-600 dark:text-gray-400">{approval.reason}</p> )} {approval.action && ( <p className="text-xs text-gray-500 dark:text-gray-500 mt-1"> 操作: {approval.action} </p> )} </div> {/* Params Preview */} {approval.params && Object.keys(approval.params).length > 0 && ( <div className="mb-3 p-2 bg-gray-50 dark:bg-gray-900 rounded text-xs"> <p className="text-gray-500 dark:text-gray-400 mb-1">参数:</p> <pre className="text-gray-700 dark:text-gray-300 overflow-x-auto"> {JSON.stringify(approval.params, null, 2)} </pre> </div> )} {/* Reject Input */} {showRejectInput && ( <div className="mb-3"> <textarea value={rejectReason} onChange={(e) => setRejectReason(e.target.value)} placeholder="请输入拒绝原因..." className="w-full px-3 py-2 text-sm border border-gray-200 dark:border-gray-700 rounded-lg bg-white dark:bg-gray-800 text-gray-900 dark:text-white resize-none" rows={2} /> </div> )} {/* Actions */} {approval.status === 'pending' && ( <div className="flex items-center gap-2"> <button onClick={onApprove} disabled={isProcessing} className="flex-1 px-3 py-1.5 text-sm bg-green-500 text-white rounded-md hover:bg-green-600 disabled:opacity-50 disabled:cursor-not-allowed flex items-center justify-center gap-1" > {isProcessing ? ( <RefreshCw className="w-3.5 h-3.5 animate-spin" /> ) : ( <CheckCircle className="w-3.5 h-3.5" /> )} 批准 </button> <button onClick={handleReject} disabled={isProcessing} className="flex-1 px-3 py-1.5 text-sm bg-red-500 text-white rounded-md hover:bg-red-600 disabled:opacity-50 disabled:cursor-not-allowed flex items-center justify-center gap-1" > {isProcessing ? ( <RefreshCw className="w-3.5 h-3.5 animate-spin" /> ) : ( <XCircle className="w-3.5 h-3.5" /> )} 拒绝 </button> </div> )} {/* Response Info */} {approval.status !== 'pending' && approval.respondedAt && ( <div className="text-xs text-gray-500 dark:text-gray-400"> {approval.respondedBy && `由 ${approval.respondedBy} `} {STATUS_CONFIG[approval.status].label} {approval.responseReason && ` - ${approval.responseReason}`} </div> )} </div> ); } // === Main Component === export function ApprovalQueue({ showFilters = true, maxHeight = '400px', onApprove, onReject, }: ApprovalQueueProps) { const { toast } = useToast(); // Store state const approvals = useHandStore(s => s.approvals); const loadApprovals = useHandStore(s => s.loadApprovals); const respondToApproval = useHandStore(s => s.respondToApproval); const isLoading = useHandStore(s => s.isLoading); // Local state const [statusFilter, setStatusFilter] = useState<ApprovalStatus | 'all'>('pending'); const [processingIds, setProcessingIds] = useState<Set<string>>(new Set()); // Load approvals on mount useEffect(() => { loadApprovals(statusFilter === 'all' ? undefined : statusFilter); }, [loadApprovals, statusFilter]); // Handle approve const handleApprove = useCallback(async (approval: Approval) => { setProcessingIds(prev => new Set(prev).add(approval.id)); try { await respondToApproval(approval.id, true); toast(`已批准: ${approval.handName}`, 'success'); onApprove?.(approval); } catch (err) { const errorMsg = err instanceof Error ? err.message : String(err); toast(`批准失败: ${errorMsg}`, 'error'); } finally { setProcessingIds(prev => { const next = new Set(prev); next.delete(approval.id); return next; }); } }, [respondToApproval, toast, onApprove]); // Handle reject const handleReject = useCallback(async (approval: Approval, reason: string) => { setProcessingIds(prev => new Set(prev).add(approval.id)); try { await respondToApproval(approval.id, false, reason); toast(`已拒绝: ${approval.handName}`, 'success'); onReject?.(approval); } catch (err) { const errorMsg = err instanceof Error ? err.message : String(err); toast(`拒绝失败: ${errorMsg}`, 'error'); } finally { setProcessingIds(prev => { const next = new Set(prev); next.delete(approval.id); return next; }); } }, [respondToApproval, toast, onReject]); // Filter approvals const filteredApprovals = statusFilter === 'all' ? approvals : approvals.filter(a => a.status === statusFilter); // Stats const stats = { pending: approvals.filter(a => a.status === 'pending').length, approved: approvals.filter(a => a.status === 'approved').length, rejected: approvals.filter(a => a.status === 'rejected').length, expired: approvals.filter(a => a.status === 'expired').length, }; return ( <div className="flex flex-col h-full"> {/* Header */} <div className="flex items-center justify-between px-4 py-3 border-b border-gray-200 dark:border-gray-700"> <div className="flex items-center gap-2"> <Clock className="w-5 h-5 text-orange-500" /> <h2 className="text-lg font-semibold text-gray-900 dark:text-white"> 审批队列 </h2> {stats.pending > 0 && ( <span className="px-2 py-0.5 text-xs font-medium bg-orange-100 text-orange-700 dark:bg-orange-900/30 dark:text-orange-400 rounded-full"> {stats.pending} 待处理 </span> )} </div> <button onClick={() => loadApprovals(statusFilter === 'all' ? undefined : statusFilter)} disabled={isLoading} className="p-2 text-gray-500 hover:text-gray-700 dark:text-gray-400 dark:hover:text-gray-300 disabled:opacity-50" title="刷新" > <RefreshCw className={`w-4 h-4 ${isLoading ? 'animate-spin' : ''}`} /> </button> </div> {/* Filters */} {showFilters && ( <div className="flex items-center gap-2 px-4 py-2 border-b border-gray-200 dark:border-gray-700 overflow-x-auto"> {[ { value: 'pending', label: '待处理', count: stats.pending }, { value: 'approved', label: '已批准', count: stats.approved }, { value: 'rejected', label: '已拒绝', count: stats.rejected }, { value: 'all', label: '全部', count: approvals.length }, ].map(option => ( <button key={option.value} onClick={() => setStatusFilter(option.value as ApprovalStatus | 'all')} className={`flex items-center gap-1 px-3 py-1 text-sm rounded-full whitespace-nowrap transition-colors ${ statusFilter === option.value ? 'bg-orange-500 text-white' : 'bg-gray-100 dark:bg-gray-800 text-gray-700 dark:text-gray-300 hover:bg-gray-200 dark:hover:bg-gray-700' }`} > {option.label} <span className={`text-xs ${statusFilter === option.value ? 'text-white/80' : 'text-gray-500 dark:text-gray-400'}`}> ({option.count}) </span> </button> ))} </div> )} {/* Content */} <div className="flex-1 overflow-y-auto p-4" style={{ maxHeight }}> {isLoading && approvals.length === 0 ? ( <div className="flex items-center justify-center h-32"> <RefreshCw className="w-6 h-6 animate-spin text-gray-400" /> </div> ) : filteredApprovals.length === 0 ? ( <div className="flex flex-col items-center justify-center h-32 text-center"> <Clock className="w-8 h-8 text-gray-400 mb-2" /> <p className="text-sm text-gray-500 dark:text-gray-400"> {statusFilter === 'pending' ? '暂无待处理的审批' : '暂无审批记录'} </p> </div> ) : ( <div className="space-y-3"> {filteredApprovals.map(approval => ( <ApprovalCard key={approval.id} approval={approval} onApprove={() => handleApprove(approval)} onReject={(reason) => handleReject(approval, reason)} isProcessing={processingIds.has(approval.id)} /> ))} </div> )} </div> </div> ); } export default ApprovalQueue; |