review-result.tsx617 lines, 18 KB
| 1 | "use client"; |
| 2 | |
| 3 | import React, { useState } from "react"; |
| 4 | import { Card, CardContent } from "@/components/ui/card"; |
| 5 | import { Badge } from "@/components/ui/badge"; |
| 6 | import { Button } from "@/components/ui/button"; |
| 7 | import { |
| 8 | Bug, |
| 9 | Shield, |
| 10 | Zap, |
| 11 | Paintbrush, |
| 12 | Lightbulb, |
| 13 | FileCode2, |
| 14 | CheckCircle2, |
| 15 | XCircle, |
| 16 | Clock, |
| 17 | ChevronDown, |
| 18 | ChevronRight, |
| 19 | Copy, |
| 20 | Check, |
| 21 | CircleDot, |
| 22 | ShieldCheck, |
| 23 | ShieldAlert, |
| 24 | ShieldX, |
| 25 | } from "lucide-react"; |
| 26 | import { cn } from "@/lib/utils"; |
| 27 | |
| 28 | interface ReviewComment { |
| 29 | file: string; |
| 30 | line: number; |
| 31 | severity: string; |
| 32 | category?: string; |
| 33 | message: string; |
| 34 | suggestion?: string; |
| 35 | } |
| 36 | |
| 37 | interface ReviewResultProps { |
| 38 | review: { |
| 39 | id: string; |
| 40 | status: string; |
| 41 | summary: string | null; |
| 42 | riskScore: number | null; |
| 43 | comments: ReviewComment[] | unknown; |
| 44 | error: string | null; |
| 45 | createdAt: Date; |
| 46 | }; |
| 47 | } |
| 48 | |
| 49 | export function ReviewResult({ review }: ReviewResultProps) { |
| 50 | if (review.status === "PENDING") { |
| 51 | return ( |
| 52 | <Card> |
| 53 | <CardContent className="py-12 px-6"> |
| 54 | <div className="flex items-center gap-4"> |
| 55 | <div className="size-10 rounded-full bg-amber-500/10 flex items-center justify-center shrink-0"> |
| 56 | <Clock className="size-5 text-amber-500" /> |
| 57 | </div> |
| 58 | <div className="flex-1 min-w-0"> |
| 59 | <h3 className="font-medium">Queued for review</h3> |
| 60 | <p className="text-sm text-muted-foreground mt-0.5"> |
| 61 | This review is queued for review and will be processed soon. |
| 62 | </p> |
| 63 | </div> |
| 64 | <div className="flex items-center gap-1"> |
| 65 | {[0, 1, 2].map((i) => ( |
| 66 | <div |
| 67 | key={i} |
| 68 | className="size-1.5 rounded-full bg-amber-500 animate-pulse" |
| 69 | style={{ animationDelay: `${i * 200}ms` }} |
| 70 | /> |
| 71 | ))} |
| 72 | </div> |
| 73 | </div> |
| 74 | </CardContent> |
| 75 | </Card> |
| 76 | ); |
| 77 | } |
| 78 | |
| 79 | if (review.status === "PROCESSING") { |
| 80 | return ( |
| 81 | <Card> |
| 82 | <CardContent className="py-12 px-6"> |
| 83 | <div className="flex items-center gap-4"> |
| 84 | <div className="relative size-10 shrink-0"> |
| 85 | <svg className="size-10 -rotate-90" viewBox="0 0 40 40"> |
| 86 | <circle |
| 87 | cx="20" |
| 88 | cy="20" |
| 89 | r="16" |
| 90 | fill="none" |
| 91 | strokeWidth="3" |
| 92 | className="stroke-muted" |
| 93 | /> |
| 94 | <circle |
| 95 | cx="20" |
| 96 | cy="20" |
| 97 | r="16" |
| 98 | fill="none" |
| 99 | strokeWidth="3" |
| 100 | className="stroke-primary" |
| 101 | style={{ |
| 102 | strokeDasharray: "100", |
| 103 | strokeDashoffset: "60", |
| 104 | animation: "spin 1s linear infinite", |
| 105 | transformOrigin: "center", |
| 106 | }} |
| 107 | /> |
| 108 | </svg> |
| 109 | <style jsx>{` |
| 110 | @keyframes spin { |
| 111 | to { |
| 112 | transform: rotate(360deg); |
| 113 | } |
| 114 | } |
| 115 | `}</style> |
| 116 | </div> |
| 117 | <div className="flex-1 min-w-0"> |
| 118 | <h3 className="font-medium">Analysing code</h3> |
| 119 | <p className="text-sm text-muted-foreground mt-0.5"> |
| 120 | Scanning for bugs, security issues, and improvements |
| 121 | </p> |
| 122 | </div> |
| 123 | <span className="text-xs text-muted-foreground tabular-nums"> |
| 124 | ~20s |
| 125 | </span> |
| 126 | </div> |
| 127 | </CardContent> |
| 128 | </Card> |
| 129 | ); |
| 130 | } |
| 131 | |
| 132 | if (review.status === "FAILED") { |
| 133 | return ( |
| 134 | <Card className="overflow-hidden border-destructive/20"> |
| 135 | <CardContent className="p-0"> |
| 136 | <div className="relative py-20 px-6"> |
| 137 | <div className="absolute inset-0 bg-linear-to-b from-red-500/5 via-transparent to-red-500/5" /> |
| 138 | |
| 139 | <div className="relative text-center"> |
| 140 | <div className="mx-auto size-16 rounded-2xl bg-destructive/10 border border-destructive/20 flex items-center justify-center mb-6"> |
| 141 | <XCircle className="size-8 text-destructive" /> |
| 142 | </div> |
| 143 | <h3 className="text-lg font-semibold text-destructive"> |
| 144 | Failed to review code |
| 145 | </h3> |
| 146 | <p className="text-sm text-muted-foreground"> |
| 147 | {review.error || "Please try again later or contact support"} |
| 148 | </p> |
| 149 | <Button variant={"outline"} className="mt-6"> |
| 150 | Retry Analysis |
| 151 | </Button> |
| 152 | </div> |
| 153 | </div> |
| 154 | </CardContent> |
| 155 | </Card> |
| 156 | ); |
| 157 | } |
| 158 | |
| 159 | const comments = Array.isArray(review.comments) |
| 160 | ? (review.comments as ReviewComment[]) |
| 161 | : []; |
| 162 | |
| 163 | const severityCounts = { |
| 164 | critical: comments.filter((c) => c.severity === "critical").length, |
| 165 | high: comments.filter((c) => c.severity === "high").length, |
| 166 | medium: comments.filter((c) => c.severity === "medium").length, |
| 167 | low: comments.filter((c) => c.severity === "low").length, |
| 168 | }; |
| 169 | |
| 170 | const totalIssues = comments.length; |
| 171 | |
| 172 | return ( |
| 173 | <div className="space-y-6"> |
| 174 | <Card className="overflow-hidden"> |
| 175 | <CardContent className="p-6 space-y-6"> |
| 176 | <RiskScoreSection score={review.riskScore ?? 0} /> |
| 177 | |
| 178 | <div className="h-px bg-border/60" /> |
| 179 | |
| 180 | <div> |
| 181 | <div className="flex items-center justify-between mb-4"> |
| 182 | <h3 className="text-sm font-medium text-muted-foreground"> |
| 183 | Severity Breakdown |
| 184 | </h3> |
| 185 | <div |
| 186 | className={cn( |
| 187 | "flex items-center justify-center min-w-[2.5rem] px-3 py-1.5 rounded-lg", |
| 188 | totalIssues === 0 ? "bg-emerald-500/10" : "bg-muted", |
| 189 | )} |
| 190 | > |
| 191 | <span |
| 192 | className={cn( |
| 193 | "text-xl font-bold tabular-nums tracking-tight", |
| 194 | totalIssues === 0 |
| 195 | ? "text-emerald-600 dark:text-emerald-400" |
| 196 | : "text-foreground", |
| 197 | )} |
| 198 | > |
| 199 | {totalIssues} |
| 200 | </span> |
| 201 | </div> |
| 202 | </div> |
| 203 | |
| 204 | <SeverityDistributionBar |
| 205 | counts={severityCounts} |
| 206 | total={totalIssues} |
| 207 | /> |
| 208 | |
| 209 | <div className="flex items-center gap-4 mt-4 flex-wrap"> |
| 210 | <SeverityLegendItem |
| 211 | label="Critical" |
| 212 | count={severityCounts.critical} |
| 213 | color="bg-red-500" |
| 214 | /> |
| 215 | <SeverityLegendItem |
| 216 | label="High" |
| 217 | count={severityCounts.high} |
| 218 | color="bg-orange-500" |
| 219 | /> |
| 220 | <SeverityLegendItem |
| 221 | label="Medium" |
| 222 | count={severityCounts.medium} |
| 223 | color="bg-amber-500" |
| 224 | /> |
| 225 | <SeverityLegendItem |
| 226 | label="Low" |
| 227 | count={severityCounts.low} |
| 228 | color="bg-slate-400 dark:bg-slate-500" |
| 229 | /> |
| 230 | </div> |
| 231 | </div> |
| 232 | |
| 233 | {review.summary && ( |
| 234 | <> |
| 235 | <div className="h-px bg-border/60" /> |
| 236 | <div> |
| 237 | <h3 className="text-sm font-medium text-muted-foreground mb-2"> |
| 238 | AI Summary |
| 239 | </h3> |
| 240 | <p className="text-sm leading-relaxed text-foreground/90"> |
| 241 | {review.summary} |
| 242 | </p> |
| 243 | </div> |
| 244 | </> |
| 245 | )} |
| 246 | </CardContent> |
| 247 | </Card> |
| 248 | |
| 249 | {comments.length > 0 ? ( |
| 250 | <div className="space-y-3"> |
| 251 | <div className="flex items-center justify-between px-1"> |
| 252 | <h2 className="text-sm font-medium text-muted-foreground"> |
| 253 | Review Comments |
| 254 | </h2> |
| 255 | <span className="text-xs text-muted-foreground tabular-nums"> |
| 256 | {comments.length} {comments.length === 1 ? "issue" : "issues"} |
| 257 | </span> |
| 258 | </div> |
| 259 | |
| 260 | <div className="space-y-2"> |
| 261 | {comments.map((comment, index) => ( |
| 262 | <CommentCard key={index} comment={comment} index={index} /> |
| 263 | ))} |
| 264 | </div> |
| 265 | </div> |
| 266 | ) : ( |
| 267 | review.status === "COMPLETED" && <NoIssuesCard /> |
| 268 | )} |
| 269 | </div> |
| 270 | ); |
| 271 | } |
| 272 | |
| 273 | function NoIssuesCard() { |
| 274 | return ( |
| 275 | <Card className="overflow-hidden"> |
| 276 | <CardContent className="p-0"> |
| 277 | <div className="relative py-16 px-6"> |
| 278 | <div className="absolute inset-0 bg-linear-to-br from-emerald-500/5 via-transparent to-emerald-500/5" /> |
| 279 | |
| 280 | <div className="relative text-center"> |
| 281 | <div className="mx-auto size-16 rounded-xl bg-emerald-500/10 border border-emerald-500/20 flex items-center justify-center mb-6"> |
| 282 | <CheckCircle2 className="size-8 text-emerald-500" /> |
| 283 | </div> |
| 284 | <h3>Looking Good!</h3> |
| 285 | <p className="text-sm text-muted-foreground mt-2 max-w-sm mx-auto"> |
| 286 | No issues were found. Your code follows best practices and appears |
| 287 | to be well-written. |
| 288 | </p> |
| 289 | </div> |
| 290 | </div> |
| 291 | </CardContent> |
| 292 | </Card> |
| 293 | ); |
| 294 | } |
| 295 | |
| 296 | function CommentCard({ |
| 297 | comment, |
| 298 | index, |
| 299 | }: { |
| 300 | comment: ReviewComment; |
| 301 | index: number; |
| 302 | }) { |
| 303 | const [expanded, setExpanded] = useState(index < 3); |
| 304 | const [copied, setCopied] = useState(false); |
| 305 | const CategoryIcon = getCategoryIcon(comment.category); |
| 306 | const severityConfig = getSeverityStyles(comment.severity); |
| 307 | |
| 308 | const copyLocation = () => { |
| 309 | navigator.clipboard.writeText(`${comment.file}:${comment.line}`); |
| 310 | setCopied(true); |
| 311 | setTimeout(() => setCopied(false), 2000); |
| 312 | }; |
| 313 | |
| 314 | const pathParts = comment.file.split("/"); |
| 315 | const fileName = pathParts.pop(); |
| 316 | const directory = pathParts.join("/"); |
| 317 | |
| 318 | return ( |
| 319 | <Card> |
| 320 | <button |
| 321 | onClick={() => setExpanded(!expanded)} |
| 322 | className="w-full text-left" |
| 323 | > |
| 324 | <div className="p-4 flex items-start gap-3"> |
| 325 | <div |
| 326 | className={cn( |
| 327 | "my-0.5 w-1 h-12 rounded-full shrink-0", |
| 328 | severityConfig.bar, |
| 329 | )} |
| 330 | /> |
| 331 | |
| 332 | <div className="flex-1 min-w-0 space-y-2"> |
| 333 | <div className="flex items-center gap-2 flex-wrap"> |
| 334 | <Badge |
| 335 | variant={"outline"} |
| 336 | className={cn( |
| 337 | "text-[10px] uppercase tracking-wider font-semibold", |
| 338 | severityConfig.badge, |
| 339 | )} |
| 340 | > |
| 341 | {comment.severity} |
| 342 | </Badge> |
| 343 | |
| 344 | {comment.category && ( |
| 345 | <Badge variant={"secondary"} className="gap-1 text-xs"> |
| 346 | {React.createElement(CategoryIcon, { |
| 347 | className: "size-3", |
| 348 | })} |
| 349 | {comment.category} |
| 350 | </Badge> |
| 351 | )} |
| 352 | |
| 353 | <div className="flex-1" /> |
| 354 | |
| 355 | {expanded ? ( |
| 356 | <ChevronDown className="size-4 text-muted-foreground" /> |
| 357 | ) : ( |
| 358 | <ChevronRight className="size-4 text-muted-foreground" /> |
| 359 | )} |
| 360 | </div> |
| 361 | |
| 362 | <p |
| 363 | className={cn( |
| 364 | "text-sm leading-relaxed", |
| 365 | !expanded && "line-clamp-2", |
| 366 | )} |
| 367 | > |
| 368 | {comment.message} |
| 369 | </p> |
| 370 | |
| 371 | <button |
| 372 | onClick={(e) => { |
| 373 | e.stopPropagation(); |
| 374 | copyLocation(); |
| 375 | }} |
| 376 | className="group/file inline-flex items-center gap-2 text-xs font-mono text-muted-foreground hover:text-foreground transition-colors" |
| 377 | > |
| 378 | <FileCode2 className="size-3.5" /> |
| 379 | {directory && <span className="opacity-60">{directory}</span>} |
| 380 | <span className="font-medium text-foreground ">{fileName}</span> |
| 381 | <span className="text-foreground">:</span> |
| 382 | <span className="text-primary font-medium">{comment.line}</span> |
| 383 | {copied ? ( |
| 384 | <Check className="size-3.5 text-emerald-500" /> |
| 385 | ) : ( |
| 386 | <Copy className="size-3.5 opacity-0 group-hover/file:opacity-100 trantransition-opacity" /> |
| 387 | )} |
| 388 | </button> |
| 389 | </div> |
| 390 | </div> |
| 391 | </button> |
| 392 | |
| 393 | {expanded && comment.suggestion && ( |
| 394 | <div className="px-4 pb-4"> |
| 395 | <div className="ml-4 rounded-lg bg-linear-to-br from-emerald-500/10 to-emerald-500/5 border border-emerald-500/20 p-4"> |
| 396 | <div className="flex items-center gap-2 mb-2"> |
| 397 | <div className="p-1 rounded-md bg-emerald-500/20"> |
| 398 | <Lightbulb className="size-3.5 text-emerald-500" /> |
| 399 | </div> |
| 400 | <span className="text-xs font-semibold text-emerald-600 dark:text-emerald-400 uppercase tracking-wider"> |
| 401 | Suggested Fix |
| 402 | </span> |
| 403 | </div> |
| 404 | <p className="text-sm leading-relaxed pl-7">{comment.suggestion}</p> |
| 405 | </div> |
| 406 | </div> |
| 407 | )} |
| 408 | </Card> |
| 409 | ); |
| 410 | } |
| 411 | |
| 412 | function SeverityDistributionBar({ |
| 413 | counts, |
| 414 | total, |
| 415 | }: { |
| 416 | counts: { critical: number; high: number; medium: number; low: number }; |
| 417 | total: number; |
| 418 | }) { |
| 419 | if (total === 0) { |
| 420 | return ( |
| 421 | <div className="h-2 bg-emerald-500/20 rounded-full overflow-hidden"> |
| 422 | <div className="w-full h-full bg-emerald-500 rounded-full" /> |
| 423 | </div> |
| 424 | ); |
| 425 | } |
| 426 | |
| 427 | const getWidth = (count: number) => `${(count / total) * 100}%`; |
| 428 | |
| 429 | return ( |
| 430 | <div className="h-2 bg-muted rounded-full overflow-hidden flex"> |
| 431 | {counts.critical > 0 && ( |
| 432 | <div |
| 433 | className="h-full bg-red-500 first:rounded-l-full last:rounded-r-full" |
| 434 | style={{ width: getWidth(counts.critical) }} |
| 435 | /> |
| 436 | )} |
| 437 | {counts.high > 0 && ( |
| 438 | <div |
| 439 | className="h-full bg-orange-500 first:rounded-l-full last:rounded-r-full" |
| 440 | style={{ width: getWidth(counts.high) }} |
| 441 | /> |
| 442 | )} |
| 443 | {counts.medium > 0 && ( |
| 444 | <div |
| 445 | className="h-full bg-amber-500 first:rounded-l-full last:rounded-r-full" |
| 446 | style={{ width: getWidth(counts.medium) }} |
| 447 | /> |
| 448 | )} |
| 449 | {counts.low > 0 && ( |
| 450 | <div |
| 451 | className="h-full bg-slate-400 dark:bg-slate-500 first:rounded-l-full last:rounded-r-full" |
| 452 | style={{ width: getWidth(counts.low) }} |
| 453 | /> |
| 454 | )} |
| 455 | </div> |
| 456 | ); |
| 457 | } |
| 458 | |
| 459 | function SeverityLegendItem({ |
| 460 | label, |
| 461 | count, |
| 462 | color, |
| 463 | }: { |
| 464 | label: string; |
| 465 | count: number; |
| 466 | color: string; |
| 467 | }) { |
| 468 | return ( |
| 469 | <div className="flex items-center gap-1.5"> |
| 470 | <div className={cn("size-2 rounded-full", color)} /> |
| 471 | <span className="text-xs text-muted-foreground">{label}</span> |
| 472 | <span className="text-xs font-medium tabular-nums">{count}</span> |
| 473 | </div> |
| 474 | ); |
| 475 | } |
| 476 | |
| 477 | function RiskScoreSection({ score }: { score: number }) { |
| 478 | const config = getRiskConfig(score); |
| 479 | |
| 480 | return ( |
| 481 | <div className="space-y-4"> |
| 482 | <div className="flex items-center justify-between"> |
| 483 | <h3 className="text-sm font-medium text-muted-foreground"> |
| 484 | Risk Score |
| 485 | </h3> |
| 486 | <div |
| 487 | className={cn( |
| 488 | "flex items-center gap-1.5 px-3 py-1.5 rounded-lg", |
| 489 | config.bg, |
| 490 | )} |
| 491 | > |
| 492 | <span |
| 493 | className={cn( |
| 494 | "text-xl font-bold tabular-nums tracking-tight", |
| 495 | config.color, |
| 496 | )} |
| 497 | > |
| 498 | {score} |
| 499 | </span> |
| 500 | <span className="text-xs text-muted-foreground font-medium"> |
| 501 | /100 |
| 502 | </span> |
| 503 | </div> |
| 504 | </div> |
| 505 | |
| 506 | <div className="relative h-1.5"> |
| 507 | <div className="absolute inset-0 rounded-full bg-linear-to-r from-emerald-500 via-amber-500 to-red-500 opacity-15" /> |
| 508 | <div |
| 509 | className="absolute inset-y-0 left-0 rounded-full bg-linear-to-r from-emerald-500 via-amber-500 to-red-500" |
| 510 | style={{ |
| 511 | width: `${score}%`, |
| 512 | transition: "width 0.3s ease-in-out", |
| 513 | }} |
| 514 | /> |
| 515 | <div |
| 516 | className="absolute top-1/2 size-3.5 rounded-full bg-background border-2 shadow-md" |
| 517 | style={{ |
| 518 | left: `${Math.min(Math.max(score, 2), 98)}%`, |
| 519 | transform: "translateX(-50%) translateY(-50%)", |
| 520 | borderColor: config.markerColor, |
| 521 | transition: "left 0.3s ease-in-out", |
| 522 | }} |
| 523 | /> |
| 524 | </div> |
| 525 | |
| 526 | <div className="flex items-center justify-between"> |
| 527 | <span className="text-[11px] text-muted-foreground">Low</span> |
| 528 | <span className="text-[11px] text-muted-foreground">Critical</span> |
| 529 | </div> |
| 530 | </div> |
| 531 | ); |
| 532 | } |
| 533 | |
| 534 | function getRiskConfig(score: number) { |
| 535 | if (score < 25) { |
| 536 | return { |
| 537 | color: "text-emerald-600 dark:text-emerald-400", |
| 538 | bg: "bg-emerald-500/10", |
| 539 | border: "border-emerald-500/20", |
| 540 | markerColor: "#10b981", |
| 541 | label: "Low Risk", |
| 542 | icon: ShieldCheck, |
| 543 | }; |
| 544 | } |
| 545 | if (score < 50) { |
| 546 | return { |
| 547 | color: "text-amber-600 dark:text-amber-400", |
| 548 | bg: "bg-amber-500/10", |
| 549 | border: "border-amber-500/20", |
| 550 | markerColor: "#f59e0b", |
| 551 | label: "Medium Risk", |
| 552 | icon: CircleDot, |
| 553 | }; |
| 554 | } |
| 555 | if (score < 75) { |
| 556 | return { |
| 557 | color: "text-orange-600 dark:text-orange-400", |
| 558 | bg: "bg-orange-500/10", |
| 559 | border: "border-orange-500/20", |
| 560 | markerColor: "#f97316", |
| 561 | label: "High Risk", |
| 562 | icon: ShieldAlert, |
| 563 | }; |
| 564 | } |
| 565 | return { |
| 566 | color: "text-red-600 dark:text-red-400", |
| 567 | bg: "bg-red-500/10", |
| 568 | border: "border-red-500/20", |
| 569 | markerColor: "#ef4444", |
| 570 | label: "Critical Risk", |
| 571 | icon: ShieldX, |
| 572 | }; |
| 573 | } |
| 574 | |
| 575 | function getSeverityStyles(severity: string) { |
| 576 | switch (severity) { |
| 577 | case "critical": |
| 578 | return { |
| 579 | bar: "bg-red-500", |
| 580 | badge: "border-red-500/30 bg-red-500/10 text-red-600 dark:text-red-400", |
| 581 | }; |
| 582 | case "high": |
| 583 | return { |
| 584 | bar: "bg-orange-500", |
| 585 | badge: |
| 586 | "border-orange-500/30 bg-orange-500/10 text-orange-600 dark:text-orange-400", |
| 587 | }; |
| 588 | case "medium": |
| 589 | return { |
| 590 | bar: "bg-amber-500", |
| 591 | badge: |
| 592 | "border-amber-500/30 bg-amber-500/10 text-amber-600 dark:text-amber-400", |
| 593 | }; |
| 594 | default: |
| 595 | return { |
| 596 | bar: "bg-slate-400 dark:bg-slate-500", |
| 597 | badge: "border-border bg-muted text-muted-foreground", |
| 598 | }; |
| 599 | } |
| 600 | } |
| 601 | |
| 602 | function getCategoryIcon(category?: string) { |
| 603 | switch (category) { |
| 604 | case "bug": |
| 605 | return Bug; |
| 606 | case "security": |
| 607 | return Shield; |
| 608 | case "performance": |
| 609 | return Zap; |
| 610 | case "style": |
| 611 | return Paintbrush; |
| 612 | case "suggestion": |
| 613 | return Lightbulb; |
| 614 | default: |
| 615 | return CircleDot; |
| 616 | } |
| 617 | } |