| 1 | import { Globe, Mail, MapPin } from "lucide-react"; |
| 2 | import type { Metadata |
| 3 | import { notFound } |
| 4 | import { Suspense } |
| 5 | |
| 6 | import { AppHeader } |
| 7 | import { CursorPagination } |
| 8 | import { Avatar, AvatarFallback |
| 9 | import { MembershipActions } |
| 10 | import { Badge } |
| 11 | import { buttonVariants } |
| 12 | import { Separator } |
| 13 | import { |
| 14 | RepositoryReadme, |
| 15 | RepositoryReadmeSkeleton, |
| 16 | } from "@/components/repositories/repository-readme"; |
| 17 | import { |
| 18 | ContributionGraph, |
| 19 | ContributionGraphSkeleton, |
| 20 | } from "@/components/profile/contribution-graph"; |
| 21 | import { |
| 22 | createServerClient, |
| 23 | getAdminOrganizations, |
| 24 | getServerSession, |
| 25 | } from "@/lib/api/server"; |
| 26 | import { plural } |
| 27 | import { cn } |
| 28 | |
| 29 | import { ProfileTabs } |
| 30 | import { REPOSITORIES_PAGE_SIZE } |
| 31 | import Link from "next/link |
| 32 | |
| 33 | export async function generateMetadata |
| 34 | params, |
| 35 | }: PageProps< |
| 36 | const { username |
| 37 | |
| 38 | const client = |
| 39 | const [{ data |
| 40 | client.GET("/api/users/{username} |
| 41 | client.GET("/api/organizations/{slug} |
| 42 | params: { path: { |
| 43 | }), |
| 44 | ]); |
| 45 | if (organization.data |
| 46 | const title = |
| 47 | const description = |
| 48 | organization.data.repositoryCount |
| 49 | "public repository", |
| 50 | "public repositories", |
| 51 | ); |
| 52 | return { |
| 53 | title, |
| 54 | description, |
| 55 | openGraph: { title, |
| 56 | }; |
| 57 | } |
| 58 | if (!data) |
| 59 | |
| 60 | const title = |
| 61 | const description = |
| 62 | |
| 63 | return { |
| 64 | title, |
| 65 | description, |
| 66 | openGraph: { title, |
| 67 | }; |
| 68 | } |
| 69 | |
| 70 | export default async function |
| 71 | params, |
| 72 | searchParams, |
| 73 | }: PageProps< |
| 74 | const { username |
| 75 | const { tab, |
| 76 | const query = |
| 77 | const pageCursor = |
| 78 | |
| 79 | const [session, |
| 80 | getServerSession(), |
| 81 | createServerClient(), |
| 82 | ]); |
| 83 | const viewer = |
| 84 | |
| 85 | const [profile, |
| 86 | client.GET("/api/users/{username} |
| 87 | client.GET("/api/organizations/{slug} |
| 88 | params: { path: { |
| 89 | }), |
| 90 | client.GET("/api/repositories/{username} |
| 91 | params: { |
| 92 | path: { username |
| 93 | query: { |
| 94 | limit: REPOSITORIES_PAGE_SIZE, |
| 95 | q: query || |
| 96 | cursor: pageCursor, |
| 97 | }, |
| 98 | }, |
| 99 | }), |
| 100 | ]); |
| 101 | |
| 102 | if (response.status |
| 103 | notFound(); |
| 104 | } |
| 105 | |
| 106 | if (error || |
| 107 | throw new Error( |
| 108 | } |
| 109 | |
| 110 | const org = |
| 111 | // May create repositories here: the user themselves, or an admin of the organization. |
| 112 | const isViewer = |
| 113 | ? (await getAdminOrganizations |
| 114 | : viewer === username |
| 115 | |
| 116 | return ( |
| 117 | <div className="flex min-h-full flex-col |
| 118 | <AppHeader username={ |
| 119 | |
| 120 | <main className="mx-auto grid w-full max-w-6xl flex-1 gap-8 px-4 py-8 md:px-6 md:grid-cols-[280px_1fr] |
| 121 | <aside className="flex flex-col gap-4 |
| 122 | <Avatar |
| 123 | className={cn( |
| 124 | "size-40 md:size-64", |
| 125 | org ? "rounded-2xl" |
| 126 | )} |
| 127 | > |
| 128 | <AvatarImage |
| 129 | alt={username} |
| 130 | src={(org |
| 131 | /> |
| 132 | <AvatarFallback className=" |
| 133 | {username.slice(0 |
| 134 | </AvatarFallback> |
| 135 | </Avatar> |
| 136 | |
| 137 | <div className="flex flex-col gap-1 |
| 138 | <h1 className="text-2xl font-semibold tracking-tight |
| 139 | {org?.name ?? |
| 140 | </h1> |
| 141 | <p className="flex items-center gap-2 text-lg text-muted-foreground |
| 142 | {username} |
| 143 | {org && <Badge |
| 144 | </p> |
| 145 | {org?.description && |
| 146 | <p className="pt-1 text-sm text-balance |
| 147 | )} |
| 148 | </div> |
| 149 | |
| 150 | {org && (org |
| 151 | <ul className="flex flex-col gap-1.5 text-sm text-muted-foreground |
| 152 | {org.location && |
| 153 | <li className="flex items-center gap-2 |
| 154 | <MapPin className=" |
| 155 | {org.location} |
| 156 | </li> |
| 157 | )} |
| 158 | {org.website && |
| 159 | <li className="flex min-w-0 items-center gap-2 |
| 160 | <Globe className=" |
| 161 | <a |
| 162 | href={org.website |
| 163 | rel="nofollow noopener noreferrer" |
| 164 | target="_blank" |
| 165 | className="truncate hover:underline" |
| 166 | > |
| 167 | {org.website.replace |
| 168 | </a> |
| 169 | </li> |
| 170 | )} |
| 171 | {org.email && |
| 172 | <li className="flex min-w-0 items-center gap-2 |
| 173 | <Mail className=" |
| 174 | <a |
| 175 | href={`mailto:${org |
| 176 | className="truncate hover:underline" |
| 177 | > |
| 178 | {org.email} |
| 179 | </a> |
| 180 | </li> |
| 181 | )} |
| 182 | </ul> |
| 183 | )} |
| 184 | |
| 185 | {org?.viewerRole && |
| 186 | <MembershipActions |
| 187 | slug={username} |
| 188 | isPublic={ |
| 189 | org.members.find |
| 190 | ?.public ?? false |
| 191 | } |
| 192 | /> |
| 193 | )} |
| 194 | {org && isViewer |
| 195 | <Link |
| 196 | href={`/${username |
| 197 | className={buttonVariants({ |
| 198 | className: "w-full", |
| 199 | variant: "outline", |
| 200 | })} |
| 201 | > |
| 202 | Settings |
| 203 | </Link> |
| 204 | )} |
| 205 | {!org && ( |
| 206 | <Link |
| 207 | href={`/settings`} |
| 208 | className={buttonVariants({ |
| 209 | className: "w-full", |
| 210 | variant: "outline", |
| 211 | })} |
| 212 | > |
| 213 | {isViewer ? "Edit profile |
| 214 | </Link> |
| 215 | )} |
| 216 | <Separator /> |
| 217 | |
| 218 | <p className="text-sm text-muted-foreground |
| 219 | {org |
| 220 | ? [ |
| 221 | // Membership is private: only members are told how many there are. |
| 222 | org.viewerRole && |
| 223 | plural( |
| 224 | org.repositoryCount, |
| 225 | "public repository", |
| 226 | "public repositories", |
| 227 | ), |
| 228 | ] |
| 229 | .filter(Boolean) |
| 230 | .join(" · ") |
| 231 | : profile.data |
| 232 | ? `${profile.data |
| 233 | : "No bio yet."} |
| 234 | </p> |
| 235 | </aside> |
| 236 | |
| 237 | <section className="flex flex-col gap-6 |
| 238 | <ProfileTabs |
| 239 | username={username} |
| 240 | isViewer={isViewer} |
| 241 | owners={viewer ? |
| 242 | extraTabs={ |
| 243 | org |
| 244 | ? [ |
| 245 | { |
| 246 | value: "people", |
| 247 | label: "People", |
| 248 | content: ( |
| 249 | <PeopleList |
| 250 | people={org.members |
| 251 | href: `/${member |
| 252 | title: member.name |
| 253 | subtitle: member.username |
| 254 | image: member.image |
| 255 | }))} |
| 256 | empty={ |
| 257 | org.viewerRole |
| 258 | ? `${username} has no members yet. |
| 259 | : `Nobody in ${username} |
| 260 | } |
| 261 | /> |
| 262 | ), |
| 263 | }, |
| 264 | // Teams are for members only. |
| 265 | ...(org.viewerRole |
| 266 | ? [ |
| 267 | { |
| 268 | value: "teams", |
| 269 | label: "Teams", |
| 270 | content: ( |
| 271 | <Suspense fallback={ |
| 272 | <OrganizationTeamList slug={ |
| 273 | </Suspense> |
| 274 | ), |
| 275 | }, |
| 276 | ] |
| 277 | : []), |
| 278 | ] |
| 279 | : [ |
| 280 | { |
| 281 | value: "organizations", |
| 282 | label: "Organizations", |
| 283 | content: ( |
| 284 | <Suspense fallback={ |
| 285 | <UserOrganizations |
| 286 | username={username} |
| 287 | isViewer={isViewer} |
| 288 | /> |
| 289 | </Suspense> |
| 290 | ), |
| 291 | }, |
| 292 | ] |
| 293 | } |
| 294 | defaultTab={tab === |
| 295 | repositories={data.repositories |
| 296 | query={query} |
| 297 | pagination={ |
| 298 | <CursorPagination |
| 299 | pathname={`/${username |
| 300 | params={{ tab: |
| 301 | cursor={pageCursor} |
| 302 | nextCursor={data.nextCursor |
| 303 | /> |
| 304 | } |
| 305 | overview={ |
| 306 | <> |
| 307 | {org && org |
| 308 | <PinnedRepositories owner={ |
| 309 | )} |
| 310 | <Suspense fallback={ |
| 311 | {org ? ( |
| 312 | // ORG's readme at .ghost/profile/README.md |
| 313 | <ProfileReadme |
| 314 | username={username} |
| 315 | slug=".ghost" |
| 316 | path="profile" |
| 317 | /> |
| 318 | ) : ( |
| 319 | <ProfileReadme username={ |
| 320 | )} |
| 321 | </Suspense> |
| 322 | {!org && ( |
| 323 | <Suspense fallback={ |
| 324 | <ProfileContributions username={ |
| 325 | </Suspense> |
| 326 | )} |
| 327 | </> |
| 328 | } |
| 329 | /> |
| 330 | </section> |
| 331 | </main> |
| 332 | </div> |
| 333 | ); |
| 334 | } |
| 335 | |
| 336 | /** The repository named after its owner is that person's bio, the way GitHub treats it. There is no separate bio to store: the README of `user/user` is it, and the API already refuses to serve one the viewer may not read. */ |
| 337 | async function ProfileReadme( |
| 338 | username, |
| 339 | slug, |
| 340 | path, |
| 341 | }: { |
| 342 | username: string |
| 343 | slug: string |
| 344 | path?: string |
| 345 | }) { |
| 346 | const client = |
| 347 | const { data |
| 348 | "/api/repositories/{username}/{slug}/readme", |
| 349 | { params: { path: |
| 350 | ); |
| 351 | |
| 352 | if (!data?.content |
| 353 | return ( |
| 354 | <p className="text-sm text-muted-foreground |
| 355 | {username} hasn' |
| 356 | </p> |
| 357 | ); |
| 358 | } |
| 359 | |
| 360 | return <RepositoryReadme readme |
| 361 | } |
| 362 | |
| 363 | async function ProfileContributions |
| 364 | const client = |
| 365 | const { data |
| 366 | params: { path: { |
| 367 | }); |
| 368 | |
| 369 | if (!data) |
| 370 | |
| 371 | return <ContributionGraph data |
| 372 | } |
| 373 | |
| 374 | /** Membership is private, so another user's list holds only the organizations the viewer shares with them. */ |
| 375 | async function UserOrganizations( |
| 376 | username, |
| 377 | isViewer, |
| 378 | }: { |
| 379 | username: string |
| 380 | isViewer: boolean |
| 381 | }) { |
| 382 | const client = |
| 383 | const { data |
| 384 | params: { path: { |
| 385 | }); |
| 386 | |
| 387 | return ( |
| 388 | <PeopleList |
| 389 | people={(data?. |
| 390 | href: `/${org |
| 391 | title: org.name |
| 392 | subtitle: org.slug |
| 393 | image: org.logo |
| 394 | }))} |
| 395 | empty={ |
| 396 | isViewer |
| 397 | ? "You aren't a member of any organizations." |
| 398 | : `You share no organizations with ${username} |
| 399 | } |
| 400 | square |
| 401 | /> |
| 402 | ); |
| 403 | } |
| 404 | |
| 405 | /** Accounts linked from a profile: an organization's members, or the organizations a user is in. */ |
| 406 | function PeopleList({ |
| 407 | people, |
| 408 | empty, |
| 409 | square = false, |
| 410 | }: { |
| 411 | people: { |
| 412 | href: string |
| 413 | title: string |
| 414 | subtitle: string |
| 415 | image: string |
| 416 | }[]; |
| 417 | empty: string |
| 418 | square?: boolean |
| 419 | }) { |
| 420 | if (people.length |
| 421 | return <p className |
| 422 | } |
| 423 | |
| 424 | return ( |
| 425 | <ul className="grid gap-2 sm:grid-cols-2 |
| 426 | {people.map(( |
| 427 | <li key={person |
| 428 | <Link |
| 429 | href={person.href |
| 430 | className="flex items-center gap-3 rounded-lg border p-3 hover:bg-muted/50" |
| 431 | > |
| 432 | <Avatar className={ |
| 433 | <AvatarImage src={ |
| 434 | <AvatarFallback> |
| 435 | {person.subtitle.slice |
| 436 | </AvatarFallback> |
| 437 | </Avatar> |
| 438 | <span className="flex min-w-0 flex-col |
| 439 | <span className="truncate text-sm font-medium |
| 440 | {person.title} |
| 441 | </span> |
| 442 | <span className="truncate text-xs text-muted-foreground |
| 443 | {person.subtitle} |
| 444 | </span> |
| 445 | </span> |
| 446 | </Link> |
| 447 | </li> |
| 448 | ))} |
| 449 | </ul> |
| 450 | ); |
| 451 | } |
| 452 | |
| 453 | /** Up to six repositories the organization chose to show first. */ |
| 454 | function PinnedRepositories({ |
| 455 | owner, |
| 456 | pinned, |
| 457 | }: { |
| 458 | owner: string |
| 459 | pinned: { |
| 460 | slug: string |
| 461 | name: string |
| 462 | description: string |
| 463 | visibility: "public |
| 464 | }[]; |
| 465 | }) { |
| 466 | return ( |
| 467 | <section className="flex flex-col gap-3 |
| 468 | <h2 className="text-sm font-semibold |
| 469 | <ul className="grid gap-3 sm:grid-cols-2 |
| 470 | {pinned.map(( |
| 471 | <li |
| 472 | key={repository.slug |
| 473 | className="flex flex-col gap-1 rounded-lg border p-4" |
| 474 | > |
| 475 | <div className="flex items-center justify-between gap-2 |
| 476 | <Link |
| 477 | href={`/${owner |
| 478 | className="truncate text-sm font-semibold text-primary hover:underline" |
| 479 | > |
| 480 | {repository.name} |
| 481 | </Link> |
| 482 | <Badge variant=" |
| 483 | {repository.visibility} |
| 484 | </Badge> |
| 485 | </div> |
| 486 | {repository.description && |
| 487 | <p className="line-clamp-2 text-xs text-muted-foreground |
| 488 | {repository.description} |
| 489 | </p> |
| 490 | )} |
| 491 | </li> |
| 492 | ))} |
| 493 | </ul> |
| 494 | </section> |
| 495 | ); |
| 496 | } |
| 497 | |
| 498 | async function OrganizationTeamList |
| 499 | const client = |
| 500 | const { data |
| 501 | params: { path: { |
| 502 | }); |
| 503 | const teams = |
| 504 | |
| 505 | if (teams.length |
| 506 | return <p className |
| 507 | } |
| 508 | return ( |
| 509 | <ul className="grid gap-2 sm:grid-cols-2 |
| 510 | {teams.map(( |
| 511 | <li key={team |
| 512 | <Link |
| 513 | href={`/${slug |
| 514 | className="flex flex-col rounded-lg border p-3 hover:bg-muted/50" |
| 515 | > |
| 516 | <span className="text-sm font-medium |
| 517 | <span className="text-xs text-muted-foreground |
| 518 | {plural(team.memberCount |
| 519 | {plural(team.repositoryCount |
| 520 | </span> |
| 521 | </Link> |
| 522 | </li> |
| 523 | ))} |
| 524 | </ul> |
| 525 | ); |
| 526 | } |