page.tsx388 lines, 13 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 | Clock, |
| 16 | Plus, |
| 17 | Minus, |
| 18 | FileText, |
| 19 | ExternalLink, |
| 20 | RefreshCw, |
| 21 | CheckCircle, |
| 22 | XCircle, |
| 23 | Loader2, |
| 24 | GitBranch, |
| 25 | Globe, |
| 26 | Lock, |
| 27 | } from "lucide-react"; |
| 28 | import { cn, formatDate } from "@/lib/utils"; |
| 29 | |
| 30 | type PageProps = { |
| 31 | params: Promise<{ id: string }>; |
| 32 | }; |
| 33 | |
| 34 | export default function RepositoryDetailPage({ params }: PageProps) { |
| 35 | const { id } = use(params); |
| 36 | const [prState, setPrState] = useState<"open" | "closed" | "all">("open"); |
| 37 | |
| 38 | const repository = trpc.repository.list.useQuery(undefined, { |
| 39 | select: (repos) => repos.find((r) => r.id === id), |
| 40 | }); |
| 41 | |
| 42 | const pullRequests = trpc.pullRequest.list.useQuery( |
| 43 | { repositoryId: id, state: prState }, |
| 44 | { enabled: !!id }, |
| 45 | ); |
| 46 | |
| 47 | const prCounts = { |
| 48 | open: pullRequests.data?.filter((pr) => pr.state === "open").length ?? 0, |
| 49 | closed: |
| 50 | pullRequests.data?.filter((pr) => pr.state === "closed").length ?? 0, |
| 51 | all: pullRequests.data?.length ?? 0, |
| 52 | }; |
| 53 | |
| 54 | if (repository.isLoading) { |
| 55 | return ( |
| 56 | <div className="space-y-8"> |
| 57 | <div className="flex items-center gap-4"> |
| 58 | <Skeleton className="size-9 rounded-lg" /> |
| 59 | <div className="space-y-2"> |
| 60 | <Skeleton className="h-7 w-64" /> |
| 61 | <Skeleton className="h-4 w-40" /> |
| 62 | </div> |
| 63 | </div> |
| 64 | <div className="space-y-3"> |
| 65 | {[...Array(3)].map((_, i) => ( |
| 66 | <Skeleton key={i} className="h-32 w-full rounded-xl" /> |
| 67 | ))} |
| 68 | </div> |
| 69 | </div> |
| 70 | ); |
| 71 | } |
| 72 | |
| 73 | if (!repository.data) { |
| 74 | return ( |
| 75 | <Card> |
| 76 | <CardContent className="py-16 text-center"> |
| 77 | <div className="mx-auto w-12 h-12 rounded-full bg-muted flex items-center justify-center"> |
| 78 | <GitBranch className="h-6 w-6 text-muted-foreground" /> |
| 79 | </div> |
| 80 | <p className="mt-4 font-medium">Repository not found</p> |
| 81 | <p className="text-sm text-muted-foreground mt-1"> |
| 82 | This repository may have been disconnected. |
| 83 | </p> |
| 84 | <Link href="/repos" className="mt-6 inline-block"> |
| 85 | <Button variant="outline"> |
| 86 | <ArrowLeft className="h-4 w-4" /> |
| 87 | Back to repositories |
| 88 | </Button> |
| 89 | </Link> |
| 90 | </CardContent> |
| 91 | </Card> |
| 92 | ); |
| 93 | } |
| 94 | |
| 95 | return ( |
| 96 | <div className="space-y-8"> |
| 97 | <div className="flex items-start justify-between gap-4"> |
| 98 | <div className="flex items-start gap-4"> |
| 99 | <Link href={"/repos"}> |
| 100 | <Button variant={"outline"} size={"icon"} className="shrink-0"> |
| 101 | <ArrowLeft className="h-4 w-4" /> |
| 102 | </Button> |
| 103 | </Link> |
| 104 | <div> |
| 105 | <div className="flex items-center gap-3"> |
| 106 | <h1 className="text-2xl font-semibold tracking-tight"> |
| 107 | {repository.data.fullName} |
| 108 | </h1> |
| 109 | <Badge variant={"outline"} className="gap-1"> |
| 110 | {repository.data.private ? ( |
| 111 | <> |
| 112 | <Lock className="size-3" /> |
| 113 | Private |
| 114 | </> |
| 115 | ) : ( |
| 116 | <> |
| 117 | <Globe className="size-3" /> |
| 118 | Public |
| 119 | </> |
| 120 | )} |
| 121 | </Badge> |
| 122 | </div> |
| 123 | <a |
| 124 | href={repository.data.htmlUrl} |
| 125 | target="_blank" |
| 126 | rel="noopener noreferrer" |
| 127 | className="text-sm text-muted-foreground hover:text-foreground transition-colors inline-flex items-center gap-1.5 mt-1" |
| 128 | > |
| 129 | View on GitHub |
| 130 | <ExternalLink className="size-3" /> |
| 131 | </a> |
| 132 | </div> |
| 133 | </div> |
| 134 | <Button |
| 135 | variant={"ghost"} |
| 136 | size={"icon-sm"} |
| 137 | onClick={() => pullRequests.refetch()} |
| 138 | disabled={pullRequests.isFetching} |
| 139 | > |
| 140 | <RefreshCw |
| 141 | className={cn("size-4", pullRequests.isFetching && "animate-spin")} |
| 142 | /> |
| 143 | </Button> |
| 144 | </div> |
| 145 | |
| 146 | <div className="border-b border-border/60"> |
| 147 | <div className="flex items-center gap-1"> |
| 148 | {(["open", "closed", "all"] as const).map((state) => ( |
| 149 | <button |
| 150 | key={state} |
| 151 | onClick={() => setPrState(state)} |
| 152 | className={cn( |
| 153 | "relative px-4 py-2.5 text-sm font-medium transition-colors", |
| 154 | prState === state |
| 155 | ? "text-foreground" |
| 156 | : "text-muted-foreground hover:text-foreground", |
| 157 | )} |
| 158 | > |
| 159 | <span className="flex items-center gap-2"> |
| 160 | {state === "open" && ( |
| 161 | <GitPullRequest className="size-4 text-emerald-500" /> |
| 162 | )} |
| 163 | {state === "closed" && ( |
| 164 | <GitMerge className="size-4 text-purple-500" /> |
| 165 | )} |
| 166 | {state === "all" && ( |
| 167 | <GitBranch className="size-4 text-muted-foreground" /> |
| 168 | )} |
| 169 | {state.charAt(0).toUpperCase() + state.slice(1)} |
| 170 | <span |
| 171 | className={cn( |
| 172 | "px-1.5 py-0.5 text-xs rounded-md tabular-nums", |
| 173 | prState === state |
| 174 | ? "bg-foreground/10 text-foreground" |
| 175 | : "bg-muted text-muted-foreground", |
| 176 | )} |
| 177 | > |
| 178 | {prCounts[state]} |
| 179 | </span> |
| 180 | </span> |
| 181 | {prState === state && ( |
| 182 | <span className="absolute bottom-0 left-0 right-0 h-0.5 bg-primary rounded-full" /> |
| 183 | )} |
| 184 | </button> |
| 185 | ))} |
| 186 | </div> |
| 187 | </div> |
| 188 | |
| 189 | <div className="space-y-3"> |
| 190 | {pullRequests.isLoading ? ( |
| 191 | [...Array(3)].map((_, i) => ( |
| 192 | <Skeleton key={i} className="h-32 w-full rounded-xl" /> |
| 193 | )) |
| 194 | ) : pullRequests.error ? ( |
| 195 | <Card className="border-destructive/50"> |
| 196 | <CardContent className="py-12 text-center"> |
| 197 | <div className="mx-auto size-12 rounded-full bg-destructive/10 flex items-center justify-center"> |
| 198 | <XCircle className="size-6 text-destructive" /> |
| 199 | </div> |
| 200 | <p className="mt-4 font-medium text-destructive"> |
| 201 | Failed to load pull requests. |
| 202 | </p> |
| 203 | <p className="text-sm text-muted-foreground mt-1"> |
| 204 | {pullRequests.error.message} |
| 205 | </p> |
| 206 | </CardContent> |
| 207 | </Card> |
| 208 | ) : pullRequests.data?.length === 0 ? ( |
| 209 | <Card> |
| 210 | <CardContent className="py-16 text-center"> |
| 211 | <div className="mx-auto size-12 rounded-full bg-muted flex items-center justify-center"> |
| 212 | <GitPullRequest className="size-6 text-muted-foreground" /> |
| 213 | </div> |
| 214 | <p className="mt-4 font-medium">No pull requests found.</p> |
| 215 | <p className="text-sm text-muted-foreground mt-1"> |
| 216 | {prState === "all" |
| 217 | ? "This repository has no pull requests yet." |
| 218 | : `No ${prState} pull requests found.`} |
| 219 | </p> |
| 220 | </CardContent> |
| 221 | </Card> |
| 222 | ) : ( |
| 223 | pullRequests.data?.map((pr) => ( |
| 224 | <PullRequestCard key={pr.id} pr={pr} repositoryId={id} /> |
| 225 | )) |
| 226 | )} |
| 227 | </div> |
| 228 | </div> |
| 229 | ); |
| 230 | } |
| 231 | |
| 232 | interface PullRequestCardProps { |
| 233 | pr: { |
| 234 | id: number; |
| 235 | number: number; |
| 236 | title: string; |
| 237 | state: "open" | "closed"; |
| 238 | draft: boolean; |
| 239 | htmlUrl: string; |
| 240 | author: { login: string; avatarUrl: string }; |
| 241 | headRef: string; |
| 242 | baseRef: string; |
| 243 | additions: number; |
| 244 | deletions: number; |
| 245 | changedFiles: number; |
| 246 | createdAt: string; |
| 247 | mergedAt: string | null; |
| 248 | review: { status: string; createdAt: Date } | null; |
| 249 | }; |
| 250 | repositoryId: string; |
| 251 | } |
| 252 | |
| 253 | function PullRequestCard({ pr, repositoryId }: PullRequestCardProps) { |
| 254 | const isMerged = pr.state === "closed" && pr.mergedAt !== null; |
| 255 | |
| 256 | return ( |
| 257 | <Card className="group hover:border-border transition-all"> |
| 258 | <CardContent className="p-5"> |
| 259 | <div className="flex items-start justify-between gap-6"> |
| 260 | <div className="flex items-start gap-4 min-w-0 flex-1"> |
| 261 | <div> |
| 262 | {isMerged ? ( |
| 263 | <GitMerge className="size-4 text-purple-500" /> |
| 264 | ) : pr.state === "closed" ? ( |
| 265 | <XCircle className="size-4 text-red-500" /> |
| 266 | ) : ( |
| 267 | <GitPullRequest className="size-4 text-emerald-500" /> |
| 268 | )} |
| 269 | </div> |
| 270 | |
| 271 | <div className="min-w-0 flex-1 space-y-2"> |
| 272 | <div> |
| 273 | <Link |
| 274 | href={`/repos/${repositoryId}/pr/${pr.number}`} |
| 275 | className="font-medium hover:text-primary transition-colors line-clamp-1" |
| 276 | > |
| 277 | {pr.title} |
| 278 | </Link> |
| 279 | {pr.draft && ( |
| 280 | <Badge variant={"secondary"} className="text-xs"> |
| 281 | Draft |
| 282 | </Badge> |
| 283 | )} |
| 284 | </div> |
| 285 | |
| 286 | <div className="flex items-center gap-3 text-sm text-muted-foreground"> |
| 287 | <span className="font-mono text-xs">#{pr.number}</span> |
| 288 | <span className="text-muted-foreground/40">•</span> |
| 289 | <span className="flex items-center gap-1.5"> |
| 290 | <Avatar className="size-4 ring-1 ring-border"> |
| 291 | <AvatarImage |
| 292 | src={pr.author.avatarUrl} |
| 293 | alt={pr.author.login} |
| 294 | /> |
| 295 | <AvatarFallback className="text-[10px]"> |
| 296 | {pr.author.login?.[0]?.toUpperCase() || "?"} |
| 297 | </AvatarFallback> |
| 298 | </Avatar> |
| 299 | {pr.author.login} |
| 300 | </span> |
| 301 | <span className="text-muted-foreground/40">•</span> |
| 302 | <span className="flex items-center gap-1"> |
| 303 | <Clock className="size-3" /> |
| 304 | {formatDate(pr.createdAt)} |
| 305 | </span> |
| 306 | </div> |
| 307 | |
| 308 | <div className="flex items-center gap-4 text-sm"> |
| 309 | <code className="px-2 py-0.5 rounded-md bg-muted text-xs font-mono text-muted-foreground flex items-center truncate"> |
| 310 | {pr.baseRef} |
| 311 | <ArrowLeft className="mx-1.5 size-3 text-muted-foreground/50" /> |
| 312 | {pr.headRef} |
| 313 | </code> |
| 314 | <div className="flex items-center gap-3"> |
| 315 | <span className="flex items-center gap-1 text-emerald-600 dark:text-emerald-400"> |
| 316 | <Plus className="size-3" /> |
| 317 | <span>{pr.additions}</span> |
| 318 | </span> |
| 319 | <span className="flex items-center gap-1 text-red-600 dark:text-red-400"> |
| 320 | <Minus className="size-3" /> |
| 321 | <span>{pr.deletions}</span> |
| 322 | </span> |
| 323 | <span className="flex items-center gap-1 text-muted-foreground"> |
| 324 | <FileText className="size-3" /> |
| 325 | <span>{pr.changedFiles}</span> |
| 326 | files |
| 327 | </span> |
| 328 | </div> |
| 329 | </div> |
| 330 | </div> |
| 331 | </div> |
| 332 | |
| 333 | <div className="flex items-center gap-3 shrink-0"> |
| 334 | {pr.review && <ReviewStatusBadge status={pr.review.status} />} |
| 335 | <Link href={`/repos/${repositoryId}/pr/${pr.number}`}> |
| 336 | <Button variant={pr.review ? "outline" : "default"}> |
| 337 | {pr.review ? "View" : "Review"} |
| 338 | </Button> |
| 339 | </Link> |
| 340 | </div> |
| 341 | </div> |
| 342 | </CardContent> |
| 343 | </Card> |
| 344 | ); |
| 345 | } |
| 346 | |
| 347 | function ReviewStatusBadge({ status }: { status: string }) { |
| 348 | const config = { |
| 349 | COMPLETED: { |
| 350 | icon: CheckCircle, |
| 351 | label: "Reviewed", |
| 352 | className: |
| 353 | "bg-emerald-500/10 text-emerald-600 dark:text-emerald-400 border-emerald-500/20", |
| 354 | }, |
| 355 | PROCESSING: { |
| 356 | icon: Loader2, |
| 357 | label: "Analyzing", |
| 358 | className: |
| 359 | "bg-blue-500/10 text-blue-600 dark:text-blue-400 border-blue-500/20", |
| 360 | spin: true, |
| 361 | }, |
| 362 | PENDING: { |
| 363 | icon: Clock, |
| 364 | label: "Queued", |
| 365 | className: |
| 366 | "bg-amber-500/10 text-amber-600 dark:text-amber-400 border-amber-500/20", |
| 367 | }, |
| 368 | FAILED: { |
| 369 | icon: XCircle, |
| 370 | label: "Failed", |
| 371 | className: |
| 372 | "bg-red-500/10 text-red-600 dark:text-red-400 border-red-500/20", |
| 373 | }, |
| 374 | }[status] ?? { |
| 375 | icon: Clock, |
| 376 | label: "Pending", |
| 377 | className: "bg-muted text-muted-foreground", |
| 378 | }; |
| 379 | |
| 380 | const Icon = config.icon; |
| 381 | |
| 382 | return ( |
| 383 | <Badge className={config.className}> |
| 384 | <Icon className={cn("size-3", config.spin && "animate-spin")} /> |
| 385 | {config.label} |
| 386 | </Badge> |
| 387 | ); |
| 388 | } |