| 1 | "use client"; |
| 2 | |
| 3 | import { useDebouncedValue } from |
| 4 | import { keepPreviousData, useQuery |
| 5 | import Link from "next/link |
| 6 | import { useState } |
| 7 | import { |
| 8 | BookLock, |
| 9 | BookMarked, |
| 10 | GitBranch, |
| 11 | Plus, |
| 12 | Search, |
| 13 | Users, |
| 14 | } from "lucide-react"; |
| 15 | |
| 16 | import { AppHeader } |
| 17 | import { CreateOrganizationDialog } |
| 18 | import { Invitations } |
| 19 | import { NewRepositoryDialog } |
| 20 | import type { Repository |
| 21 | import { FromNowHoverCard } |
| 22 | import { useAuthenticate } |
| 23 | import { Button } |
| 24 | import { Card, CardContent |
| 25 | import { |
| 26 | InputGroup, |
| 27 | InputGroupAddon, |
| 28 | InputGroupInput, |
| 29 | } from "@/components/ui/input-group"; |
| 30 | import { |
| 31 | Select, |
| 32 | SelectContent, |
| 33 | SelectItem, |
| 34 | SelectTrigger, |
| 35 | SelectValue, |
| 36 | } from "@/components/ui/select"; |
| 37 | import { Separator } |
| 38 | import { apiClient, apiErrorMessage |
| 39 | import { authClient } |
| 40 | import { cn } |
| 41 | |
| 42 | const ALL = " |
| 43 | |
| 44 | export default function DashboardPage |
| 45 | const { data |
| 46 | const username = |
| 47 | const owners = |
| 48 | const [q, |
| 49 | const [creatingOrganization, |
| 50 | // One request once typing pauses, not one per keystroke. |
| 51 | const [search] |
| 52 | // Whose repositories the sidebar shows: everything, or one account's, the way GitHub's context switcher narrows the dashboard. |
| 53 | const [context, |
| 54 | |
| 55 | const { data: |
| 56 | queryKey: ["my-organizations", |
| 57 | enabled: Boolean(username |
| 58 | queryFn: async () |
| 59 | const { data, |
| 60 | if (error) throw |
| 61 | return data.organizations |
| 62 | }, |
| 63 | }); |
| 64 | |
| 65 | // Owned and shared alike, most recently pushed first. |
| 66 | const { data: |
| 67 | queryKey: ["viewer-repositories", |
| 68 | enabled: Boolean(username |
| 69 | placeholderData: keepPreviousData, |
| 70 | queryFn: async () |
| 71 | const scoped = |
| 72 | .filter(Boolean) |
| 73 | .join(" ") |
| 74 | const { data, |
| 75 | params: { query: { |
| 76 | }); |
| 77 | if (error) throw |
| 78 | return data.repositories |
| 79 | name: repository.name |
| 80 | slug: repository.slug |
| 81 | owner: repository.owner |
| 82 | description: repository.description |
| 83 | visibility: repository.visibility |
| 84 | updatedAt: repository.lastPushedAt |
| 85 | })); |
| 86 | }, |
| 87 | }); |
| 88 | |
| 89 | return ( |
| 90 | <div className="flex min-h-full flex-col |
| 91 | <AppHeader username={ |
| 92 | |
| 93 | <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] |
| 94 | <aside className="flex flex-col gap-4 |
| 95 | <div className="flex items-center justify-between |
| 96 | <h2 className="text-sm font-semibold |
| 97 | <NewRepositoryDialog owners={ |
| 98 | <Button size=" |
| 99 | <Plus data-icon=" |
| 100 | New |
| 101 | </Button> |
| 102 | </NewRepositoryDialog> |
| 103 | </div> |
| 104 | |
| 105 | {organizations.length > |
| 106 | <Select value={ |
| 107 | <SelectTrigger aria-label=" |
| 108 | <SelectValue /> |
| 109 | </SelectTrigger> |
| 110 | <SelectContent> |
| 111 | <SelectItem value={ |
| 112 | <SelectItem value={ |
| 113 | {organizations.map(( |
| 114 | <SelectItem key={ |
| 115 | {organization.name} |
| 116 | </SelectItem> |
| 117 | ))} |
| 118 | </SelectContent> |
| 119 | </Select> |
| 120 | )} |
| 121 | |
| 122 | <InputGroup> |
| 123 | <InputGroupAddon> |
| 124 | <Search /> |
| 125 | </InputGroupAddon> |
| 126 | <InputGroupInput |
| 127 | type="search" |
| 128 | placeholder="Find a repository" |
| 129 | aria-label="Find a repository" |
| 130 | value={q} |
| 131 | onChange={(event) |
| 132 | /> |
| 133 | </InputGroup> |
| 134 | |
| 135 | {repositories.length > |
| 136 | <ul className="-mx-2 flex flex-col |
| 137 | {repositories.map(( |
| 138 | <li key={` |
| 139 | <RepositoryRow repository={ |
| 140 | </li> |
| 141 | ))} |
| 142 | </ul> |
| 143 | ) : ( |
| 144 | <p className="text-sm text-muted-foreground |
| 145 | {isPending |
| 146 | ? "Loading repositories…" |
| 147 | : search |
| 148 | ? "No repositories match." |
| 149 | : "You don't have any repositories yet."} |
| 150 | </p> |
| 151 | )} |
| 152 | |
| 153 | <Separator /> |
| 154 | |
| 155 | <Link |
| 156 | href={`/${username |
| 157 | className="text-sm text-primary hover:underline" |
| 158 | > |
| 159 | View your profile |
| 160 | </Link> |
| 161 | </aside> |
| 162 | |
| 163 | <section className="flex flex-col gap-6 |
| 164 | <div className="flex flex-col gap-1 |
| 165 | <h1 className="text-2xl font-semibold tracking-tight |
| 166 | {username ? `Welcome back, |
| 167 | </h1> |
| 168 | <p className="text-muted-foreground |
| 169 | Push your first commit or start a new project. |
| 170 | </p> |
| 171 | </div> |
| 172 | |
| 173 | <Invitations /> |
| 174 | |
| 175 | <div className="grid gap-4 sm:grid-cols-3 |
| 176 | <NewRepositoryDialog owners={ |
| 177 | <StartCard |
| 178 | icon={<BookMarked |
| 179 | title="Create a repository" |
| 180 | description="Host code, track changes, and collaborate." |
| 181 | /> |
| 182 | </NewRepositoryDialog> |
| 183 | <StartCard |
| 184 | icon={<GitBranch |
| 185 | title="Import a repository" |
| 186 | description="Bring an existing project over with its history." |
| 187 | /> |
| 188 | <StartCard |
| 189 | icon={<Users |
| 190 | title="Start an organization" |
| 191 | description="Share repositories across a team." |
| 192 | onClick={() => |
| 193 | /> |
| 194 | <CreateOrganizationDialog |
| 195 | open={creatingOrganization} |
| 196 | onOpenChange={setCreatingOrganization} |
| 197 | /> |
| 198 | </div> |
| 199 | |
| 200 | <Card> |
| 201 | <CardHeader> |
| 202 | <CardTitle className=" |
| 203 | </CardHeader> |
| 204 | <CardContent> |
| 205 | <p className="text-sm text-muted-foreground |
| 206 | Activity from repositories you own or watch will show up here. |
| 207 | </p> |
| 208 | </CardContent> |
| 209 | </Card> |
| 210 | </section> |
| 211 | </main> |
| 212 | </div> |
| 213 | ); |
| 214 | } |
| 215 | |
| 216 | /** Clickable when it opens something: as a dialog trigger, the dialog passes its click handler in through `props`. */ |
| 217 | function StartCard({ |
| 218 | icon, |
| 219 | title, |
| 220 | description, |
| 221 | ...props |
| 222 | }: { |
| 223 | icon: React |
| 224 | title: string |
| 225 | description: string |
| 226 | } & React |
| 227 | const interactive = |
| 228 | return ( |
| 229 | <Card |
| 230 | {...props} |
| 231 | role={interactive ? |
| 232 | tabIndex={interactive ? |
| 233 | onKeyDown={(event) |
| 234 | if (interactive && (event |
| 235 | event.preventDefault(); |
| 236 | event.currentTarget.click |
| 237 | } |
| 238 | }} |
| 239 | className={cn( |
| 240 | "gap-3", |
| 241 | interactive && "cursor-pointer transition-colors hover:bg-muted/50" |
| 242 | )} |
| 243 | > |
| 244 | <CardHeader> |
| 245 | <span className="text-muted-foreground |
| 246 | <CardTitle className=" |
| 247 | </CardHeader> |
| 248 | <CardContent> |
| 249 | <p className="text-sm text-muted-foreground |
| 250 | </CardContent> |
| 251 | </Card> |
| 252 | ); |
| 253 | } |
| 254 | |
| 255 | /** One line per repository: the sidebar is too narrow for a card. */ |
| 256 | function RepositoryRow({ repository |
| 257 | const fullName = |
| 258 | const Icon = |
| 259 | |
| 260 | return ( |
| 261 | <Link |
| 262 | href={`/${repository |
| 263 | title={fullName} |
| 264 | className="flex items-center gap-2 rounded-md px-2 py-1.5 text-sm hover:bg-muted" |
| 265 | > |
| 266 | <Icon |
| 267 | className="size-4 shrink-0 text-muted-foreground" |
| 268 | aria-label={repository.visibility |
| 269 | /> |
| 270 | <span className="min-w-0 flex-1 truncate |
| 271 | <span className="text-muted-foreground |
| 272 | <span className="font-medium |
| 273 | </span> |
| 274 | <FromNowHoverCard |
| 275 | date={repository.updatedAt |
| 276 | className="shrink-0 text-xs text-muted-foreground" |
| 277 | /> |
| 278 | </Link> |
| 279 | ); |
| 280 | } |