| 1 | import { Box, CircleDot, Code2, GitPullRequest, History, LayoutGrid, ListTree, Lock, Rocket, Settings } from "lucide-react"; |
| 2 | import { Link, NavLink, Outlet, data, useLocation, useRouteLoaderData } from "react-router"; |
| 3 | |
| 4 | import type { Project } from "@g1t/contracts"; |
| 5 | |
| 6 | import type { Route } from "./+types/layout"; |
| 7 | import { page } from "../../lib/meta"; |
| 8 | import { type Tab as PageTab, tabsFor } from "../../lib/project-nav"; |
| 9 | import { Pill, TabLink as Tab } from "../../components/ui"; |
| 10 | import { projects, repos, work } from "../../lib/services.server"; |
| 11 | import { getViewer, roleIn, unwrap } from "../../lib/session.server"; |
| 12 | |
| 13 | export function meta({ loaderData: loaded, params, ...args }: Route.MetaArgs) { |
| 14 | return page(args, { title: `${loaded?.project?.name ?? params.repo} · ${params.owner} · g1t` }); |
| 15 | } |
| 16 | |
| 17 | export async function loader({ params, context }: Route.LoaderArgs) { |
| 18 | const viewer = getViewer(context); |
| 19 | const path = { namespace: params.owner, name: params.repo }; |
| 20 | const [repo, counts, found] = await Promise.all([ |
| 21 | repos.get(path, viewer), |
| 22 | work.counts(path, viewer), |
| 23 | projects.get(params.owner, params.repo, viewer), |
| 24 | ]); |
| 25 | if (!repo.ok && !found.ok) throw data(null, { status: 404 }); |
| 26 | let project: Project | null = found.ok ? found.value : null; |
| 27 | // A repository made a moment ago, before its project: make it now. |
| 28 | if (!project && repo.ok && !repo.value.forkOf) { |
| 29 | const own = await projects.byRepo(repo.value.id); |
| 30 | project = own.find((p) => p.slug === params.repo.toLowerCase()) ?? own[0] ?? null; |
| 31 | } |
| 32 | return { |
| 33 | repo: unwrap(repo), |
| 34 | project, |
| 35 | open: counts.ok ? counts.value : { issues: 0, pulls: 0 }, |
| 36 | member: roleIn(viewer, params.owner) != null, |
| 37 | }; |
| 38 | } |
| 39 | |
| 40 | /** The project the page is in, for the pages under it. */ |
| 41 | export function useProject() { |
| 42 | return useRouteLoaderData<typeof loader>("routes/repo/layout"); |
| 43 | } |
| 44 | |
| 45 | function Header({ project, isPrivate, namespace, name, description, large }: { |
| 46 | project: Project | null; |
| 47 | isPrivate: boolean; |
| 48 | namespace: string; |
| 49 | name: string; |
| 50 | description: string | null; |
| 51 | large?: boolean; |
| 52 | }) { |
| 53 | const base = `/${namespace}/${name}`; |
| 54 | return ( |
| 55 | <div className="flex flex-wrap items-center gap-x-3 gap-y-1"> |
| 56 | <span |
| 57 | className={`flex shrink-0 items-center justify-center rounded-md bg-raised text-muted ring-1 ring-line ${large ? "size-8" : "size-6"}`} |
| 58 | > |
| 59 | {isPrivate ? <Lock size={large ? 15 : 13} /> : <Box size={large ? 15 : 13} />} |
| 60 | </span> |
| 61 | <h1 className={large ? "text-lg" : "text-[0.9375rem]"}> |
| 62 | <Link to={`/${namespace}`} className="font-mono text-muted hover:text-fg"> |
| 63 | {namespace} |
| 64 | </Link> |
| 65 | <span className="mx-1.5 text-faint">/</span> |
| 66 | <Link to={base} className="font-semibold hover:underline"> |
| 67 | {project?.name ?? name} |
| 68 | </Link> |
| 69 | </h1> |
| 70 | <Pill>{isPrivate ? "private" : "public"}</Pill> |
| 71 | {description && <p className="min-w-0 truncate text-sm text-muted">{description}</p>} |
| 72 | </div> |
| 73 | ); |
| 74 | } |
| 75 | |
| 76 | /** |
| 77 | * The views of the page the project is on, as tabs across its top: Files, |
| 78 | * Commits and what is coming under Code, and so on. Soon tabs open their |
| 79 | * roadmap page, under the same tabs. |
| 80 | */ |
| 81 | function PageTabs({ base, tabs }: { base: string; tabs: PageTab[] }) { |
| 82 | const { pathname } = useLocation(); |
| 83 | const rest = pathname.slice(base.length + 1); |
| 84 | const current = (tab: PageTab) => |
| 85 | [tab.path, ...(tab.also ?? [])].some((path) => rest === path || rest.startsWith(`${path}/`)); |
| 86 | return ( |
| 87 | <nav aria-label="Views" className="-mb-px flex gap-1 overflow-x-auto [scrollbar-width:none] [&::-webkit-scrollbar]:hidden"> |
| 88 | {tabs.map((tab) => ( |
| 89 | <NavLink |
| 90 | key={tab.path} |
| 91 | to={`${base}/${tab.path}`} |
| 92 | title={tab.about} |
| 93 | prefetch="intent" |
| 94 | className={`flex shrink-0 items-center gap-1.5 border-b-2 px-3 pt-1 pb-2.5 text-sm transition-colors ${ |
| 95 | current(tab) |
| 96 | ? "border-accent font-medium text-fg" |
| 97 | : tab.soon |
| 98 | ? "border-transparent text-faint hover:text-muted" |
| 99 | : "border-transparent text-muted hover:text-fg" |
| 100 | }`} |
| 101 | > |
| 102 | {tab.label} |
| 103 | {/* Soon, said quietly: a dot, with the word for screen readers. */} |
| 104 | {tab.soon && ( |
| 105 | <> |
| 106 | <span aria-hidden="true" className="size-1.5 rounded-full bg-accent/70" /> |
| 107 | <span className="sr-only">(soon)</span> |
| 108 | </> |
| 109 | )} |
| 110 | </NavLink> |
| 111 | ))} |
| 112 | </nav> |
| 113 | ); |
| 114 | } |
| 115 | |
| 116 | export default function ProjectLayout({ loaderData }: Route.ComponentProps) { |
| 117 | const { repo, project, open, member } = loaderData; |
| 118 | const base = `/${repo.namespace}/${repo.name}`; |
| 119 | const signedIn = useRouteLoaderData("root")?.user != null; |
| 120 | const description = project?.description ?? repo.description; |
| 121 | const { pathname } = useLocation(); |
| 122 | const tabs = tabsFor(pathname.slice(base.length + 1), member); |
| 123 | if (signedIn) { |
| 124 | return ( |
| 125 | <> |
| 126 | <div className="border-b border-line"> |
| 127 | <div className={`mx-auto max-w-6xl px-4 sm:px-6 ${tabs ? "pt-3" : "py-3"}`}> |
| 128 | <Header |
| 129 | project={project} |
| 130 | isPrivate={repo.isPrivate} |
| 131 | namespace={repo.namespace} |
| 132 | name={repo.name} |
| 133 | description={description} |
| 134 | /> |
| 135 | {tabs && ( |
| 136 | <div className="mt-3"> |
| 137 | <PageTabs base={base} tabs={tabs} /> |
| 138 | </div> |
| 139 | )} |
| 140 | </div> |
| 141 | </div> |
| 142 | <div className="mx-auto max-w-6xl px-4 py-6 sm:px-6"> |
| 143 | <Outlet /> |
| 144 | </div> |
| 145 | </> |
| 146 | ); |
| 147 | } |
| 148 | return ( |
| 149 | <> |
| 150 | {/* The project's own header band, under the site header. */} |
| 151 | <div className="border-b border-line bg-surface/60"> |
| 152 | <div className="mx-auto max-w-6xl px-4 pt-6"> |
| 153 | <Header |
| 154 | project={project} |
| 155 | isPrivate={repo.isPrivate} |
| 156 | namespace={repo.namespace} |
| 157 | name={repo.name} |
| 158 | description={null} |
| 159 | large |
| 160 | /> |
| 161 | {description && <p className="mt-2 max-w-2xl text-sm text-muted">{description}</p>} |
| 162 | <nav className="mt-5 flex gap-6 overflow-x-auto"> |
| 163 | <Tab to={base} end icon={<LayoutGrid size={15} />}> |
| 164 | Overview |
| 165 | </Tab> |
| 166 | <Tab to={`${base}/code`} also={`${base}/tree`} icon={<Code2 size={15} />}> |
| 167 | Code |
| 168 | </Tab> |
| 169 | <Tab to={`${base}/issues`} icon={<CircleDot size={15} />} count={open.issues}> |
| 170 | Issues |
| 171 | </Tab> |
| 172 | <Tab to={`${base}/pulls`} also={`${base}/pull`} icon={<GitPullRequest size={15} />} count={open.pulls}> |
| 173 | Pull requests |
| 174 | </Tab> |
| 175 | <Tab to={`${base}/commits`} icon={<History size={15} />}> |
| 176 | Commits |
| 177 | </Tab> |
| 178 | {member && ( |
| 179 | <Tab to={`${base}/plans`} icon={<ListTree size={15} />}> |
| 180 | Plan |
| 181 | </Tab> |
| 182 | )} |
| 183 | {member && ( |
| 184 | <Tab to={`${base}/deployments`} icon={<Rocket size={15} />}> |
| 185 | Deployments |
| 186 | </Tab> |
| 187 | )} |
| 188 | {member && ( |
| 189 | <Tab to={`${base}/settings`} icon={<Settings size={15} />}> |
| 190 | Settings |
| 191 | </Tab> |
| 192 | )} |
| 193 | </nav> |
| 194 | </div> |
| 195 | </div> |
| 196 | <div className="mx-auto max-w-6xl px-4 py-6"> |
| 197 | <Outlet /> |
| 198 | </div> |
| 199 | </> |
| 200 | ); |
| 201 | } |