|
| 1 | +// ======================================== |
| 2 | +// FindingDrawer Component |
| 3 | +// ======================================== |
| 4 | +// Right-side finding detail drawer for displaying discovery finding details |
| 5 | + |
| 6 | +import { useEffect } from 'react'; |
| 7 | +import { useIntl } from 'react-intl'; |
| 8 | +import { X, FileText, AlertTriangle, ExternalLink, MapPin, Code, Lightbulb, Target } from 'lucide-react'; |
| 9 | +import { Badge } from '@/components/ui/Badge'; |
| 10 | +import { Button } from '@/components/ui/Button'; |
| 11 | +import { cn } from '@/lib/utils'; |
| 12 | +import type { Finding } from '@/lib/api'; |
| 13 | + |
| 14 | +// ========== Types ========== |
| 15 | +export interface FindingDrawerProps { |
| 16 | + finding: Finding | null; |
| 17 | + isOpen: boolean; |
| 18 | + onClose: () => void; |
| 19 | +} |
| 20 | + |
| 21 | +// ========== Severity Configuration ========== |
| 22 | +const severityConfig: Record<string, { label: string; variant: 'default' | 'secondary' | 'destructive' | 'outline' | 'success' | 'warning' | 'info' }> = { |
| 23 | + critical: { label: 'issues.discovery.findings.severity.critical', variant: 'destructive' }, |
| 24 | + high: { label: 'issues.discovery.findings.severity.high', variant: 'destructive' }, |
| 25 | + medium: { label: 'issues.discovery.findings.severity.medium', variant: 'warning' }, |
| 26 | + low: { label: 'issues.discovery.findings.severity.low', variant: 'secondary' }, |
| 27 | +}; |
| 28 | + |
| 29 | +function getSeverityConfig(severity: string) { |
| 30 | + return severityConfig[severity] || { label: 'issues.discovery.findings.severity.unknown', variant: 'outline' }; |
| 31 | +} |
| 32 | + |
| 33 | +// ========== Component ========== |
| 34 | + |
| 35 | +export function FindingDrawer({ finding, isOpen, onClose }: FindingDrawerProps) { |
| 36 | + const { formatMessage } = useIntl(); |
| 37 | + |
| 38 | + // ESC key to close |
| 39 | + useEffect(() => { |
| 40 | + if (!isOpen) return; |
| 41 | + const handleEsc = (e: KeyboardEvent) => { |
| 42 | + if (e.key === 'Escape') onClose(); |
| 43 | + }; |
| 44 | + window.addEventListener('keydown', handleEsc); |
| 45 | + return () => window.removeEventListener('keydown', handleEsc); |
| 46 | + }, [isOpen, onClose]); |
| 47 | + |
| 48 | + if (!finding || !isOpen) { |
| 49 | + return null; |
| 50 | + } |
| 51 | + |
| 52 | + const severity = getSeverityConfig(finding.severity); |
| 53 | + |
| 54 | + return ( |
| 55 | + <> |
| 56 | + {/* Overlay */} |
| 57 | + <div |
| 58 | + className={cn( |
| 59 | + 'fixed inset-0 bg-black/40 transition-opacity z-40', |
| 60 | + isOpen ? 'opacity-100' : 'opacity-0 pointer-events-none' |
| 61 | + )} |
| 62 | + onClick={onClose} |
| 63 | + aria-hidden="true" |
| 64 | + /> |
| 65 | + |
| 66 | + {/* Drawer */} |
| 67 | + <div |
| 68 | + className={cn( |
| 69 | + 'fixed top-0 right-0 h-full w-1/2 bg-background border-l border-border shadow-2xl z-50 flex flex-col transition-transform duration-300 ease-in-out', |
| 70 | + isOpen ? 'translate-x-0' : 'translate-x-full' |
| 71 | + )} |
| 72 | + role="dialog" |
| 73 | + aria-modal="true" |
| 74 | + style={{ minWidth: '400px', maxWidth: '800px' }} |
| 75 | + > |
| 76 | + {/* Header */} |
| 77 | + <div className="flex items-start justify-between p-6 border-b border-border bg-card"> |
| 78 | + <div className="flex-1 min-w-0 mr-4"> |
| 79 | + <div className="flex items-center gap-2 mb-2 flex-wrap"> |
| 80 | + <span className="text-xs font-mono text-muted-foreground">{finding.id}</span> |
| 81 | + <Badge variant={severity.variant}> |
| 82 | + {formatMessage({ id: severity.label })} |
| 83 | + </Badge> |
| 84 | + {finding.type && ( |
| 85 | + <Badge variant="outline">{finding.type}</Badge> |
| 86 | + )} |
| 87 | + {finding.category && ( |
| 88 | + <Badge variant="info">{finding.category}</Badge> |
| 89 | + )} |
| 90 | + </div> |
| 91 | + <h2 className="text-lg font-semibold text-foreground"> |
| 92 | + {finding.title} |
| 93 | + </h2> |
| 94 | + </div> |
| 95 | + <Button variant="ghost" size="icon" onClick={onClose} className="flex-shrink-0 hover:bg-secondary"> |
| 96 | + <X className="h-5 w-5" /> |
| 97 | + </Button> |
| 98 | + </div> |
| 99 | + |
| 100 | + {/* Content */} |
| 101 | + <div className="flex-1 overflow-y-auto p-6 space-y-6"> |
| 102 | + {/* Description */} |
| 103 | + <div> |
| 104 | + <h3 className="text-sm font-semibold text-foreground mb-2 flex items-center gap-2"> |
| 105 | + <AlertTriangle className="h-4 w-4" /> |
| 106 | + {formatMessage({ id: 'issues.discovery.findings.description' })} |
| 107 | + </h3> |
| 108 | + <p className="text-sm text-muted-foreground whitespace-pre-wrap"> |
| 109 | + {finding.description} |
| 110 | + </p> |
| 111 | + </div> |
| 112 | + |
| 113 | + {/* File Location */} |
| 114 | + {finding.file && ( |
| 115 | + <div> |
| 116 | + <h3 className="text-sm font-semibold text-foreground mb-2 flex items-center gap-2"> |
| 117 | + <MapPin className="h-4 w-4" /> |
| 118 | + {formatMessage({ id: 'issues.discovery.findings.location' })} |
| 119 | + </h3> |
| 120 | + <div className="flex items-center gap-2 text-sm"> |
| 121 | + <FileText className="h-4 w-4 text-muted-foreground" /> |
| 122 | + <code className="px-2 py-1 bg-muted rounded text-xs"> |
| 123 | + {finding.file} |
| 124 | + {finding.line && `:${finding.line}`} |
| 125 | + </code> |
| 126 | + </div> |
| 127 | + </div> |
| 128 | + )} |
| 129 | + |
| 130 | + {/* Code Snippet */} |
| 131 | + {finding.code_snippet && ( |
| 132 | + <div> |
| 133 | + <h3 className="text-sm font-semibold text-foreground mb-2 flex items-center gap-2"> |
| 134 | + <Code className="h-4 w-4" /> |
| 135 | + {formatMessage({ id: 'issues.discovery.findings.codeSnippet' })} |
| 136 | + </h3> |
| 137 | + <pre className="p-3 bg-muted rounded-md overflow-x-auto text-xs border border-border"> |
| 138 | + <code>{finding.code_snippet}</code> |
| 139 | + </pre> |
| 140 | + </div> |
| 141 | + )} |
| 142 | + |
| 143 | + {/* Suggested Fix */} |
| 144 | + {finding.suggested_issue && ( |
| 145 | + <div> |
| 146 | + <h3 className="text-sm font-semibold text-foreground mb-2 flex items-center gap-2"> |
| 147 | + <Lightbulb className="h-4 w-4" /> |
| 148 | + {formatMessage({ id: 'issues.discovery.findings.suggestedFix' })} |
| 149 | + </h3> |
| 150 | + <p className="text-sm text-muted-foreground whitespace-pre-wrap"> |
| 151 | + {finding.suggested_issue} |
| 152 | + </p> |
| 153 | + </div> |
| 154 | + )} |
| 155 | + |
| 156 | + {/* Confidence */} |
| 157 | + {finding.confidence !== undefined && ( |
| 158 | + <div> |
| 159 | + <h3 className="text-sm font-semibold text-foreground mb-2 flex items-center gap-2"> |
| 160 | + <Target className="h-4 w-4" /> |
| 161 | + {formatMessage({ id: 'issues.discovery.findings.confidence' })} |
| 162 | + </h3> |
| 163 | + <div className="flex items-center gap-2"> |
| 164 | + <div className="flex-1 h-2 bg-muted rounded-full overflow-hidden"> |
| 165 | + <div |
| 166 | + className={cn( |
| 167 | + "h-full transition-all", |
| 168 | + finding.confidence >= 0.9 ? "bg-green-500" : |
| 169 | + finding.confidence >= 0.7 ? "bg-yellow-500" : "bg-red-500" |
| 170 | + )} |
| 171 | + style={{ width: `${finding.confidence * 100}%` }} |
| 172 | + /> |
| 173 | + </div> |
| 174 | + <span className="text-sm font-medium"> |
| 175 | + {Math.round(finding.confidence * 100)}% |
| 176 | + </span> |
| 177 | + </div> |
| 178 | + </div> |
| 179 | + )} |
| 180 | + |
| 181 | + {/* Reference */} |
| 182 | + {finding.reference && ( |
| 183 | + <div> |
| 184 | + <h3 className="text-sm font-semibold text-foreground mb-2 flex items-center gap-2"> |
| 185 | + <ExternalLink className="h-4 w-4" /> |
| 186 | + {formatMessage({ id: 'issues.discovery.findings.reference' })} |
| 187 | + </h3> |
| 188 | + <a |
| 189 | + href={finding.reference} |
| 190 | + target="_blank" |
| 191 | + rel="noopener noreferrer" |
| 192 | + className="text-sm text-primary hover:underline break-all" |
| 193 | + > |
| 194 | + {finding.reference} |
| 195 | + </a> |
| 196 | + </div> |
| 197 | + )} |
| 198 | + |
| 199 | + {/* Perspective */} |
| 200 | + {finding.perspective && ( |
| 201 | + <div className="pt-4 border-t border-border"> |
| 202 | + <Badge variant="secondary" className="text-xs"> |
| 203 | + {formatMessage({ id: 'issues.discovery.findings.perspective' })}: {finding.perspective} |
| 204 | + </Badge> |
| 205 | + </div> |
| 206 | + )} |
| 207 | + </div> |
| 208 | + </div> |
| 209 | + </> |
| 210 | + ); |
| 211 | +} |
| 212 | + |
| 213 | +export default FindingDrawer; |
0 commit comments