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

202 lines8,005 bytesCodeBlame
1import { Box, Lock } 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 } from "../../components/ui";
10import { ArchivedBanner } from "../../components/repo-lifecycle";
11import { WelcomeBanner } from "../../components/welcome";
12import { clearWelcome, welcomes } from "../../lib/invites";
13import { notFound } from "../../lib/not-found.server";
14import { redirectIfRenamed, redirectIfTransferred } from "../../lib/renamed.server";
15import { accessFor, repoFor } from "../../lib/access.server";
16import { projects, work } from "../../lib/services.server";
17import { getViewer, unwrap } from "../../lib/session.server";
18
19export function meta({ loaderData: loaded, params, ...args }: Route.MetaArgs) {
20 return page(args, { title: `${loaded?.project?.name ?? params.repo} · ${params.owner} · g1t` });
21}
22
23export async function loader({ params, context, request }: Route.LoaderArgs) {
24 const viewer = getViewer(context);
25 const path = { namespace: params.owner, name: params.repo };
26 const [repo, counts, found] = await Promise.all([
27 repoFor(context, params),
28 work.counts(path, viewer),
29 projects.get(params.owner, params.repo, viewer),
30 ]);
31 if (!repo.ok && !found.ok) {
32 // Under a workspace's old name, after a rename: the project is at the new one.
33 await redirectIfRenamed(request, params.owner);
34 // A repository transferred to another workspace: it is there now.
35 await redirectIfTransferred(request, params.owner, params.repo, viewer);
36 throw notFound("project");
37 }
38 let project: Project | null = found.ok ? found.value : null;
39 // A repository made a moment ago, before its project: make it now.
40 if (!project && repo.ok && !repo.value.forkOf) {
41 const own = await projects.byRepo(repo.value.id);
42 project = own.find((p) => p.slug === params.repo.toLowerCase()) ?? own[0] ?? null;
43 }
44 const value = unwrap(repo);
45 // The viewer's role on the repository and what it lets them do, for the
46 // pages under it and the sidebar.
47 const access = accessFor(viewer, value);
48 // Just given access with an invite: welcomed once (routes/invite.tsx).
49 const welcome = Boolean(access.role) && welcomes(request.headers.get("cookie"), `${value.namespace}/${value.name}`);
50 const headers = welcome ? { "Set-Cookie": clearWelcome(new URL(request.url).protocol === "https:") } : undefined;
51 return data({
52 welcome,
53 repo: value,
54 project,
55 open: counts.ok ? counts.value : { issues: 0, pulls: 0 },
56 access,
57 member: access.insider,
58 }, { headers });
59}
60
61/** The project the page is in, for the pages under it. */
62export function useProject() {
63 return useRouteLoaderData<typeof loader>("routes/repo/layout");
64}
65
66/** A repository's topics, each a way into Explore. */
67function Topics({ topics }: { topics: string[] | undefined }) {
68 if (!topics?.length) return null;
69 return (
70 <div className="mt-2 flex flex-wrap gap-1.5">
71 {topics.map((topic) => (
72 <Link
73 key={topic}
74 to={`/explore?topic=${encodeURIComponent(topic)}`}
75 className="rounded-full bg-accent/10 px-2 py-px text-xs text-accent ring-1 ring-accent/30 transition-colors hover:bg-accent/20"
76 >
77 {topic}
78 </Link>
79 ))}
80 </div>
81 );
82}
83
84function Header({ project, isPrivate, archived, namespace, name, description, large }: {
85 project: Project | null;
86 isPrivate: boolean;
87 namespace: string;
88 name: string;
89 description: string | null;
90 archived?: boolean;
91 large?: boolean;
92}) {
93 const base = `/${namespace}/${name}`;
94 return (
95 <div className="flex flex-wrap items-center gap-x-3 gap-y-1">
96 <span
97 className={`flex shrink-0 items-center justify-center rounded-md bg-raised text-muted ring-1 ring-line ${large ? "size-8" : "size-6"}`}
98 >
99 {isPrivate ? <Lock size={large ? 15 : 13} /> : <Box size={large ? 15 : 13} />}
100 </span>
101 <h1 className={large ? "text-lg" : "text-[0.9375rem]"}>
102 <Link to={`/${namespace}`} className="font-mono text-muted hover:text-fg">
103 {namespace}
104 </Link>
105 <span className="mx-1.5 text-faint">/</span>
106 <Link to={base} className="font-semibold hover:underline">
107 {project?.name ?? name}
108 </Link>
109 </h1>
110 <Pill>{isPrivate ? "private" : "public"}</Pill>
111 {archived && <Pill>archived</Pill>}
112 {description && <p className="min-w-0 truncate text-sm text-muted">{description}</p>}
113 </div>
114 );
115}
116
117/**
118 * The views of the page the project is on, as tabs across its top: Files,
119 * Commits and what is coming under Code, and so on. Soon tabs open their
120 * roadmap page, under the same tabs.
121 */
122function PageTabs({ base, tabs }: { base: string; tabs: PageTab[] }) {
123 const { pathname } = useLocation();
124 const rest = pathname.slice(base.length + 1);
125 const current = (tab: PageTab) =>
126 [tab.path, ...(tab.also ?? [])].some((path) => rest === path || rest.startsWith(`${path}/`));
127 return (
128 <nav aria-label="Views" className="relative -mb-px flex gap-1 overflow-x-auto [scrollbar-width:none] [&::-webkit-scrollbar]:hidden">
129 {tabs.map((tab) => (
130 <NavLink
131 key={tab.path}
132 to={`${base}/${tab.path}`}
133 title={tab.about}
134 prefetch="intent"
135 className={`flex shrink-0 items-center gap-1.5 border-b-2 px-3 pt-1 pb-2.5 text-sm transition-colors ${
136 current(tab)
137 ? "border-accent font-medium text-fg"
138 : tab.soon
139 ? "border-transparent text-faint hover:text-muted"
140 : "border-transparent text-muted hover:text-fg"
141 }`}
142 >
143 {tab.label}
144 {/* Soon, said quietly: a dot, with the word for screen readers. */}
145 {tab.soon && (
146 <>
147 <span aria-hidden="true" className="size-1.5 rounded-full bg-accent/70" />
148 <span className="sr-only">(soon)</span>
149 </>
150 )}
151 </NavLink>
152 ))}
153 </nav>
154 );
155}
156
157export default function ProjectLayout({ loaderData }: Route.ComponentProps) {
158 const { repo, project, member, access, welcome } = loaderData;
159 const base = `/${repo.namespace}/${repo.name}`;
160 const description = project?.description ?? repo.description;
161 const { pathname } = useLocation();
162 const tabs = tabsFor(pathname.slice(base.length + 1), member, access.can);
163 // Everyone, signed in or not, finds the project's pages in the sidebar;
164 // the page shows its name, and the views of the page it is on as tabs.
165 return (
166 <>
167 <div className="border-b border-line">
168 <div className={`mx-auto max-w-6xl px-4 sm:px-6 ${tabs ? "pt-3" : "py-3"}`}>
169 <Header
170 project={project}
171 isPrivate={repo.isPrivate}
172 archived={Boolean(repo.archivedAt)}
173 namespace={repo.namespace}
174 name={repo.name}
175 description={description}
176 />
177 <Topics topics={repo.topics} />
178 {tabs && (
179 <div className="mt-3">
180 <PageTabs base={base} tabs={tabs} />
181 </div>
182 )}
183 </div>
184 </div>
185 <div className="mx-auto max-w-6xl px-4 py-6 sm:px-6">
186 {welcome && (
187 <div className="mb-6">
188 <WelcomeBanner title={`You're in ${repo.namespace}/${repo.name}`}>
189 You have the {access.role} role on it now. Its code, issues and pull requests are yours to work on.
190 </WelcomeBanner>
191 </div>
192 )}
193 {repo.archivedAt && (
194 <div className="mb-6">
195 <ArchivedBanner base={base} owner={access.can.administer} settings={/^settings(\/|$)/.test(pathname.slice(base.length + 1))} />
196 </div>
197 )}
198 <Outlet />
199 </div>
200 </>
201 );
202}