page.tsx435 lines, 13 KB
| 1 | "use client"; |
| 2 | |
| 3 | import { 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 { |
| 11 | GitPullRequest, |
| 12 | Clock, |
| 13 | CheckCircle, |
| 14 | XCircle, |
| 15 | Loader2, |
| 16 | AlertTriangle, |
| 17 | FileText, |
| 18 | ExternalLink, |
| 19 | RefreshCw, |
| 20 | } from "lucide-react"; |
| 21 | import { cn } from "@/lib/utils"; |
| 22 | |
| 23 | type ReviewStatus = "all" | "COMPLETED" | "PROCESSING" | "PENDING" | "FAILED"; |
| 24 | |
| 25 | export default function ReviewsPage() { |
| 26 | const [statusFilter, setStatusFilter] = useState<ReviewStatus>("all"); |
| 27 | |
| 28 | const reviews = trpc.review.list.useQuery( |
| 29 | { limit: 50 }, |
| 30 | { |
| 31 | refetchInterval: (query) => { |
| 32 | const hasProcessing = query.state.data?.some( |
| 33 | (r) => r.status === "PENDING" || r.status === "PROCESSING", |
| 34 | ); |
| 35 | return hasProcessing ? 3000 : false; |
| 36 | }, |
| 37 | }, |
| 38 | ); |
| 39 | |
| 40 | const triggerReview = trpc.review.trigger.useMutation({ |
| 41 | onSuccess: () => { |
| 42 | reviews.refetch(); |
| 43 | }, |
| 44 | }); |
| 45 | |
| 46 | const filteredReviews = reviews.data?.filter( |
| 47 | (r) => statusFilter === "all" || r.status === statusFilter, |
| 48 | ); |
| 49 | |
| 50 | const statusCounts = { |
| 51 | all: reviews.data?.length ?? 0, |
| 52 | COMPLETED: |
| 53 | reviews.data?.filter((r) => r.status === "COMPLETED").length ?? 0, |
| 54 | PROCESSING: |
| 55 | reviews.data?.filter((r) => r.status === "PROCESSING").length ?? 0, |
| 56 | PENDING: reviews.data?.filter((r) => r.status === "PENDING").length ?? 0, |
| 57 | FAILED: reviews.data?.filter((r) => r.status === "FAILED").length ?? 0, |
| 58 | }; |
| 59 | |
| 60 | return ( |
| 61 | <div className="space-y-8"> |
| 62 | <div className="flex items-center justify-between"> |
| 63 | <div> |
| 64 | <h1 className="text-2xl font-semibold tracking-tight">Reviews</h1> |
| 65 | <p className="text-muted-foreground mt-1"> |
| 66 | {statusCounts.all} total reviews |
| 67 | </p> |
| 68 | </div> |
| 69 | <Button |
| 70 | variant={"ghost"} |
| 71 | size={"icon-sm"} |
| 72 | onClick={() => reviews.refetch()} |
| 73 | disabled={reviews.isFetching} |
| 74 | > |
| 75 | <RefreshCw |
| 76 | className={cn("size-4", reviews.isFetching && "animate-spin")} |
| 77 | /> |
| 78 | </Button> |
| 79 | </div> |
| 80 | |
| 81 | <div className="flex items-center gap-2 flex-wrap p-1 bg-muted/50 rounded-lg w-fit"> |
| 82 | {(["all", "COMPLETED", "PROCESSING", "PENDING", "FAILED"] as const).map( |
| 83 | (status) => ( |
| 84 | <button |
| 85 | key={status} |
| 86 | onClick={() => setStatusFilter(status)} |
| 87 | className={cn( |
| 88 | "flex items-center gap-2 px-3 py-1.5 rounded-md text-sm font-medium transition-all", |
| 89 | statusFilter === status |
| 90 | ? "bg-background text-foreground shadow-sm" |
| 91 | : "text-muted-foreground hover:text-foreground", |
| 92 | )} |
| 93 | > |
| 94 | <StatusIcon status={status} className="size-3.5" /> |
| 95 | {status === "all" |
| 96 | ? "All" |
| 97 | : status.charAt(0) + status.slice(1).toLowerCase()} |
| 98 | <span |
| 99 | className={cn( |
| 100 | "ml-1 text-xs tabular-nums", |
| 101 | statusFilter === status |
| 102 | ? "text-muted-foreground" |
| 103 | : "text-muted-foreground/70", |
| 104 | )} |
| 105 | > |
| 106 | {statusCounts[status]} |
| 107 | </span> |
| 108 | </button> |
| 109 | ), |
| 110 | )} |
| 111 | </div> |
| 112 | |
| 113 | {reviews.isLoading ? ( |
| 114 | <div> |
| 115 | {[...Array(5)].map((_, i) => ( |
| 116 | <Skeleton key={i} className="h-28 w-full rounded-xl" /> |
| 117 | ))} |
| 118 | </div> |
| 119 | ) : reviews.error ? ( |
| 120 | <Card className="border-destructive/50"> |
| 121 | <CardContent className="py-8 text-center"> |
| 122 | <p className="text-destructive">{reviews.error.message}</p> |
| 123 | </CardContent> |
| 124 | </Card> |
| 125 | ) : filteredReviews?.length === 0 ? ( |
| 126 | <Card> |
| 127 | <CardContent className="py-16 text-center"> |
| 128 | <div className="mx-auto w-12 h-12 rounded-full bg-muted flex items-center justify-center"> |
| 129 | <FileText className="h-6 w-6 text-muted-foreground" /> |
| 130 | </div> |
| 131 | <p className="mt-4 font-medium"> |
| 132 | {statusFilter === "all" |
| 133 | ? "No reviews yet" |
| 134 | : `No ${statusFilter.toLowerCase()} reviews`} |
| 135 | </p> |
| 136 | <p className="text-sm text-muted-foreground mt-1"> |
| 137 | {statusFilter === "all" && |
| 138 | "Run your first AI review on a pull request!"} |
| 139 | </p> |
| 140 | </CardContent> |
| 141 | </Card> |
| 142 | ) : ( |
| 143 | <div className="space-y-3"> |
| 144 | {filteredReviews?.map((review) => ( |
| 145 | <ReviewCard |
| 146 | key={review.id} |
| 147 | review={review} |
| 148 | onRetry={ |
| 149 | review.status === "FAILED" |
| 150 | ? () => |
| 151 | triggerReview.mutate({ |
| 152 | repositoryId: review.repository.id, |
| 153 | prNumber: review.prNumber, |
| 154 | }) |
| 155 | : undefined |
| 156 | } |
| 157 | /> |
| 158 | ))} |
| 159 | </div> |
| 160 | )} |
| 161 | </div> |
| 162 | ); |
| 163 | } |
| 164 | |
| 165 | interface ReviewCardProps { |
| 166 | review: { |
| 167 | id: string; |
| 168 | prNumber: number; |
| 169 | prTitle: string; |
| 170 | prUrl: string; |
| 171 | status: string; |
| 172 | summary: string | null; |
| 173 | riskScore: number | null; |
| 174 | comments: unknown; |
| 175 | error: string | null; |
| 176 | createdAt: Date; |
| 177 | repository: { |
| 178 | id: string; |
| 179 | fullName: string; |
| 180 | }; |
| 181 | }; |
| 182 | onRetry?: () => void; |
| 183 | } |
| 184 | |
| 185 | function ReviewCard({ review, onRetry }: ReviewCardProps) { |
| 186 | const commentCount = Array.isArray(review.comments) |
| 187 | ? review.comments.length |
| 188 | : 0; |
| 189 | |
| 190 | const getStatusMessage = () => { |
| 191 | switch (review.status) { |
| 192 | case "PENDING": |
| 193 | return "Queued — will start shortly"; |
| 194 | case "PROCESSING": |
| 195 | return "Analyzing code…"; |
| 196 | case "FAILED": |
| 197 | return review.error || "Analysis failed"; |
| 198 | default: |
| 199 | return null; |
| 200 | } |
| 201 | }; |
| 202 | |
| 203 | const statusMessage = getStatusMessage(); |
| 204 | |
| 205 | return ( |
| 206 | <Card className="group hover:border-border transition-colors"> |
| 207 | <CardContent className="p-4"> |
| 208 | <div className="flex items-start justify-between gap-4"> |
| 209 | <div className="flex items-start gap-4 min-w-0 flex-1"> |
| 210 | <div |
| 211 | className={cn( |
| 212 | "mt-1 p-2 rounded-lg shrink-0", |
| 213 | getStatusBg(review.status), |
| 214 | )} |
| 215 | > |
| 216 | <StatusIcon status={review.status} className="size-4" /> |
| 217 | </div> |
| 218 | <div className="min-w-0 flex-1 space-y-1"> |
| 219 | <div className="flex items-center gap-2 flex-wrap"> |
| 220 | <Link |
| 221 | href={`/repos/${review.repository.id}/pr/${review.prNumber}`} |
| 222 | className="font-medium hover:text-primary transition-colors truncate" |
| 223 | > |
| 224 | {review.prTitle} |
| 225 | </Link> |
| 226 | <StatusBadge status={review.status} /> |
| 227 | </div> |
| 228 | <div className="flex items-center gap-3 text-sm text-muted-foreground"> |
| 229 | <span className="font-medium"> |
| 230 | {review.repository.fullName} |
| 231 | </span> |
| 232 | <span className="text-muted-foreground/50">•</span> |
| 233 | <span>#{review.prNumber}</span> |
| 234 | <span className="text-muted-foreground/50">•</span> |
| 235 | <span className="flex items-center gap-1"> |
| 236 | <Clock className="h-3 w-3" /> |
| 237 | {formatRelativeTime(review.createdAt)} |
| 238 | </span> |
| 239 | </div> |
| 240 | {review.status === "COMPLETED" && ( |
| 241 | <div className="flex items-center gap-4 pt-1"> |
| 242 | {review.riskScore !== null && ( |
| 243 | <RiskScoreBadge score={review.riskScore} /> |
| 244 | )} |
| 245 | {commentCount > 0 && ( |
| 246 | <span className="flex items-center gap-1.5 text-sm text-muted-foreground"> |
| 247 | <AlertTriangle className="size-3.5" /> |
| 248 | {commentCount}{" "} |
| 249 | {commentCount === 1 ? "comment" : "comments"} |
| 250 | </span> |
| 251 | )} |
| 252 | </div> |
| 253 | )} |
| 254 | {review.summary && review.status === "COMPLETED" && ( |
| 255 | <p className="text-sm text-muted-foreground line-clamp-2 pt-1"> |
| 256 | {review.summary} |
| 257 | </p> |
| 258 | )} |
| 259 | {statusMessage && review.status !== "COMPLETED" && ( |
| 260 | <p |
| 261 | className={cn( |
| 262 | "text-sm pt-1", |
| 263 | review.status === "FAILED" |
| 264 | ? "text-red-600 dark:text-red-400" |
| 265 | : "text-muted-foreground", |
| 266 | )} |
| 267 | > |
| 268 | {statusMessage} |
| 269 | </p> |
| 270 | )} |
| 271 | </div> |
| 272 | </div> |
| 273 | <div className="flex items-center gap-2 shrink-0"> |
| 274 | <a |
| 275 | href={review.prUrl} |
| 276 | target="_blank" |
| 277 | rel="noopener noreferrer" |
| 278 | className="p-2 text-muted-foreground hover:text-foreground transition-colors rounded-md hover:bg-muted" |
| 279 | > |
| 280 | <ExternalLink className="size-4" /> |
| 281 | </a> |
| 282 | {review.status === "FAILED" && onRetry ? ( |
| 283 | <Button onClick={onRetry}>Retry</Button> |
| 284 | ) : ( |
| 285 | <Link |
| 286 | href={`/repos/${review.repository.id}/pr/${review.prNumber}`} |
| 287 | > |
| 288 | <Button size={"sm"} variant={"outline"}> |
| 289 | {review.status === "COMPLETED" ? "Completed" : "Pending"} |
| 290 | </Button> |
| 291 | </Link> |
| 292 | )} |
| 293 | </div> |
| 294 | </div> |
| 295 | </CardContent> |
| 296 | </Card> |
| 297 | ); |
| 298 | } |
| 299 | |
| 300 | function getStatusBg(status: string) { |
| 301 | switch (status) { |
| 302 | case "COMPLETED": |
| 303 | return "bg-emerald-500/10"; |
| 304 | case "PROCESSING": |
| 305 | return "bg-blue-500/10"; |
| 306 | case "PENDING": |
| 307 | return "bg-amber-500/10"; |
| 308 | case "FAILED": |
| 309 | return "bg-red-500/10"; |
| 310 | default: |
| 311 | return "bg-muted"; |
| 312 | } |
| 313 | } |
| 314 | |
| 315 | function StatusBadge({ status }: { status: string }) { |
| 316 | const variants: Record< |
| 317 | string, |
| 318 | "success" | "info" | "warning" | "destructive" |
| 319 | > = { |
| 320 | COMPLETED: "success", |
| 321 | PROCESSING: "info", |
| 322 | PENDING: "warning", |
| 323 | FAILED: "destructive", |
| 324 | }; |
| 325 | |
| 326 | return ( |
| 327 | <Badge variant={variants[status] || "secondary"}> |
| 328 | {status.charAt(0) + status.slice(1).toLowerCase()} |
| 329 | </Badge> |
| 330 | ); |
| 331 | } |
| 332 | |
| 333 | function StatusIcon({ |
| 334 | status, |
| 335 | className, |
| 336 | }: { |
| 337 | status: string; |
| 338 | className?: string; |
| 339 | }) { |
| 340 | switch (status) { |
| 341 | case "COMPLETED": |
| 342 | return ( |
| 343 | <CheckCircle |
| 344 | className={cn("text-emerald-600 dark:text-emerald-400", className)} |
| 345 | /> |
| 346 | ); |
| 347 | case "PROCESSING": |
| 348 | return ( |
| 349 | <Loader2 |
| 350 | className={cn( |
| 351 | "text-blue-600 dark:text-blue-400 animate-spin", |
| 352 | className, |
| 353 | )} |
| 354 | /> |
| 355 | ); |
| 356 | case "PENDING": |
| 357 | return ( |
| 358 | <Clock |
| 359 | className={cn("text-amber-600 dark:text-amber-400", className)} |
| 360 | /> |
| 361 | ); |
| 362 | case "FAILED": |
| 363 | return ( |
| 364 | <XCircle className={cn("text-red-600 dark:text-red-400", className)} /> |
| 365 | ); |
| 366 | default: |
| 367 | return ( |
| 368 | <GitPullRequest className={cn("text-muted-foreground", className)} /> |
| 369 | ); |
| 370 | } |
| 371 | } |
| 372 | |
| 373 | function RiskScoreBadge({ score }: { score: number }) { |
| 374 | const config = getRiskConfig(score); |
| 375 | |
| 376 | return ( |
| 377 | <span |
| 378 | className={cn( |
| 379 | "inline-flex items-center gap-1.5 text-sm font-medium", |
| 380 | config.textColor, |
| 381 | )} |
| 382 | > |
| 383 | <span className={cn("w-2 h-2 rounded-full", config.barColor)} /> |
| 384 | {config.label} |
| 385 | <span className="text-muted-foreground font-normal tabular-nums"> |
| 386 | {score} |
| 387 | </span> |
| 388 | </span> |
| 389 | ); |
| 390 | } |
| 391 | |
| 392 | function getRiskConfig(score: number) { |
| 393 | if (score < 25) |
| 394 | return { |
| 395 | label: "Low", |
| 396 | textColor: "text-emerald-600 dark:text-emerald-400", |
| 397 | barColor: "bg-emerald-500", |
| 398 | }; |
| 399 | if (score < 50) |
| 400 | return { |
| 401 | label: "Medium", |
| 402 | textColor: "text-amber-600 dark:text-amber-400", |
| 403 | barColor: "bg-amber-500", |
| 404 | }; |
| 405 | if (score < 75) |
| 406 | return { |
| 407 | label: "High", |
| 408 | textColor: "text-orange-600 dark:text-orange-400", |
| 409 | barColor: "bg-orange-500", |
| 410 | }; |
| 411 | return { |
| 412 | label: "Critical", |
| 413 | textColor: "text-red-600 dark:text-red-400", |
| 414 | barColor: "bg-red-500", |
| 415 | }; |
| 416 | } |
| 417 | |
| 418 | function formatRelativeTime(date: Date): string { |
| 419 | const now = new Date(); |
| 420 | const diffMs = now.getTime() - new Date(date).getTime(); |
| 421 | const diffMins = Math.floor(diffMs / (1000 * 60)); |
| 422 | const diffHours = Math.floor(diffMs / (1000 * 60 * 60)); |
| 423 | const diffDays = Math.floor(diffMs / (1000 * 60 * 60 * 24)); |
| 424 | |
| 425 | if (diffMins < 1) return "Just now"; |
| 426 | if (diffMins < 60) return `${diffMins}m ago`; |
| 427 | if (diffHours < 24) return `${diffHours}h ago`; |
| 428 | if (diffDays === 1) return "Yesterday"; |
| 429 | if (diffDays < 7) return `${diffDays}d ago`; |
| 430 | |
| 431 | return new Date(date).toLocaleDateString("en-US", { |
| 432 | month: "short", |
| 433 | day: "numeric", |
| 434 | }); |
| 435 | } |