page.tsx566 lines, 17 KB
| 1 | "use client"; |
| 2 | |
| 3 | import { use, useState } from "react"; |
| 4 | import Link from "next/link"; |
| 5 | import { trpc } from "@/lib/trpc/client"; |
| 6 | import { Button } from "@/components/ui/button"; |
| 7 | import { Card, CardContent } from "@/components/ui/card"; |
| 8 | import { Badge } from "@/components/ui/badge"; |
| 9 | import { Skeleton } from "@/components/ui/skeleton"; |
| 10 | import { Avatar, AvatarFallback, AvatarImage } from "@/components/ui/avatar"; |
| 11 | import { |
| 12 | ArrowLeft, |
| 13 | GitPullRequest, |
| 14 | GitMerge, |
| 15 | ExternalLink, |
| 16 | Clock, |
| 17 | Plus, |
| 18 | Minus, |
| 19 | FileText, |
| 20 | XCircle, |
| 21 | CheckCircle, |
| 22 | Loader2, |
| 23 | Sparkles, |
| 24 | GitBranch, |
| 25 | ArrowRight, |
| 26 | Wand2, |
| 27 | ScanSearch, |
| 28 | } from "lucide-react"; |
| 29 | import { cn } from "@/lib/utils"; |
| 30 | import { DiffViewer } from "@/components/diff-viewer"; |
| 31 | import { ReviewResult } from "@/components/review-result"; |
| 32 | |
| 33 | type PageProps = { |
| 34 | params: Promise<{ id: string; prNumber: string }>; |
| 35 | }; |
| 36 | |
| 37 | export default function PullRequestDetailPage({ params }: PageProps) { |
| 38 | const { id, prNumber } = use(params); |
| 39 | const prNum = parseInt(prNumber, 10); |
| 40 | const [activeTab, setActiveTab] = useState<"review" | "files">("review"); |
| 41 | |
| 42 | const pr = trpc.pullRequest.get.useQuery( |
| 43 | { repositoryId: id, prNumber: prNum }, |
| 44 | { enabled: !isNaN(prNum) }, |
| 45 | ); |
| 46 | |
| 47 | const files = trpc.pullRequest.files.useQuery( |
| 48 | { repositoryId: id, prNumber: prNum }, |
| 49 | { enabled: !isNaN(prNum) }, |
| 50 | ); |
| 51 | |
| 52 | const latestReview = trpc.review.getLatestForPR.useQuery( |
| 53 | { repositoryId: id, prNumber: prNum }, |
| 54 | { |
| 55 | enabled: !isNaN(prNum), |
| 56 | refetchInterval: (query) => { |
| 57 | const status = query.state.data?.status; |
| 58 | if (status === "PENDING" || status === "PROCESSING") { |
| 59 | return 2000; |
| 60 | } |
| 61 | return false; |
| 62 | }, |
| 63 | }, |
| 64 | ); |
| 65 | |
| 66 | const triggerReview = trpc.review.trigger.useMutation({ |
| 67 | onSuccess: () => { |
| 68 | latestReview.refetch(); |
| 69 | pr.refetch(); |
| 70 | }, |
| 71 | }); |
| 72 | |
| 73 | const isReviewing = |
| 74 | latestReview.data?.status === "PENDING" || |
| 75 | latestReview.data?.status === "PROCESSING"; |
| 76 | |
| 77 | if (pr.isLoading) { |
| 78 | return ( |
| 79 | <div className="space-y-8"> |
| 80 | <div className="flex items-center gap-4"> |
| 81 | <Skeleton className="size-10 rounded-lg" /> |
| 82 | <div className="space-y-2"> |
| 83 | <Skeleton className="h-7 w-96" /> |
| 84 | <Skeleton className="h-4 w-64" /> |
| 85 | </div> |
| 86 | </div> |
| 87 | <Skeleton className="h-24 w-full rounded-xl" /> |
| 88 | <Skeleton className="h-64 w-full rounded-xl" /> |
| 89 | </div> |
| 90 | ); |
| 91 | } |
| 92 | |
| 93 | if (pr.error || !pr.data) { |
| 94 | return ( |
| 95 | <Card> |
| 96 | <CardContent className="py-16 text-center"> |
| 97 | <div className="mx-auto size-12 rounded-full bg-destructive/10 flex items-center justify-center"> |
| 98 | <XCircle className="size-6 text-destructive" /> |
| 99 | </div> |
| 100 | <p className="mt-4 font-medium text-destructive"> |
| 101 | {pr.error?.message ?? "Pull Request not found"} |
| 102 | </p> |
| 103 | <Link href={`/repos/${id}`} className="mt-6 inline-block"> |
| 104 | <Button variant={"outline"}> |
| 105 | <ArrowLeft className="size-4" /> |
| 106 | Back to Repository |
| 107 | </Button> |
| 108 | </Link> |
| 109 | </CardContent> |
| 110 | </Card> |
| 111 | ); |
| 112 | } |
| 113 | |
| 114 | const isMerged = pr.data.state === "closed" && pr.data.mergedAt; |
| 115 | |
| 116 | return ( |
| 117 | <div className="space-y-8"> |
| 118 | <div className="flex items-start gap-4"> |
| 119 | <Link href={`/repos/${id}`}> |
| 120 | <Button variant={"outline"} size={"icon"} className="shrink-0 mt-1"> |
| 121 | <ArrowLeft className="size-4" /> |
| 122 | </Button> |
| 123 | </Link> |
| 124 | |
| 125 | <div className="flex-1 min-w-0"> |
| 126 | <div className="flex items-start justify-between gap-4"> |
| 127 | <div className="min-w-0"> |
| 128 | <div className="flex items-center gap-3 flex-wrap"> |
| 129 | <div |
| 130 | className={cn( |
| 131 | "p-2 rounded-lg shrink-0", |
| 132 | isMerged |
| 133 | ? "bg-purple-500/10" |
| 134 | : pr.data.state === "closed" |
| 135 | ? "bg-red-500/10" |
| 136 | : "bg-emerald-500/10", |
| 137 | )} |
| 138 | > |
| 139 | {isMerged ? ( |
| 140 | <GitMerge className="size-5 text-purple-500" /> |
| 141 | ) : pr.data.state === "closed" ? ( |
| 142 | <XCircle className="size-5 text-red-500" /> |
| 143 | ) : ( |
| 144 | <GitPullRequest className="size-5 text-emerald-500" /> |
| 145 | )} |
| 146 | </div> |
| 147 | <div className="min-w-0"> |
| 148 | <h1 className="text-xl font-semibold tracking-tight truncate"> |
| 149 | {pr.data.title} |
| 150 | </h1> |
| 151 | <div className="flex items-center gap-2 mt-1 flex-wrap"> |
| 152 | <PRStatusBadge |
| 153 | state={pr.data.state} |
| 154 | isMerged={!!isMerged} |
| 155 | draft={pr.data.draft} |
| 156 | /> |
| 157 | <span className="text-sm text-muted-foreground font-mono"> |
| 158 | #{pr.data.number} |
| 159 | </span> |
| 160 | </div> |
| 161 | </div> |
| 162 | </div> |
| 163 | </div> |
| 164 | |
| 165 | <a |
| 166 | href={pr.data.htmlUrl} |
| 167 | target="_blank" |
| 168 | rel="noopener noreferrer" |
| 169 | className="shrink-0" |
| 170 | > |
| 171 | <Button variant={"outline"} size={"sm"} className="gap-2"> |
| 172 | <ExternalLink className="size-4" /> |
| 173 | GitHub |
| 174 | </Button> |
| 175 | </a> |
| 176 | </div> |
| 177 | |
| 178 | <div className="flex items-center gap-4 mt-4 text-sm text-muted-foreground flex-wrap"> |
| 179 | <span className="flex items-center gap-2"> |
| 180 | <Avatar className="h-5 w-5 ring-1 ring-border"> |
| 181 | <AvatarImage src={pr.data.author.avatarUrl} /> |
| 182 | <AvatarFallback className="text-[10px]"> |
| 183 | {pr.data.author.login?.[0]?.toUpperCase() ?? "?"} |
| 184 | </AvatarFallback> |
| 185 | </Avatar> |
| 186 | <span className="font-medium text-foreground"> |
| 187 | {pr.data.author.login} |
| 188 | </span> |
| 189 | </span> |
| 190 | <span className="text-muted-foreground/40">•</span> |
| 191 | <span className="flex items-center gap-1.5"> |
| 192 | <Clock className="size-3.5" /> |
| 193 | </span> |
| 194 | </div> |
| 195 | </div> |
| 196 | </div> |
| 197 | |
| 198 | <Card> |
| 199 | <CardContent className="p-0"> |
| 200 | <div className="flex items-center divide-x divide-border/60"> |
| 201 | <div className="flex-1 p-4"> |
| 202 | <div className="flex items-center gap-3"> |
| 203 | <div className="p-2 rounded-lg bg-muted"> |
| 204 | <GitBranch className="size-4 text-muted-foreground" /> |
| 205 | </div> |
| 206 | <div className="min-w-0 flex-1"> |
| 207 | <p className="text-xs text-muted-foreground mb-1"> |
| 208 | Merged request |
| 209 | </p> |
| 210 | <div className="flex items-center gap-2 text-sm"> |
| 211 | <code className="px-2 py-0.5 rounded bg-secondary font-mono text-xs miw truncate"> |
| 212 | {pr.data.headRef} |
| 213 | </code> |
| 214 | <ArrowRight className="size-3 text-muted-foreground shrink-0" /> |
| 215 | <code className="px-2 py-0.5 rounded bg-secondary font-mono text-xs truncate"> |
| 216 | {pr.data.baseRef} |
| 217 | </code> |
| 218 | </div> |
| 219 | </div> |
| 220 | </div> |
| 221 | </div> |
| 222 | |
| 223 | <div className="flex items-center gap-6 px-6 py-4"> |
| 224 | <StatItem |
| 225 | icon={Plus} |
| 226 | value={pr.data.additions} |
| 227 | colorClass="text-emerald-600 dark:text-emerald-400" |
| 228 | bgClass="bg-emerald-500/10" |
| 229 | /> |
| 230 | <StatItem |
| 231 | icon={Minus} |
| 232 | value={pr.data.deletions} |
| 233 | colorClass="text-red-600 dark:text-red-400" |
| 234 | bgClass="bg-red-500/10" |
| 235 | /> |
| 236 | <StatItem |
| 237 | icon={FileText} |
| 238 | value={pr.data.changedFiles} |
| 239 | colorClass="text-muted-foreground dark:text-muted-foreground" |
| 240 | bgClass="bg-muted" |
| 241 | /> |
| 242 | </div> |
| 243 | |
| 244 | <div className="px-6 py-4 flex items-center gap-3"> |
| 245 | <div className="flex items-center gap-2 rounded-lg px-3 py-1.5"> |
| 246 | <ReviewStatusBadge |
| 247 | status={latestReview.data?.status ?? null} |
| 248 | completedAt={ |
| 249 | latestReview.data?.status === "COMPLETED" |
| 250 | ? latestReview.data.createdAt |
| 251 | : null |
| 252 | } |
| 253 | /> |
| 254 | {!isReviewing && <div className="h-4 w-px bg-border" />} |
| 255 | {isReviewing ? null : ( |
| 256 | <Button |
| 257 | variant="outline" |
| 258 | size={"sm"} |
| 259 | onClick={() => { |
| 260 | triggerReview.mutate({ |
| 261 | repositoryId: id, |
| 262 | prNumber: prNum, |
| 263 | }); |
| 264 | }} |
| 265 | disabled={triggerReview.isPending} |
| 266 | className="gap-1.5 h-auto py-1 px-2 text-xs" |
| 267 | > |
| 268 | <Wand2 /> |
| 269 | {latestReview.data ? "Re-run" : "Review"} |
| 270 | </Button> |
| 271 | )} |
| 272 | </div> |
| 273 | </div> |
| 274 | </div> |
| 275 | </CardContent> |
| 276 | </Card> |
| 277 | |
| 278 | {/* Tabs */} |
| 279 | <div className="border-b border-border/60"> |
| 280 | <div className="flex items-center gap-1"> |
| 281 | <TabButton |
| 282 | active={activeTab === "review"} |
| 283 | onClick={() => setActiveTab("review")} |
| 284 | icon={ScanSearch} |
| 285 | label="Reviews" |
| 286 | count={ |
| 287 | latestReview.data?.status === "COMPLETED" |
| 288 | ? Array.isArray(latestReview.data.comments) |
| 289 | ? latestReview.data.comments.length |
| 290 | : 0 |
| 291 | : 0 |
| 292 | } |
| 293 | /> |
| 294 | |
| 295 | <TabButton |
| 296 | active={activeTab === "files"} |
| 297 | onClick={() => setActiveTab("files")} |
| 298 | icon={FileText} |
| 299 | label="Changed Files" |
| 300 | count={files.data?.length} |
| 301 | /> |
| 302 | </div> |
| 303 | </div> |
| 304 | |
| 305 | {/* Tab Content */} |
| 306 | {activeTab === "review" && ( |
| 307 | <div> |
| 308 | {latestReview.data ? ( |
| 309 | <ReviewResult review={latestReview.data} /> |
| 310 | ) : ( |
| 311 | <Card> |
| 312 | <CardContent className="py-16 text-center"> |
| 313 | <div className="mx-auto size-14 rounded-full bg-primary/10 flex items-center justify-center"> |
| 314 | <ScanSearch className="size-7 text-primary" /> |
| 315 | </div> |
| 316 | <p className="mt-4 font-medium">No reviews yet.</p> |
| 317 | <p className="text-sm text-muted-foreground mt-1 max-w-sm mx-auto"> |
| 318 | Click "Run AI Review" to analyze this pull request |
| 319 | for bugs, security issues, and improvements. |
| 320 | </p> |
| 321 | <Button |
| 322 | className="mt-6" |
| 323 | onClick={() => |
| 324 | triggerReview.mutate({ repositoryId: id, prNumber: prNum }) |
| 325 | } |
| 326 | disabled={triggerReview.isPending} |
| 327 | > |
| 328 | Run AI Review |
| 329 | </Button> |
| 330 | </CardContent> |
| 331 | </Card> |
| 332 | )} |
| 333 | </div> |
| 334 | )} |
| 335 | |
| 336 | {activeTab === "files" && ( |
| 337 | <div> |
| 338 | {files.isLoading ? ( |
| 339 | <div className="space-y-3"> |
| 340 | {[...Array(3)].map((_, i) => ( |
| 341 | <Skeleton key={i} className="h-32 w-full rounded-xl" /> |
| 342 | ))} |
| 343 | </div> |
| 344 | ) : files.error ? ( |
| 345 | <Card className="border-destructive/50"> |
| 346 | <CardContent className="py-12 text-center"> |
| 347 | <div className="mx-auto size-12 rounded-full bg-destructive/10 flex items-center justify-center"> |
| 348 | <XCircle className="size-6 text-destructive" /> |
| 349 | </div> |
| 350 | <p className="mt-4 font-medium text-destructive"> |
| 351 | No files changed. |
| 352 | </p> |
| 353 | <p className="text-sm text-muted-foreground mt-1"> |
| 354 | {files.error.message} |
| 355 | </p> |
| 356 | </CardContent> |
| 357 | </Card> |
| 358 | ) : files.data ? ( |
| 359 | <DiffViewer files={files.data} /> |
| 360 | ) : null} |
| 361 | </div> |
| 362 | )} |
| 363 | </div> |
| 364 | ); |
| 365 | } |
| 366 | |
| 367 | function TabButton({ |
| 368 | active, |
| 369 | onClick, |
| 370 | icon: Icon, |
| 371 | label, |
| 372 | count, |
| 373 | }: { |
| 374 | active: boolean; |
| 375 | onClick: () => void; |
| 376 | icon: React.ComponentType<{ className?: string }>; |
| 377 | label: string; |
| 378 | count?: number; |
| 379 | }) { |
| 380 | return ( |
| 381 | <button |
| 382 | onClick={onClick} |
| 383 | className={cn( |
| 384 | "relative px-4 py-2.5 text-sm font-medium transition-colors flex items-center gap-2", |
| 385 | active |
| 386 | ? "text-foreground" |
| 387 | : "text-muted-foreground hover:text-foreground", |
| 388 | )} |
| 389 | > |
| 390 | <Icon className="size-4" /> |
| 391 | {label} |
| 392 | {count !== undefined && ( |
| 393 | <span |
| 394 | className={cn( |
| 395 | "px-1.5 py-0.5 text-xs rounded-md tabular-nums", |
| 396 | active |
| 397 | ? "bg-foreground/10 text-foreground" |
| 398 | : "bg-muted text-muted-foreground", |
| 399 | )} |
| 400 | > |
| 401 | {count} |
| 402 | </span> |
| 403 | )} |
| 404 | {active && ( |
| 405 | <span className="absolute bottom-0 left-0 right-0 h-0.5 bg-primary rounded-full" /> |
| 406 | )} |
| 407 | </button> |
| 408 | ); |
| 409 | } |
| 410 | |
| 411 | function StatItem({ |
| 412 | icon: Icon, |
| 413 | value, |
| 414 | label, |
| 415 | colorClass, |
| 416 | bgClass, |
| 417 | }: { |
| 418 | icon: React.ComponentType<{ className?: string }>; |
| 419 | value: number; |
| 420 | label?: string; |
| 421 | colorClass: string; |
| 422 | bgClass: string; |
| 423 | }) { |
| 424 | return ( |
| 425 | <div className="flex items-center gap-2"> |
| 426 | <div className={cn("p-1.5 rounded-md", bgClass)}> |
| 427 | <Icon className={cn("size-3.5", colorClass)} /> |
| 428 | </div> |
| 429 | <div> |
| 430 | <p className={cn("text-sm font-semibold tabular-nums", colorClass)}> |
| 431 | {value.toLocaleString()} |
| 432 | </p> |
| 433 | {label && ( |
| 434 | <p className={cn("text-xs font-medium", colorClass)}>{label}</p> |
| 435 | )} |
| 436 | </div> |
| 437 | </div> |
| 438 | ); |
| 439 | } |
| 440 | |
| 441 | function PRStatusBadge({ |
| 442 | state, |
| 443 | isMerged, |
| 444 | draft, |
| 445 | }: { |
| 446 | state: string; |
| 447 | isMerged: boolean; |
| 448 | draft: boolean; |
| 449 | }) { |
| 450 | if (draft) { |
| 451 | return ( |
| 452 | <Badge variant={"secondary"} className="gap-1"> |
| 453 | Draft |
| 454 | </Badge> |
| 455 | ); |
| 456 | } |
| 457 | |
| 458 | if (isMerged) { |
| 459 | return ( |
| 460 | <Badge |
| 461 | variant={"secondary"} |
| 462 | className="bg-purple-600/10 dark:text-purple-400 border-purple-500/20 border" |
| 463 | > |
| 464 | <GitMerge className="size-3" /> |
| 465 | Merged |
| 466 | </Badge> |
| 467 | ); |
| 468 | } |
| 469 | |
| 470 | if (state === "closed") { |
| 471 | return ( |
| 472 | <Badge variant={"destructive"} className="gap-1"> |
| 473 | <XCircle className="size-3" /> |
| 474 | Closed |
| 475 | </Badge> |
| 476 | ); |
| 477 | } |
| 478 | |
| 479 | if (state === "open") { |
| 480 | return ( |
| 481 | <Badge |
| 482 | variant={"secondary"} |
| 483 | className="gap-1 bg-emerald-600/10 dark:text-emerald-400 border-emerald-500/20 border" |
| 484 | > |
| 485 | <GitMerge className="size-3" /> |
| 486 | Open |
| 487 | </Badge> |
| 488 | ); |
| 489 | } |
| 490 | } |
| 491 | |
| 492 | function ReviewStatusBadge({ |
| 493 | status, |
| 494 | completedAt, |
| 495 | }: { |
| 496 | status: string | null; |
| 497 | completedAt?: Date | null; |
| 498 | }) { |
| 499 | const getTimeAgo = (date: Date) => { |
| 500 | const now = new Date(); |
| 501 | const diffMs = now.getTime() - new Date(date).getTime(); |
| 502 | const diffMin = Math.floor(diffMs / (1000 * 60)); |
| 503 | const diffHours = Math.floor(diffMin / 60); |
| 504 | const diffDays = Math.floor(diffHours / 24); |
| 505 | |
| 506 | if (diffMin < 1) return "just now"; |
| 507 | if (diffMin < 60) return `${diffMin} min ago`; |
| 508 | if (diffHours < 24) return `${diffHours}h ago`; |
| 509 | return `${diffDays}d ago`; |
| 510 | }; |
| 511 | |
| 512 | if (!status) { |
| 513 | return ( |
| 514 | <Badge |
| 515 | variant="outline" |
| 516 | className="gap-1.5 border bg-muted text-muted-foreground" |
| 517 | > |
| 518 | <Clock className="h-3 w-3" /> |
| 519 | Not reviewed |
| 520 | </Badge> |
| 521 | ); |
| 522 | } |
| 523 | |
| 524 | const config = { |
| 525 | COMPLETED: { |
| 526 | icon: CheckCircle, |
| 527 | label: completedAt |
| 528 | ? `AI Review completed · ${getTimeAgo(completedAt)}` |
| 529 | : "AI Review completed", |
| 530 | className: |
| 531 | "bg-emerald-500/10 text-emerald-600 dark:text-emerald-400 border-emerald-500/20", |
| 532 | }, |
| 533 | PROCESSING: { |
| 534 | icon: Loader2, |
| 535 | label: "Analyzing code…", |
| 536 | className: |
| 537 | "bg-blue-500/10 text-blue-600 dark:text-blue-400 border-blue-500/20", |
| 538 | spin: true, |
| 539 | }, |
| 540 | PENDING: { |
| 541 | icon: Clock, |
| 542 | label: "Queued for review", |
| 543 | className: |
| 544 | "bg-amber-500/10 text-amber-600 dark:text-amber-400 border-amber-500/20", |
| 545 | }, |
| 546 | FAILED: { |
| 547 | icon: XCircle, |
| 548 | label: "Review failed", |
| 549 | className: |
| 550 | "bg-red-500/10 text-red-600 dark:text-red-400 border-red-500/20", |
| 551 | }, |
| 552 | }[status] ?? { |
| 553 | icon: Clock, |
| 554 | label: "Not reviewed", |
| 555 | className: "bg-muted text-muted-foreground", |
| 556 | }; |
| 557 | |
| 558 | const Icon = config.icon; |
| 559 | |
| 560 | return ( |
| 561 | <Badge variant="outline" className={cn("gap-1.5 border", config.className)}> |
| 562 | <Icon className={cn("h-3 w-3", config.spin && "animate-spin")} /> |
| 563 | {config.label} |
| 564 | </Badge> |
| 565 | ); |
| 566 | } |