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 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 | /** * HandApprovalModal - Modal for approving/rejecting Hand executions * * Provides detailed view of Hand execution request with: * - Hand name and description * - Trigger parameters * - Expected output/impact * - Risk level indicator * - Approval timeout countdown * - Approve/Reject buttons */ import { useState, useEffect, useCallback, useMemo } from 'react'; import { X, AlertTriangle, CheckCircle, XCircle, Clock, Loader2, Shield, Zap, Info, } from 'lucide-react'; import type { HandRun } from '../store/gatewayStore'; import { HAND_DEFINITIONS, type HandId } from '../types/hands'; // === Types === export type RiskLevel = 'low' | 'medium' | 'high'; export interface HandApprovalData { runId: string; handId: HandId; handName: string; description: string; params: Record<string, unknown>; riskLevel: RiskLevel; expectedImpact?: string; requestedAt: string; requestedBy?: string; timeoutSeconds?: number; } interface HandApprovalModalProps { handRun: HandRun | null; isOpen: boolean; onApprove: (runId: string) => Promise<void>; onReject: (runId: string, reason: string) => Promise<void>; onClose: () => void; } // === Risk Level Config === const RISK_CONFIG: Record< RiskLevel, { label: string; color: string; bgColor: string; borderColor: string; icon: typeof AlertTriangle } > = { low: { label: 'Low Risk', color: 'text-green-600 dark:text-green-400', bgColor: 'bg-green-100 dark:bg-green-900/30', borderColor: 'border-green-300 dark:border-green-700', icon: CheckCircle, }, medium: { label: 'Medium Risk', color: 'text-yellow-600 dark:text-yellow-400', bgColor: 'bg-yellow-100 dark:bg-yellow-900/30', borderColor: 'border-yellow-300 dark:border-yellow-700', icon: AlertTriangle, }, high: { label: 'High Risk', color: 'text-red-600 dark:text-red-400', bgColor: 'bg-red-100 dark:bg-red-900/30', borderColor: 'border-red-300 dark:border-red-700', icon: AlertTriangle, }, }; // === Helper Functions === function calculateRiskLevel(handId: HandId, params: Record<string, unknown>): RiskLevel { // Risk assessment based on Hand type and parameters switch (handId) { case 'browser': // Browser automation can be high risk if interacting with sensitive sites const url = String(params.url || '').toLowerCase(); if (url.includes('bank') || url.includes('payment') || url.includes('admin')) { return 'high'; } if (params.headless === false) { return 'medium'; // Non-headless mode is more visible } return 'low'; case 'twitter': // Twitter actions can have public impact const action = String(params.action || ''); if (action === 'post' || action === 'engage') { return 'high'; // Public posting is high impact } return 'medium'; case 'collector': // Data collection depends on scope if (params.pagination === true) { return 'medium'; // Large scale collection } return 'low'; case 'lead': // Lead generation accesses external data return 'medium'; case 'clip': // Video processing is generally safe return 'low'; case 'predictor': // Predictions are read-only return 'low'; case 'researcher': // Research is generally safe const depth = String(params.depth || ''); return depth === 'deep' ? 'medium' : 'low'; default: return 'medium'; } } function getExpectedImpact(handId: HandId, params: Record<string, unknown>): string { switch (handId) { case 'browser': return `Will perform browser automation on ${params.url || 'specified URL'}`; case 'twitter': if (params.action === 'post') { return 'Will post content to Twitter/X publicly'; } if (params.action === 'engage') { return 'Will like/reply to tweets'; } return 'Will perform Twitter/X operations'; case 'collector': return `Will collect data from ${params.targetUrl || 'specified source'}`; case 'lead': return `Will search for leads from ${params.source || 'specified source'}`; case 'clip': return `Will process video: ${params.inputPath || 'specified input'}`; case 'predictor': return `Will run prediction on ${params.dataSource || 'specified data'}`; case 'researcher': return `Will conduct research on: ${params.topic || 'specified topic'}`; default: return 'Will execute Hand operation'; } } function formatTimeRemaining(seconds: number): string { if (seconds <= 0) return 'Expired'; if (seconds < 60) return `${seconds}s`; const minutes = Math.floor(seconds / 60); const secs = seconds % 60; return `${minutes}m ${secs}s`; } // === Countdown Timer Hook === function useCountdown(startedAt: string, timeoutSeconds: number = 300) { const [timeRemaining, setTimeRemaining] = useState(0); const [isExpired, setIsExpired] = useState(false); useEffect(() => { const started = new Date(startedAt).getTime(); const expiresAt = started + timeoutSeconds * 1000; const updateTimer = () => { const now = Date.now(); const remaining = Math.max(0, Math.floor((expiresAt - now) / 1000)); setTimeRemaining(remaining); setIsExpired(remaining <= 0); }; updateTimer(); const interval = setInterval(updateTimer, 1000); return () => clearInterval(interval); }, [startedAt, timeoutSeconds]); return { timeRemaining, isExpired }; } // === Sub-components === function RiskBadge({ level }: { level: RiskLevel }) { const config = RISK_CONFIG[level]; const Icon = config.icon; return ( <div className={`inline-flex items-center gap-1.5 px-2.5 py-1 rounded-md text-sm font-medium border ${config.bgColor} ${config.color} ${config.borderColor}`} > <Icon className="w-3.5 h-3.5" /> {config.label} </div> ); } function TimeoutProgress({ timeRemaining, totalSeconds }: { timeRemaining: number; totalSeconds: number }) { const percentage = Math.max(0, Math.min(100, (timeRemaining / totalSeconds) * 100)); const isUrgent = timeRemaining < 60; return ( <div className="space-y-1.5"> <div className="flex items-center justify-between text-xs"> <span className="text-gray-500 dark:text-gray-400 flex items-center gap-1"> <Clock className="w-3 h-3" /> Time Remaining </span> <span className={`font-medium ${isUrgent ? 'text-red-600 dark:text-red-400' : 'text-gray-700 dark:text-gray-300'}`} > {formatTimeRemaining(timeRemaining)} </span> </div> <div className="h-2 bg-gray-200 dark:bg-gray-700 rounded-full overflow-hidden"> <div className={`h-full transition-all duration-1000 ease-linear rounded-full ${ isUrgent ? 'bg-red-500' : 'bg-blue-500' }`} style={{ width: `${percentage}%` }} /> </div> </div> ); } function ParamsDisplay({ params }: { params: Record<string, unknown> }) { if (!params || Object.keys(params).length === 0) { return ( <p className="text-sm text-gray-500 dark:text-gray-400 italic">No parameters provided</p> ); } return ( <div className="bg-gray-50 dark:bg-gray-900 rounded-lg p-3 font-mono text-xs overflow-x-auto"> <pre className="text-gray-700 dark:text-gray-300"> {JSON.stringify(params, null, 2)} </pre> </div> ); } // === Main Component === export function HandApprovalModal({ handRun, isOpen, onApprove, onReject, onClose, }: HandApprovalModalProps) { const [isProcessing, setIsProcessing] = useState(false); const [showRejectInput, setShowRejectInput] = useState(false); const [rejectReason, setRejectReason] = useState(''); const [error, setError] = useState<string | null>(null); // Parse HandRun to get approval data const approvalData = useMemo((): HandApprovalData | null => { if (!handRun) return null; // Extract hand ID from run data (could be stored in result or need to be passed separately) const result = handRun.result as Record<string, unknown> | undefined; const handId = (result?.handId as HandId) || 'researcher'; // Default fallback const params = (result?.params as Record<string, unknown>) || {}; const handDef = HAND_DEFINITIONS.find((h) => h.id === handId); return { runId: handRun.runId, handId, handName: handDef?.name || handId, description: handDef?.description || 'Hand execution request', params, riskLevel: calculateRiskLevel(handId, params), expectedImpact: getExpectedImpact(handId, params), requestedAt: handRun.startedAt, timeoutSeconds: 300, // 5 minutes default timeout }; }, [handRun]); // Timer countdown const { timeRemaining, isExpired } = useCountdown( approvalData?.requestedAt || new Date().toISOString(), approvalData?.timeoutSeconds || 300 ); // Handle keyboard escape useEffect(() => { const handleEscape = (e: KeyboardEvent) => { if (e.key === 'Escape' && isOpen) { onClose(); } }; window.addEventListener('keydown', handleEscape); return () => window.removeEventListener('keydown', handleEscape); }, [isOpen, onClose]); // Reset state when modal opens/closes useEffect(() => { if (isOpen) { setShowRejectInput(false); setRejectReason(''); setError(null); setIsProcessing(false); } }, [isOpen]); const handleApprove = useCallback(async () => { if (!approvalData || isProcessing || isExpired) return; setIsProcessing(true); setError(null); try { await onApprove(approvalData.runId); onClose(); } catch (err) { setError(err instanceof Error ? err.message : 'Failed to approve'); } finally { setIsProcessing(false); } }, [approvalData, isProcessing, isExpired, onApprove, onClose]); const handleReject = useCallback(async () => { if (!approvalData || isProcessing) return; if (!showRejectInput) { setShowRejectInput(true); return; } if (!rejectReason.trim()) { setError('Please provide a reason for rejection'); return; } setIsProcessing(true); setError(null); try { await onReject(approvalData.runId, rejectReason.trim()); onClose(); } catch (err) { setError(err instanceof Error ? err.message : 'Failed to reject'); } finally { setIsProcessing(false); } }, [approvalData, isProcessing, showRejectInput, rejectReason, onReject, onClose]); const handleCancelReject = useCallback(() => { setShowRejectInput(false); setRejectReason(''); setError(null); }, []); if (!isOpen || !approvalData) return null; return ( <div className="fixed inset-0 z-50 flex items-center justify-center"> {/* Backdrop */} <div className="absolute inset-0 bg-black/50 backdrop-blur-sm" onClick={onClose} /> {/* Modal */} <div className="relative bg-white dark:bg-gray-800 rounded-xl shadow-2xl w-full max-w-lg mx-4 max-h-[90vh] overflow-hidden flex flex-col"> {/* Header */} <div className="flex items-start justify-between p-4 border-b border-gray-200 dark:border-gray-700 flex-shrink-0"> <div className="flex items-center gap-3"> <div className="w-10 h-10 bg-amber-100 dark:bg-amber-900/30 rounded-lg flex items-center justify-center"> <Shield className="w-5 h-5 text-amber-600 dark:text-amber-400" /> </div> <div> <h2 className="text-lg font-semibold text-gray-900 dark:text-white"> Hand Approval Request </h2> <p className="text-xs text-gray-500 dark:text-gray-400"> Review and approve Hand execution </p> </div> </div> <button onClick={onClose} className="p-1 text-gray-400 hover:text-gray-600 dark:hover:text-gray-300 transition-colors" > <X className="w-5 h-5" /> </button> </div> {/* Content */} <div className="flex-1 overflow-y-auto p-4 space-y-4"> {/* Expired Warning */} {isExpired && ( <div className="flex items-center gap-2 p-3 bg-gray-100 dark:bg-gray-900 rounded-lg text-gray-600 dark:text-gray-400"> <Clock className="w-5 h-5 flex-shrink-0" /> <span className="text-sm">This approval request has expired</span> </div> )} {/* Hand Info */} <div className="space-y-3"> <div className="flex items-center justify-between"> <div className="flex items-center gap-2"> <Zap className="w-4 h-4 text-amber-500" /> <h3 className="font-medium text-gray-900 dark:text-white"> {approvalData.handName} </h3> </div> <RiskBadge level={approvalData.riskLevel} /> </div> <p className="text-sm text-gray-600 dark:text-gray-400"> {approvalData.description} </p> </div> {/* Timeout Progress */} {!isExpired && ( <TimeoutProgress timeRemaining={timeRemaining} totalSeconds={approvalData.timeoutSeconds || 300} /> )} {/* Parameters */} <div className="space-y-2"> <label className="block text-sm font-medium text-gray-700 dark:text-gray-300"> Execution Parameters </label> <ParamsDisplay params={approvalData.params} /> </div> {/* Expected Impact */} {approvalData.expectedImpact && ( <div className="space-y-2"> <label className="block text-sm font-medium text-gray-700 dark:text-gray-300 flex items-center gap-1"> <Info className="w-3.5 h-3.5" /> Expected Impact </label> <p className="text-sm text-gray-600 dark:text-gray-400 bg-blue-50 dark:bg-blue-900/20 p-3 rounded-lg"> {approvalData.expectedImpact} </p> </div> )} {/* Request Info */} <div className="text-xs text-gray-500 dark:text-gray-400 space-y-1 pt-2 border-t border-gray-200 dark:border-gray-700"> <p>Run ID: {approvalData.runId}</p> <p> Requested: {new Date(approvalData.requestedAt).toLocaleString()} </p> </div> {/* Reject Input */} {showRejectInput && ( <div className="space-y-2"> <label className="block text-sm font-medium text-gray-700 dark:text-gray-300"> Rejection Reason <span className="text-red-500">*</span> </label> <textarea value={rejectReason} onChange={(e) => setRejectReason(e.target.value)} placeholder="Please provide a reason for rejecting this request..." className="w-full px-3 py-2 text-sm border border-gray-200 dark:border-gray-600 rounded-md bg-white dark:bg-gray-900 text-gray-900 dark:text-white focus:outline-none focus:ring-2 focus:ring-red-500" rows={3} autoFocus disabled={isProcessing} /> </div> )} {/* Error Message */} {error && ( <div className="flex items-center gap-2 p-3 bg-red-50 dark:bg-red-900/20 rounded-lg text-red-700 dark:text-red-400"> <AlertTriangle className="w-4 h-4 flex-shrink-0" /> <span className="text-sm">{error}</span> </div> )} </div> {/* Footer */} <div className="flex items-center justify-end gap-2 p-4 border-t border-gray-200 dark:border-gray-700 flex-shrink-0"> {showRejectInput ? ( <> <button type="button" onClick={handleCancelReject} disabled={isProcessing} className="px-4 py-2 text-sm text-gray-700 dark:text-gray-300 bg-gray-100 dark:bg-gray-700 rounded-lg hover:bg-gray-200 dark:hover:bg-gray-600 transition-colors disabled:opacity-50" > Cancel </button> <button type="button" onClick={handleReject} disabled={isProcessing || !rejectReason.trim()} className="px-4 py-2 text-sm text-white bg-red-600 rounded-lg hover:bg-red-700 transition-colors disabled:opacity-50 flex items-center gap-2" > {isProcessing ? ( <> <Loader2 className="w-4 h-4 animate-spin" /> Rejecting... </> ) : ( <> <XCircle className="w-4 h-4" /> Confirm Rejection </> )} </button> </> ) : ( <> <button type="button" onClick={onClose} disabled={isProcessing} className="px-4 py-2 text-sm text-gray-700 dark:text-gray-300 bg-gray-100 dark:bg-gray-700 rounded-lg hover:bg-gray-200 dark:hover:bg-gray-600 transition-colors disabled:opacity-50" > Close </button> <button type="button" onClick={handleReject} disabled={isProcessing || isExpired} className="px-4 py-2 text-sm border border-red-200 dark:border-red-800 text-red-600 dark:text-red-400 rounded-lg hover:bg-red-50 dark:hover:bg-red-900/20 transition-colors disabled:opacity-50 flex items-center gap-2" > <XCircle className="w-4 h-4" /> Reject </button> <button type="button" onClick={handleApprove} disabled={isProcessing || isExpired} className="px-4 py-2 text-sm text-white bg-green-600 rounded-lg hover:bg-green-700 transition-colors disabled:opacity-50 flex items-center gap-2" > {isProcessing ? ( <> <Loader2 className="w-4 h-4 animate-spin" /> Approving... </> ) : ( <> <CheckCircle className="w-4 h-4" /> Approve </> )} </button> </> )} </div> </div> </div> ); } export default HandApprovalModal; |