pr_01m47d15m3e54sn21z27rpy5n9/apps/web/app/routes/repo/layout.tsx

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