| 1 | import { waitUntil } from "cloudflare:workers"; |
| 2 | import { Box, GitFork, Link2, Lock } from "lucide-react"; |
| 3 | import type { ReactNode } from "react"; |
| 4 | import { Link, Outlet, type ShouldRevalidateFunctionArgs, data, useLocation, useRouteLoaderData } from "react-router"; |
| 5 | |
| 6 | import type { Project } from "@g1t/contracts"; |
| 7 | |
| 8 | import type { Route } from "./+types/layout"; |
| 9 | import { AboutEditor, LinkList } from "../../components/project-about"; |
| 10 | import { Topics } from "../../components/topics"; |
| 11 | import { projectHomepage } from "../../lib/about"; |
| 12 | import { page } from "../../lib/meta"; |
| 13 | import { type Tab as PageTab, tabsFor } from "../../lib/project-nav"; |
| 14 | import { Badge } from "../../components/ui/badge"; |
| 15 | import { Hint } from "../../components/ui/hint"; |
| 16 | import { NavTab, NavTabs } from "../../components/ui/nav-tabs"; |
| 17 | import { ProjectStrip } from "../../components/mobile"; |
| 18 | import { seesSettings } from "../../lib/access"; |
| 19 | import { WatchMenu } from "../../components/notifications"; |
| 20 | import { PinButton } from "../../components/pin-button"; |
| 21 | import { StarButton } from "../../components/star-button"; |
| 22 | import { ForkButton, type Forking } from "../../components/fork-button"; |
| 23 | import { ArchivedBanner } from "../../components/repo-lifecycle"; |
| 24 | import { WelcomeBanner } from "../../components/welcome"; |
| 25 | import { clearWelcome, welcomes } from "../../lib/invites"; |
| 26 | import { notFound } from "../../lib/not-found.server"; |
| 27 | import { redirectIfRenamed, redirectIfTransferred } from "../../lib/renamed.server"; |
| 28 | import { accessFor, countsFor, projectFor, repoFor } from "../../lib/access.server"; |
| 29 | import { inbox, projects, repos, work } from "../../lib/services.server"; |
| 30 | import { getViewer, roleIn, unwrap } from "../../lib/session.server"; |
| 31 | import { isFailed, orElse, part } from "../../lib/partial"; |
| 32 | |
| 33 | /** How many forks the header counts before it says "100+". */ |
| 34 | const FORKS_COUNTED = 100; |
| 35 | |
| 36 | export function meta({ loaderData: loaded, params, ...args }: Route.MetaArgs) { |
| 37 | return page(args, { title: `${loaded?.project?.name ?? params.repo} · ${params.owner} · g1t` }); |
| 38 | } |
| 39 | |
| 40 | export async function loader({ params, context, request }: Route.LoaderArgs) { |
| 41 | const viewer = getViewer(context); |
| 42 | const path = { namespace: params.owner, name: params.repo }; |
| 43 | // Members pin the workspace's projects, and what they open is their Recent. |
| 44 | const member = viewer ? roleIn(viewer, params.owner) != null : false; |
| 45 | const [repo, counts, found, watching, shortcuts, stars, forks, workItems] = await Promise.all([ |
| 46 | repoFor(context, params), |
| 47 | // The repository is what the frame is about; its open counts are only |
| 48 | // the tabs' badges, which do without them. |
| 49 | part(countsFor(context, params), "Open counts"), |
| 50 | projectFor(context, params), |
| 51 | // How the person watches it, for the header's Watch menu, as soon as |
| 52 | // the repository is known. |
| 53 | viewer ? repoFor(context, params).then((found) => (found.ok ? inbox.watching(viewer.username, found.value.id).catch(() => null) : null)) : null, |
| 54 | // Whether they pinned it, for the header's Pin button. |
| 55 | member ? projects.shortcuts(params.owner, viewer).catch(() => null) : null, |
| 56 | // How many starred it and whether they did, for the header's Star button. |
| 57 | repos |
| 58 | .stars(path, viewer) |
| 59 | .then((found) => (found.ok ? found.value : null)) |
| 60 | .catch(() => null), |
| 61 | // Its forks the viewer can see, counted up to 100, for the header's Fork button. |
| 62 | repos |
| 63 | .forks(path, viewer, FORKS_COUNTED) |
| 64 | .then((found) => (found.ok ? found.value.length : null)) |
| 65 | .catch(() => null), |
| 66 | // Whether work items can be filed from it (off by default in a fork): Work |
| 67 | // leaves its menus when they cannot. On when it could not be read. |
| 68 | work |
| 69 | .getSettings(path, viewer) |
| 70 | .then((found) => (found.ok ? (found.value.workItemsEnabled ?? true) : true)) |
| 71 | .catch(() => true), |
| 72 | ]); |
| 73 | if (!repo.ok && !found.ok) { |
| 74 | // Under a workspace's old name, after a rename: the project is at the new one. |
| 75 | await redirectIfRenamed(request, params.owner); |
| 76 | // A repository transferred to another workspace: it is there now. |
| 77 | await redirectIfTransferred(request, params.owner, params.repo, viewer); |
| 78 | throw notFound("project"); |
| 79 | } |
| 80 | let project: Project | null = found.ok ? found.value : null; |
| 81 | // A repository made a moment ago, before its project: make it now. |
| 82 | if (!project && repo.ok && !repo.value.forkOf) { |
| 83 | // The repository still has its page when its project cannot be read: drawn by its own name. |
| 84 | const own = orElse(await part(projects.byRepo(repo.value.id), "Its project"), []); |
| 85 | project = own.find((p) => p.slug === params.repo.toLowerCase()) ?? own[0] ?? null; |
| 86 | } |
| 87 | // Opened just now: it leads their Recent, recorded after the page is sent. |
| 88 | if (member && project) waitUntil(projects.visited(viewer!, project.id).catch(() => undefined)); |
| 89 | const value = unwrap(repo); |
| 90 | // The viewer's role on the repository and what it lets them do, for the |
| 91 | // pages under it and the sidebar. |
| 92 | const access = accessFor(viewer, value); |
| 93 | // Just given access with an invite: welcomed once (routes/invite.tsx). |
| 94 | const welcome = Boolean(access.role) && welcomes(request.headers.get("cookie"), `${value.namespace}/${value.name}`); |
| 95 | // Fork shows when it may be forked, and to its admins turned off when it |
| 96 | // may not; never on a pull request's working copy. |
| 97 | const forkable = !value.isPrivate || Boolean(value.allowForking); |
| 98 | const forking: Forking | null = |
| 99 | value.forkOf || forks == null || (!forkable && !access.can.administer) |
| 100 | ? null |
| 101 | : { count: Math.min(forks, FORKS_COUNTED), more: forks >= FORKS_COUNTED, allowed: forkable }; |
| 102 | const headers = welcome ? { "Set-Cookie": clearWelcome(new URL(request.url).protocol === "https:") } : undefined; |
| 103 | return data( |
| 104 | { |
| 105 | welcome, |
| 106 | repo: value, |
| 107 | project, |
| 108 | open: !isFailed(counts) && counts.ok ? counts.value : { items: 0, pulls: 0 }, |
| 109 | access, |
| 110 | member: access.insider, |
| 111 | watching, |
| 112 | // Null for a pull request's working copy, or when it could not be read. |
| 113 | stars: value.forkOf ? null : stars, |
| 114 | forking, |
| 115 | workItems, |
| 116 | signedIn: Boolean(viewer), |
| 117 | // Null when they cannot pin it: not one of their workspaces. |
| 118 | pinned: member && project ? (shortcuts?.pinned ?? []).some((pinned) => pinned.id === project.id) : null, |
| 119 | }, |
| 120 | { headers }, |
| 121 | ); |
| 122 | } |
| 123 | |
| 124 | /** |
| 125 | * The project's header, tabs and the viewer's role change with the project |
| 126 | * or after something was submitted, not from one page of it to the next, |
| 127 | * nor when a page refreshes itself. |
| 128 | */ |
| 129 | export function shouldRevalidate({ currentParams, nextParams, formMethod, defaultShouldRevalidate }: ShouldRevalidateFunctionArgs) { |
| 130 | if (formMethod && formMethod !== "GET") return defaultShouldRevalidate; |
| 131 | return currentParams.owner !== nextParams.owner || currentParams.repo !== nextParams.repo; |
| 132 | } |
| 133 | |
| 134 | /** The project the page is in, for the pages under it. */ |
| 135 | export function useProject() { |
| 136 | return useRouteLoaderData<typeof loader>("routes/repo/layout"); |
| 137 | } |
| 138 | |
| 139 | /** The project's name row: its kind, owner and name, visibility, and Pin, Watch and Star at the end. */ |
| 140 | function Header({ |
| 141 | project, |
| 142 | isPrivate, |
| 143 | archived, |
| 144 | namespace, |
| 145 | name, |
| 146 | large, |
| 147 | actions, |
| 148 | parent, |
| 149 | }: { |
| 150 | project: Project | null; |
| 151 | /** The repository it was forked from, when the viewer can see it. */ |
| 152 | parent?: { namespace: string; name: string } | null; |
| 153 | isPrivate: boolean; |
| 154 | namespace: string; |
| 155 | name: string; |
| 156 | archived?: boolean; |
| 157 | large?: boolean; |
| 158 | /** At the end of the row: Pin, Watch and Star. */ |
| 159 | actions?: ReactNode; |
| 160 | }) { |
| 161 | const base = `/${namespace}/${name}`; |
| 162 | return ( |
| 163 | <div className="flex flex-wrap items-center gap-x-3 gap-y-1"> |
| 164 | <span className={`flex shrink-0 items-center justify-center rounded-md bg-raised text-muted ring-1 ring-line ${large ? "size-8" : "size-6"}`}> |
| 165 | {isPrivate ? <Lock size={large ? 15 : 13} /> : <Box size={large ? 15 : 13} />} |
| 166 | </span> |
| 167 | <h1 className={large ? "text-lg" : "text-[0.9375rem]"}> |
| 168 | <Link to={`/${namespace}`} className="font-mono text-muted hover:text-fg"> |
| 169 | {namespace} |
| 170 | </Link> |
| 171 | <span className="mx-1.5 text-faint">/</span> |
| 172 | <Link to={base} className="font-semibold hover:underline"> |
| 173 | {project?.name ?? name} |
| 174 | </Link> |
| 175 | </h1> |
| 176 | <Badge size="md">{isPrivate ? "private" : "public"}</Badge> |
| 177 | {archived && <Badge size="md">archived</Badge>} |
| 178 | {actions && <div className="ml-auto flex shrink-0 items-center gap-2">{actions}</div>} |
| 179 | {parent && ( |
| 180 | <p className="flex basis-full items-center gap-1.5 text-xs text-muted"> |
| 181 | <GitFork size={12} className="shrink-0 text-faint" /> |
| 182 | forked from |
| 183 | <Link to={`/${parent.namespace}/${parent.name}`} className="font-mono text-fg/80 hover:text-accent hover:underline"> |
| 184 | {parent.namespace}/{parent.name} |
| 185 | </Link> |
| 186 | </p> |
| 187 | )} |
| 188 | </div> |
| 189 | ); |
| 190 | } |
| 191 | |
| 192 | /** |
| 193 | * The views of the section the page is in (Files, Commits and the rest |
| 194 | * under Code, and so on), at the top of the page's content: a secondary |
| 195 | * row that belongs to what is under it, not a second header. It runs the |
| 196 | * content's width on a rule of its own, so it heads everything below it |
| 197 | * and outweighs the filters there. Soon tabs open their roadmap page, |
| 198 | * under the same tabs. |
| 199 | */ |
| 200 | function PageTabs({ base, tabs }: { base: string; tabs: PageTab[] }) { |
| 201 | const { pathname } = useLocation(); |
| 202 | const rest = pathname.slice(base.length + 1); |
| 203 | const current = (tab: PageTab) => [tab.path, ...(tab.also ?? [])].some((path) => rest === path || (!tab.exact && rest.startsWith(`${path}/`))); |
| 204 | return ( |
| 205 | <div className="mb-6 border-b border-line pb-3"> |
| 206 | <NavTabs label="Views" variant="pill"> |
| 207 | {tabs.map((tab) => ( |
| 208 | <Hint key={tab.path} label={tab.about}> |
| 209 | <NavTab variant="pill" to={`${base}/${tab.path}`} current={current(tab)} soon={tab.soon}> |
| 210 | {tab.label} |
| 211 | </NavTab> |
| 212 | </Hint> |
| 213 | ))} |
| 214 | </NavTabs> |
| 215 | </div> |
| 216 | ); |
| 217 | } |
| 218 | |
| 219 | export default function ProjectLayout({ loaderData }: Route.ComponentProps) { |
| 220 | const { repo, project, member, access, welcome, watching, pinned, stars, forking, signedIn } = loaderData; |
| 221 | const base = `/${repo.namespace}/${repo.name}`; |
| 222 | // A repository has no description: its project's is shown. |
| 223 | const description = project?.description ?? null; |
| 224 | // Its homepage, else the repository's website. |
| 225 | const site = projectHomepage(project) ?? repo.website ?? null; |
| 226 | const { pathname } = useLocation(); |
| 227 | const rest = pathname.slice(base.length + 1); |
| 228 | const tabs = tabsFor(rest, member, access.can); |
| 229 | // One row of tabs for the project's pages, along the header's border (a |
| 230 | // member's sidebar has them on a computer), and the views of the section |
| 231 | // the page is in as a quieter row at the top of its content. |
| 232 | return ( |
| 233 | <> |
| 234 | <div data-page-head className="border-b border-line"> |
| 235 | {/* The project's pages close the header for a visitor, and on a phone; a member's sidebar lists them from 768px. */} |
| 236 | <div className={`mx-auto max-w-6xl px-4 pt-4 sm:px-5 ${signedIn ? "pb-4 max-md:pb-0" : ""}`}> |
| 237 | <Header |
| 238 | project={project} |
| 239 | large={!rest} |
| 240 | isPrivate={repo.isPrivate} |
| 241 | archived={Boolean(repo.archivedAt)} |
| 242 | namespace={repo.namespace} |
| 243 | name={repo.name} |
| 244 | parent={repo.parent} |
| 245 | actions={ |
| 246 | watching || stars || forking || (project && (pinned != null || access.can.manage_settings)) ? ( |
| 247 | <> |
| 248 | {project && access.can.manage_settings && <AboutEditor project={project} action={`${base}?index`} />} |
| 249 | {project && pinned != null && <PinButton workspace={project.workspace} slug={project.slug} name={project.name} pinned={pinned} />} |
| 250 | {watching && <WatchMenu action={`${base}/notifications`} level={watching.level} events={watching.events} />} |
| 251 | {forking && <ForkButton base={base} name={`${repo.namespace}/${repo.name}`} forking={forking} signedIn={signedIn} />} |
| 252 | {stars && <StarButton base={base} name={`${repo.namespace}/${repo.name}`} stars={stars} signedIn={signedIn} />} |
| 253 | </> |
| 254 | ) : null |
| 255 | } |
| 256 | /> |
| 257 | {/* What it is, where it lives and its topics, the same on every page of it. */} |
| 258 | {description && <p className="mt-2 max-w-3xl text-sm text-muted">{description}</p>} |
| 259 | {project ? ( |
| 260 | <LinkList links={{ ...project.links, homepage: project.links.homepage ?? site }} className="mt-2 flex flex-wrap gap-x-5 gap-y-2 space-y-0" /> |
| 261 | ) : ( |
| 262 | site && ( |
| 263 | <a href={site} rel="noopener nofollow" className="mt-2 inline-flex items-center gap-1.5 text-sm text-accent hover:underline"> |
| 264 | <Link2 size={14} /> |
| 265 | {site.replace(/^https?:\/\//, "")} |
| 266 | </a> |
| 267 | ) |
| 268 | )} |
| 269 | <Topics topics={repo.topics} className="mt-3" /> |
| 270 | <div className={signedIn ? "mt-3 md:hidden" : "mt-3"}> |
| 271 | <ProjectStrip base={base} member={member} can={access.can} settings={seesSettings(access)} counts={loaderData.open} always={!signedIn} /> |
| 272 | </div> |
| 273 | </div> |
| 274 | </div> |
| 275 | <div className="mx-auto max-w-6xl px-4 py-6 sm:px-5"> |
| 276 | {tabs && <PageTabs base={base} tabs={tabs} />} |
| 277 | {welcome && ( |
| 278 | <div className="mb-6"> |
| 279 | <WelcomeBanner title={`You're in ${repo.namespace}/${repo.name}`}> |
| 280 | You have the {access.role} role on it now. Its code, work items and pull requests are yours to work on. |
| 281 | </WelcomeBanner> |
| 282 | </div> |
| 283 | )} |
| 284 | {repo.archivedAt && ( |
| 285 | <div className="mb-6"> |
| 286 | <ArchivedBanner base={base} owner={access.can.administer} settings={/^settings(\/|$)/.test(pathname.slice(base.length + 1))} /> |
| 287 | </div> |
| 288 | )} |
| 289 | <Outlet /> |
| 290 | </div> |
| 291 | </> |
| 292 | ); |
| 293 | } |