page.tsx559 lines, 18 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 { Checkbox } from "@/components/ui/checkbox"; |
| 9 | import { Badge } from "@/components/ui/badge"; |
| 10 | import { Skeleton } from "@/components/ui/skeleton"; |
| 11 | import { |
| 12 | AlertDialog, |
| 13 | AlertDialogAction, |
| 14 | AlertDialogCancel, |
| 15 | AlertDialogContent, |
| 16 | AlertDialogDescription, |
| 17 | AlertDialogFooter, |
| 18 | AlertDialogHeader, |
| 19 | AlertDialogTitle, |
| 20 | AlertDialogTrigger, |
| 21 | } from "@/components/ui/alert-dialog"; |
| 22 | import { |
| 23 | GitBranch, |
| 24 | Lock, |
| 25 | Globe, |
| 26 | RefreshCw, |
| 27 | Plus, |
| 28 | Trash2, |
| 29 | ArrowRight, |
| 30 | Star, |
| 31 | GitPullRequest, |
| 32 | Search, |
| 33 | X, |
| 34 | CheckCircle, |
| 35 | FolderGit2, |
| 36 | } from "lucide-react"; |
| 37 | import { cn } from "@/lib/utils"; |
| 38 | import { Input } from "@/components/ui/input"; |
| 39 | import { ConnectGithub } from "@/components/connect-github"; |
| 40 | |
| 41 | interface GitHubRepo { |
| 42 | githubId: number; |
| 43 | name: string; |
| 44 | fullName: string; |
| 45 | private: boolean; |
| 46 | htmlUrl: string; |
| 47 | description: string | null; |
| 48 | language: string | null; |
| 49 | stars: number; |
| 50 | updatedAt: string; |
| 51 | } |
| 52 | |
| 53 | const languageColors: Record<string, string> = { |
| 54 | TypeScript: "bg-blue-500", |
| 55 | JavaScript: "bg-yellow-400", |
| 56 | Python: "bg-green-500", |
| 57 | Go: "bg-cyan-500", |
| 58 | Rust: "bg-orange-500", |
| 59 | Java: "bg-red-500", |
| 60 | Ruby: "bg-red-400", |
| 61 | PHP: "bg-purple-500", |
| 62 | "C#": "bg-green-600", |
| 63 | "C++": "bg-pink-500", |
| 64 | C: "bg-gray-500", |
| 65 | Swift: "bg-orange-400", |
| 66 | Kotlin: "bg-purple-400", |
| 67 | Dart: "bg-blue-400", |
| 68 | Vue: "bg-emerald-500", |
| 69 | Svelte: "bg-orange-600", |
| 70 | }; |
| 71 | |
| 72 | export default function ReposPage() { |
| 73 | const [selectedRepos, setSelectedRepos] = useState<Set<number>>(new Set()); |
| 74 | const [showGitHubRepos, setShowGitHubRepos] = useState(false); |
| 75 | const [searchQuery, setSearchQuery] = useState(""); |
| 76 | |
| 77 | const connectedRepos = trpc.repository.list.useQuery(); |
| 78 | const githubRepos = trpc.repository.fetchFromGithub.useQuery(undefined, { |
| 79 | enabled: showGitHubRepos, |
| 80 | }); |
| 81 | |
| 82 | const connectMutation = trpc.repository.connect.useMutation({ |
| 83 | onSuccess: () => { |
| 84 | connectedRepos.refetch(); |
| 85 | setSelectedRepos(new Set()); |
| 86 | setShowGitHubRepos(false); |
| 87 | }, |
| 88 | }); |
| 89 | |
| 90 | const disconnectMutation = trpc.repository.disconnect.useMutation({ |
| 91 | onSuccess: () => { |
| 92 | connectedRepos.refetch(); |
| 93 | }, |
| 94 | }); |
| 95 | |
| 96 | const connectedIds = new Set( |
| 97 | connectedRepos.data?.map((repo) => repo.githubId) || [], |
| 98 | ); |
| 99 | |
| 100 | const availableRepos = |
| 101 | githubRepos.data?.filter((repo) => !connectedIds.has(repo.githubId)) || []; |
| 102 | |
| 103 | const filteredAvailableRepos = availableRepos.filter( |
| 104 | (repo) => |
| 105 | repo.name.toLowerCase().includes(searchQuery.toLowerCase()) || |
| 106 | repo.description?.toLowerCase().includes(searchQuery.toLowerCase()), |
| 107 | ); |
| 108 | |
| 109 | const toggleRepo = (githubId: number) => { |
| 110 | const next = new Set(selectedRepos); |
| 111 | if (next.has(githubId)) { |
| 112 | next.delete(githubId); |
| 113 | } else { |
| 114 | next.add(githubId); |
| 115 | } |
| 116 | setSelectedRepos(next); |
| 117 | }; |
| 118 | |
| 119 | const handleConnect = () => { |
| 120 | const reposToConnect = availableRepos |
| 121 | .filter((r) => selectedRepos.has(r.githubId)) |
| 122 | .map((r) => ({ |
| 123 | githubId: r.githubId, |
| 124 | name: r.name, |
| 125 | fullName: r.fullName, |
| 126 | private: r.private, |
| 127 | htmlUrl: r.htmlUrl, |
| 128 | })); |
| 129 | connectMutation.mutate({ repos: reposToConnect }); |
| 130 | }; |
| 131 | |
| 132 | const selectAll = () => { |
| 133 | setSelectedRepos(new Set(filteredAvailableRepos.map((r) => r.githubId))); |
| 134 | }; |
| 135 | |
| 136 | const clearSelection = () => { |
| 137 | setSelectedRepos(new Set()); |
| 138 | }; |
| 139 | |
| 140 | return ( |
| 141 | <div className="space-y-8"> |
| 142 | <div className="flex items-start justify-between gap-4"> |
| 143 | <div> |
| 144 | <h1 className="text-2xl font-semibold tracking-tight"> |
| 145 | Repositories |
| 146 | </h1> |
| 147 | <p className="text-muted-foreground mt-1"> |
| 148 | Select repositories to connect to your account. |
| 149 | </p> |
| 150 | </div> |
| 151 | <Button |
| 152 | onClick={() => { |
| 153 | setShowGitHubRepos(!showGitHubRepos); |
| 154 | setSearchQuery(""); |
| 155 | setSelectedRepos(new Set()); |
| 156 | }} |
| 157 | variant={showGitHubRepos ? "outline" : "default"} |
| 158 | > |
| 159 | {showGitHubRepos ? ( |
| 160 | <> |
| 161 | <X className="size-4" /> |
| 162 | Cancel |
| 163 | </> |
| 164 | ) : ( |
| 165 | <> |
| 166 | <Plus className="size-4" /> |
| 167 | Add Repository |
| 168 | </> |
| 169 | )} |
| 170 | </Button> |
| 171 | </div> |
| 172 | |
| 173 | {showGitHubRepos && ( |
| 174 | <Card className="overflow-hidden"> |
| 175 | <div className="border-b border-border/60 bg-muted/30 px-6 py-4"> |
| 176 | <div className="flex items-center justify-between"> |
| 177 | <div> |
| 178 | <h2 className="font-semibold">Import GitHub Repositories</h2> |
| 179 | <p className="text-sm text-muted-foreground mt-0.5"> |
| 180 | Select repositories to import from GitHub. |
| 181 | </p> |
| 182 | </div> |
| 183 | <Button |
| 184 | variant={"ghost"} |
| 185 | size={"icon-sm"} |
| 186 | onClick={() => githubRepos.refetch()} |
| 187 | disabled={githubRepos.isFetching} |
| 188 | > |
| 189 | <RefreshCw |
| 190 | className={cn( |
| 191 | "size-4", |
| 192 | githubRepos.isFetching && "animate-spin", |
| 193 | )} |
| 194 | /> |
| 195 | </Button> |
| 196 | </div> |
| 197 | </div> |
| 198 | |
| 199 | <CardContent className="p-0"> |
| 200 | {githubRepos.isLoading ? ( |
| 201 | <div className="p-6 space-y-3"> |
| 202 | {[...Array(4)].map((_, i) => ( |
| 203 | <Skeleton key={i} className="h-16 w-full rounded-lg" /> |
| 204 | ))} |
| 205 | </div> |
| 206 | ) : githubRepos.error ? ( |
| 207 | <div className="p-6"> |
| 208 | {githubRepos.error.data?.code === "PRECONDITION_FAILED" ? ( |
| 209 | <ConnectGithub |
| 210 | title="Github account not connected" |
| 211 | description="Connect your Github account to view your repositories." |
| 212 | /> |
| 213 | ) : ( |
| 214 | <div className="rounded-lg bg-destructive/10 border border-destructive/20 p-4 text-center"> |
| 215 | <p className="text-sm text-destructive"> |
| 216 | {githubRepos.error.message} |
| 217 | </p> |
| 218 | </div> |
| 219 | )} |
| 220 | </div> |
| 221 | ) : availableRepos.length === 0 ? ( |
| 222 | <div className="py-16 text-center"> |
| 223 | <div className="mx-auto size-12 rounded-full bg-emerald-500/10 flex items-center justify-center"> |
| 224 | <CheckCircle className="size-6 text-emerald-500" /> |
| 225 | </div> |
| 226 | <p className="mt-4 font-medium">All caught up!</p> |
| 227 | <p className="text-sm text-muted-foreground mt-1"> |
| 228 | All your repos are already connected! |
| 229 | </p> |
| 230 | </div> |
| 231 | ) : ( |
| 232 | <> |
| 233 | <div className="px-6 py-4 border-b border-border/60 flex items-center gap-4"> |
| 234 | <div className="relative flex-1"> |
| 235 | <Search className="absolute left-3 top-1/2 -translate-y-1/2 size-4 text-muted-foreground" /> |
| 236 | <Input |
| 237 | placeholder="Search repos" |
| 238 | value={searchQuery} |
| 239 | onChange={(e) => setSearchQuery(e.target.value)} |
| 240 | className="pl-10" |
| 241 | /> |
| 242 | </div> |
| 243 | <div className="flex items-center gap-2 text-sm"> |
| 244 | <button |
| 245 | onClick={selectAll} |
| 246 | className="text-muted-foreground hover:text-foreground transition-colors" |
| 247 | > |
| 248 | Select all |
| 249 | </button> |
| 250 | {selectedRepos.size > 0 && ( |
| 251 | <> |
| 252 | <span className="text-muted-foreground">•</span> |
| 253 | <button |
| 254 | onClick={clearSelection} |
| 255 | className="text-muted-foreground hover:text-foreground transition-colors" |
| 256 | > |
| 257 | Clear |
| 258 | </button> |
| 259 | </> |
| 260 | )} |
| 261 | </div> |
| 262 | </div> |
| 263 | |
| 264 | <div className="max-h-[400px] overflow-y-auto"> |
| 265 | {filteredAvailableRepos.length === 0 ? ( |
| 266 | <div className="py-12 text-center"> |
| 267 | <p className="text-sm text-muted-foreground"> |
| 268 | {" "} |
| 269 | No repositories match your search. |
| 270 | </p> |
| 271 | </div> |
| 272 | ) : ( |
| 273 | <div className="divide-y divide-border/60"> |
| 274 | {filteredAvailableRepos.map((repo) => ( |
| 275 | <RepoSelectItem |
| 276 | key={repo.githubId} |
| 277 | repo={repo} |
| 278 | selected={selectedRepos.has(repo.githubId)} |
| 279 | onToggle={() => toggleRepo(repo.githubId)} |
| 280 | /> |
| 281 | ))} |
| 282 | </div> |
| 283 | )} |
| 284 | </div> |
| 285 | |
| 286 | <div className="px-6 py-4 border-t border-border/60 bg-muted/60 flex items-center justify-between"> |
| 287 | <p className="text-sm text-muted-foreground"> |
| 288 | {selectedRepos.size} of {filteredAvailableRepos.length}{" "} |
| 289 | selected |
| 290 | </p> |
| 291 | <Button |
| 292 | onClick={handleConnect} |
| 293 | disabled={ |
| 294 | selectedRepos.size === 0 || connectMutation.isPending |
| 295 | } |
| 296 | > |
| 297 | {connectMutation.isPending ? ( |
| 298 | <> |
| 299 | <RefreshCw className="size-4 animate-spin" /> |
| 300 | Connecting... |
| 301 | </> |
| 302 | ) : ( |
| 303 | <> |
| 304 | {connectMutation.isPending ? ( |
| 305 | <> |
| 306 | <RefreshCw className="h-4 w-4 animate-spin" /> |
| 307 | Connecting... |
| 308 | </> |
| 309 | ) : ( |
| 310 | <> |
| 311 | Connect |
| 312 | {selectedRepos.size > 0 && |
| 313 | ` (${selectedRepos.size})`} |
| 314 | </> |
| 315 | )} |
| 316 | </> |
| 317 | )} |
| 318 | </Button> |
| 319 | </div> |
| 320 | </> |
| 321 | )} |
| 322 | </CardContent> |
| 323 | </Card> |
| 324 | )} |
| 325 | |
| 326 | <div className="space-y-4"> |
| 327 | <div className="flex items-center justify-between"> |
| 328 | <h2 className="text-sm font-medium text-muted-foreground uppercase tracking-wider"> |
| 329 | Connected Repositories |
| 330 | </h2> |
| 331 | {connectedRepos.data && connectedRepos.data.length > 0 && ( |
| 332 | <Badge variant={"secondary"} className="tabular-nums"> |
| 333 | {connectedRepos.data.length} |
| 334 | </Badge> |
| 335 | )} |
| 336 | </div> |
| 337 | |
| 338 | {connectedRepos.isLoading ? ( |
| 339 | <div className="p-6 space-y-3"> |
| 340 | {[...Array(4)].map((_, i) => ( |
| 341 | <Skeleton key={i} className="h-16 w-full rounded-xl" /> |
| 342 | ))} |
| 343 | </div> |
| 344 | ) : connectedRepos.data?.length === 0 ? ( |
| 345 | <Card> |
| 346 | <CardContent className="py-16 text-center"> |
| 347 | <div className="mx-auto h-14 rounded-full bg-muted flex items-center justify-center"> |
| 348 | <FolderGit2 className="size-7 text-muted-foreground" /> |
| 349 | </div> |
| 350 | <p className="mt-4 font-medium"> |
| 351 | No connected repositories found. |
| 352 | </p> |
| 353 | <p className="text-sm text-muted-foreground mt-1 max-w-sm mx-auto"> |
| 354 | Connect your GitHub repositories to start getting AI-powered |
| 355 | code reviews on your pull requests. |
| 356 | </p> |
| 357 | <Button className="mt-6" onClick={() => setShowGitHubRepos(true)}> |
| 358 | <Plus className="size-4" /> |
| 359 | Add your first repository |
| 360 | </Button> |
| 361 | </CardContent> |
| 362 | </Card> |
| 363 | ) : ( |
| 364 | <div className="grid gap-3 sm:grid-cols-2"> |
| 365 | {connectedRepos.data?.map((repo) => ( |
| 366 | <ConnectedRepoCard |
| 367 | key={repo.id} |
| 368 | repo={repo} |
| 369 | onDisconnect={() => disconnectMutation.mutate({ id: repo.id })} |
| 370 | isDisconnecting={disconnectMutation.isPending} |
| 371 | /> |
| 372 | ))} |
| 373 | </div> |
| 374 | )} |
| 375 | </div> |
| 376 | </div> |
| 377 | ); |
| 378 | } |
| 379 | |
| 380 | function ConnectedRepoCard({ |
| 381 | repo, |
| 382 | onDisconnect, |
| 383 | isDisconnecting, |
| 384 | }: { |
| 385 | repo: { |
| 386 | id: string; |
| 387 | fullName: string; |
| 388 | private: boolean; |
| 389 | createdAt: Date; |
| 390 | }; |
| 391 | onDisconnect: () => void; |
| 392 | isDisconnecting: boolean; |
| 393 | }) { |
| 394 | return ( |
| 395 | <Card className="group hover:border-primary/30 transition-all hover:shadow-sm"> |
| 396 | <CardContent className="p-5"> |
| 397 | <div className="flex items-start justify-between gap-3"> |
| 398 | <Link href={`/repos/${repo.id}`} className="flex-1 min-w-0"> |
| 399 | <div className="flex items-start gap-3"> |
| 400 | <div |
| 401 | className={cn( |
| 402 | "size-10 rounded-lg flex items-center justify-center shrink-0 transition-colors", |
| 403 | repo.private |
| 404 | ? "bg-emerald-500/10 group-hover:bg-emerald-500/15" |
| 405 | : "bg-emerald-500/10 group-hover:bg-emerald-500/15", |
| 406 | )} |
| 407 | > |
| 408 | {repo.private ? ( |
| 409 | <Lock className="size-4 text-amber-600 dark:text-amber-400" /> |
| 410 | ) : ( |
| 411 | <Globe className="size-4 text-emerald-600 dark:text-emerald-400" /> |
| 412 | )} |
| 413 | </div> |
| 414 | <div className="min-w-0"> |
| 415 | <span className="font-medium block truncate group-hover:text-primary transition-colors"> |
| 416 | {repo.fullName} |
| 417 | </span> |
| 418 | <div className="flex items-center gap-2 mt-1"> |
| 419 | <Badge |
| 420 | variant={"outline"} |
| 421 | className="text-xs px-1.5 py-0 h-5" |
| 422 | > |
| 423 | {repo.private ? "Private" : "Public"} |
| 424 | </Badge> |
| 425 | </div> |
| 426 | </div> |
| 427 | </div> |
| 428 | </Link> |
| 429 | |
| 430 | <AlertDialog> |
| 431 | <AlertDialogTrigger asChild> |
| 432 | <Button |
| 433 | variant={"ghost"} |
| 434 | size={"icon-sm"} |
| 435 | disabled={isDisconnecting} |
| 436 | className="opacity-0 group-hover:opacity-100 transition-opacity shrink-0 text-muted-foreground hover:text-destructive hover:bg-destructive/10" |
| 437 | > |
| 438 | <Trash2 className="size-4" /> |
| 439 | </Button> |
| 440 | </AlertDialogTrigger> |
| 441 | <AlertDialogContent> |
| 442 | <AlertDialogHeader> |
| 443 | <AlertDialogTitle>Disconnect Repository</AlertDialogTitle> |
| 444 | <AlertDialogDescription> |
| 445 | Are you sure you want to disconnect{" "} |
| 446 | <span className="font-medium text-foreground"> |
| 447 | {repo.fullName} |
| 448 | </span> |
| 449 | ? This will remove all review history for this repository. |
| 450 | </AlertDialogDescription> |
| 451 | </AlertDialogHeader> |
| 452 | <AlertDialogFooter> |
| 453 | <AlertDialogCancel>Cancel</AlertDialogCancel> |
| 454 | <AlertDialogAction |
| 455 | onClick={onDisconnect} |
| 456 | className="bg-destructive text-destructive-foreground hover:bg-destructive/90" |
| 457 | > |
| 458 | Disconnect |
| 459 | </AlertDialogAction> |
| 460 | </AlertDialogFooter> |
| 461 | </AlertDialogContent> |
| 462 | </AlertDialog> |
| 463 | </div> |
| 464 | |
| 465 | <div className="mt-4 pt-4 border-t border-border/60 flex items-center justify-between"> |
| 466 | <span className="text-xs text-muted-foreground"> |
| 467 | Connected {formatDate(repo.createdAt)} |
| 468 | </span> |
| 469 | <Link href={`/repos/${repo.id}`}> |
| 470 | <Button |
| 471 | variant={"ghost"} |
| 472 | size={"sm"} |
| 473 | className="h-7 text-xs gap-1.5 -mr-2" |
| 474 | > |
| 475 | View PRs |
| 476 | <ArrowRight className="size-3" /> |
| 477 | </Button> |
| 478 | </Link> |
| 479 | </div> |
| 480 | </CardContent> |
| 481 | </Card> |
| 482 | ); |
| 483 | } |
| 484 | |
| 485 | function RepoSelectItem({ |
| 486 | repo, |
| 487 | selected, |
| 488 | onToggle, |
| 489 | }: { |
| 490 | repo: GitHubRepo; |
| 491 | selected: boolean; |
| 492 | onToggle: () => void; |
| 493 | }) { |
| 494 | const langColor = repo.language |
| 495 | ? languageColors[repo.language] || "bg-gray-400" |
| 496 | : null; |
| 497 | |
| 498 | return ( |
| 499 | <label |
| 500 | className={cn( |
| 501 | "flex cursor-pointer items-center gap-4 px-6 py-4 transition-colors", |
| 502 | selected ? "bg-primary/5" : "hover:bg-muted/50", |
| 503 | )} |
| 504 | > |
| 505 | <Checkbox |
| 506 | checked={selected} |
| 507 | onCheckedChange={onToggle} |
| 508 | className="shrink-0" |
| 509 | /> |
| 510 | <div className="flex-1 min-w-0"> |
| 511 | <div className="flex items-center gap-2"> |
| 512 | <span className="font-medium truncate">{repo.fullName}</span> |
| 513 | {repo.private && ( |
| 514 | <Lock className="size-3 text-muted-foreground shrink-0" /> |
| 515 | )} |
| 516 | </div> |
| 517 | {repo.description && ( |
| 518 | <p className="text-sm text-muted-foreground truncate mt-0.5"> |
| 519 | {repo.description} |
| 520 | </p> |
| 521 | )} |
| 522 | </div> |
| 523 | <div className="flex items-center gap-4 shrink-0"> |
| 524 | {repo.stars > 0 && ( |
| 525 | <span className="flex items-center gap-1 text-xs text-muted-foreground"> |
| 526 | <Star className="size-3" /> |
| 527 | <span className="tabular-nums">{repo.stars}</span> |
| 528 | </span> |
| 529 | )} |
| 530 | {repo.language && ( |
| 531 | <div className="flex items-center gap-1.5"> |
| 532 | <span className={cn("size-2.5 rounded-full shrink-0", langColor)} /> |
| 533 | <span className="text-xs text-muted-foreground"> |
| 534 | {repo.language} |
| 535 | </span> |
| 536 | </div> |
| 537 | )} |
| 538 | </div> |
| 539 | </label> |
| 540 | ); |
| 541 | } |
| 542 | |
| 543 | function formatDate(date: Date): string { |
| 544 | const now = new Date(); |
| 545 | const diffMs = now.getTime() - new Date(date).getTime(); |
| 546 | const diffDays = Math.floor(diffMs / (1000 * 60 * 60 * 24)); |
| 547 | |
| 548 | if (diffDays === 0) return "today"; |
| 549 | if (diffDays === 1) return "yesterday"; |
| 550 | if (diffDays < 7) return `${diffDays} days ago`; |
| 551 | if (diffDays < 30) return `${Math.floor(diffDays / 7)} weeks ago`; |
| 552 | if (diffDays < 365) return `${Math.floor(diffDays / 30)} months ago`; |
| 553 | |
| 554 | return new Date(date).toLocaleDateString("en-US", { |
| 555 | month: "short", |
| 556 | day: "numeric", |
| 557 | year: "numeric", |
| 558 | }); |
| 559 | } |