| 1 | "use client"; |
| 2 | |
| 3 | import { useDebouncedValue } from |
| 4 | import { keepPreviousData, useQuery |
| 5 | import Link from "next/link |
| 6 | import { useState } |
| 7 | import { BookMarked, GitBranch |
| 8 | |
| 9 | import { AppHeader } |
| 10 | import { CreateOrganizationDialog } |
| 11 | import { Invitations } |
| 12 | import { NewRepositoryDialog } |
| 13 | import type { Repository |
| 14 | import { useAuthenticate } |
| 15 | import { Button } |
| 16 | import { |
| 17 | InputGroup, |
| 18 | InputGroupAddon, |
| 19 | InputGroupInput, |
| 20 | } from "@/components/ui/input-group"; |
| 21 | import { |
| 22 | Select, |
| 23 | SelectContent, |
| 24 | SelectItem, |
| 25 | SelectTrigger, |
| 26 | SelectValue, |
| 27 | } from "@/components/ui/select"; |
| 28 | import { Separator } |
| 29 | import { apiClient, unwrap |
| 30 | import { authClient } |
| 31 | import { StartCard } |
| 32 | import { RepositoryRow, RepositoryRowSkeleton |
| 33 | |
| 34 | const ALL = " |
| 35 | |
| 36 | export default function DashboardPage |
| 37 | const { data |
| 38 | const username = |
| 39 | const owners = |
| 40 | const [q, |
| 41 | const [creatingOrganization, |
| 42 | // One request once typing pauses, not one per keystroke. |
| 43 | const [search] |
| 44 | // Whose repositories the sidebar shows: everything, or one account's, the way GitHub's context switcher narrows the dashboard. |
| 45 | const [context, |
| 46 | |
| 47 | const { data: |
| 48 | queryKey: ["my-organizations", |
| 49 | enabled: Boolean(username |
| 50 | queryFn: async () |
| 51 | const data = |
| 52 | return data.organizations |
| 53 | }, |
| 54 | }); |
| 55 | |
| 56 | // Owned and shared alike, most recently pushed first. |
| 57 | const { data: |
| 58 | queryKey: ["viewer-repositories", |
| 59 | enabled: Boolean(username |
| 60 | placeholderData: keepPreviousData, |
| 61 | queryFn: async () |
| 62 | const scoped = |
| 63 | .filter(Boolean) |
| 64 | .join(" ") |
| 65 | const data = |
| 66 | apiClient.GET("/api/repositories |
| 67 | params: { query: { |
| 68 | }), |
| 69 | ); |
| 70 | return data.repositories |
| 71 | }, |
| 72 | }); |
| 73 | |
| 74 | return ( |
| 75 | <div className="flex min-h-full flex-col |
| 76 | <AppHeader username={ |
| 77 | |
| 78 | <main className="mx-auto grid w-full max-w-6xl flex-1 gap-8 px-4 py-8 md:px-6 lg:grid-cols-[300px_1fr] |
| 79 | <aside className="flex flex-col gap-4 |
| 80 | <div className="flex items-center justify-between |
| 81 | <h2 className="text-sm font-semibold |
| 82 | <NewRepositoryDialog owners={ |
| 83 | <Button size=" |
| 84 | <Plus data-icon=" |
| 85 | New |
| 86 | </Button> |
| 87 | </NewRepositoryDialog> |
| 88 | </div> |
| 89 | |
| 90 | {organizations.length > |
| 91 | <Select value={ |
| 92 | <SelectTrigger aria-label=" |
| 93 | <SelectValue /> |
| 94 | </SelectTrigger> |
| 95 | <SelectContent> |
| 96 | <SelectItem value={ |
| 97 | <SelectItem value={ |
| 98 | {organizations.map(( |
| 99 | <SelectItem key={ |
| 100 | {organization.name} |
| 101 | </SelectItem> |
| 102 | ))} |
| 103 | </SelectContent> |
| 104 | </Select> |
| 105 | )} |
| 106 | |
| 107 | <InputGroup> |
| 108 | <InputGroupAddon> |
| 109 | <Search /> |
| 110 | </InputGroupAddon> |
| 111 | <InputGroupInput |
| 112 | type="search" |
| 113 | placeholder="Find a repository" |
| 114 | aria-label="Find a repository" |
| 115 | value={q} |
| 116 | onChange={(event) |
| 117 | /> |
| 118 | </InputGroup> |
| 119 | |
| 120 | {repositories.length > |
| 121 | <ul className="-mx-2 flex flex-col |
| 122 | {repositories.map(( |
| 123 | <li key={` |
| 124 | <RepositoryRow repository={ |
| 125 | </li> |
| 126 | ))} |
| 127 | </ul> |
| 128 | ) : isPending ? |
| 129 | <ul className="-mx-2 flex flex-col |
| 130 | {[0, 1, 2 |
| 131 | <li key={row |
| 132 | <RepositoryRowSkeleton /> |
| 133 | </li> |
| 134 | ))} |
| 135 | </ul> |
| 136 | ) : ( |
| 137 | <p className="text-sm text-muted-foreground |
| 138 | {search |
| 139 | ? "No repositories match." |
| 140 | : "You don't have any repositories yet."} |
| 141 | </p> |
| 142 | )} |
| 143 | |
| 144 | <Separator /> |
| 145 | |
| 146 | <Link |
| 147 | href={`/${username |
| 148 | className="text-sm text-primary hover:underline" |
| 149 | > |
| 150 | View your profile |
| 151 | </Link> |
| 152 | </aside> |
| 153 | |
| 154 | <section className="flex flex-col gap-6 |
| 155 | <div className="flex flex-col gap-1 |
| 156 | <h1 className="text-2xl font-semibold tracking-tight |
| 157 | {username ? `Welcome back, |
| 158 | </h1> |
| 159 | <p className="text-muted-foreground |
| 160 | Push your first commit or start a new project. |
| 161 | </p> |
| 162 | </div> |
| 163 | |
| 164 | <Invitations /> |
| 165 | |
| 166 | <div className="grid gap-4 sm:grid-cols-3 |
| 167 | <NewRepositoryDialog owners={ |
| 168 | <StartCard |
| 169 | icon={<BookMarked |
| 170 | title="Create a repository" |
| 171 | description="Host code, track changes, and collaborate." |
| 172 | /> |
| 173 | </NewRepositoryDialog> |
| 174 | <NewRepositoryDialog owners={ |
| 175 | <StartCard |
| 176 | icon={<GitBranch |
| 177 | title="Import a repository" |
| 178 | description="Bring an existing project over with its history." |
| 179 | /> |
| 180 | </NewRepositoryDialog> |
| 181 | <StartCard |
| 182 | icon={<Users |
| 183 | title="Start an organization" |
| 184 | description="Share repositories across a team." |
| 185 | onClick={() => |
| 186 | /> |
| 187 | <CreateOrganizationDialog |
| 188 | open={creatingOrganization} |
| 189 | onOpenChange={setCreatingOrganization} |
| 190 | /> |
| 191 | </div> |
| 192 | </section> |
| 193 | </main> |
| 194 | </div> |
| 195 | ); |
| 196 | } |