flagon-io/g1t

public

Where people and agents ship software together. The open-source git platform for the whole job: issues, agents, checks and deploys to the edge.

g1t/apps/web/app/root.tsx

425 lines14,932 bytesCodeBlame
1import {
2 BookOpen,
3 ChevronDown,
4 LayoutDashboard,
5 LogOut,
6 Plus,
7 Search,
8 Settings,
9} from "lucide-react";
10import {
11 Form,
12 isRouteErrorResponse,
13 Link,
14 Links,
15 Meta,
16 NavLink,
17 Outlet,
18 Scripts,
19 ScrollRestoration,
20 type ShouldRevalidateFunctionArgs,
21 useRouteLoaderData,
22 useSubmit,
23} from "react-router";
24
25import type { User } from "@g1t/contracts";
26
27import type { Route } from "./+types/root";
28import "./app.css";
29import { Logo } from "./components/logo";
30import { Avatar, ButtonLink, notACredential } from "./components/ui";
31import {
32 DropdownMenu,
33 DropdownMenuContent,
34 DropdownMenuItem,
35 DropdownMenuLabel,
36 DropdownMenuSeparator,
37 DropdownMenuTrigger,
38} from "./components/ui/dropdown-menu";
39import { AppShell, Progress, type ShellData } from "./components/shell";
40import { billing, projects, work } from "./lib/services.server";
41import { getViewer, roleIn, viewerMiddleware } from "./lib/session.server";
42
43export const links: Route.LinksFunction = () => [
44 { rel: "icon", href: "/favicon.ico", sizes: "32x32" },
45 { rel: "icon", type: "image/svg+xml", href: "/favicon.svg" },
46 { rel: "icon", type: "image/png", sizes: "192x192", href: "/icon-192.png" },
47 { rel: "apple-touch-icon", href: "/apple-touch-icon.png" },
48 { rel: "preconnect", href: "https://fonts.googleapis.com" },
49 {
50 rel: "preconnect",
51 href: "https://fonts.gstatic.com",
52 crossOrigin: "anonymous",
53 },
54 {
55 rel: "stylesheet",
56 href: "https://fonts.googleapis.com/css2?family=Inter:opsz,wght@14..32,400..700&family=JetBrains+Mono:wght@400;500;600&display=swap",
57 },
58];
59
60export const middleware: Route.MiddlewareFunction[] = [viewerMiddleware];
61
62export async function loader({ context, params }: Route.LoaderArgs) {
63 const user = getViewer(context);
64 return { user, shell: user ? await shellFor(user, params) : null };
65}
66
67/**
68 * The sidebar only changes with the workspace or repository being looked
69 * at, or after something was submitted: not on every page within them.
70 */
71export function shouldRevalidate({
72 currentParams,
73 nextParams,
74 formMethod,
75 defaultShouldRevalidate,
76}: ShouldRevalidateFunctionArgs) {
77 if (formMethod && formMethod !== "GET") return defaultShouldRevalidate;
78 return currentParams.owner !== nextParams.owner || currentParams.repo !== nextParams.repo;
79}
80
81/**
82 * The sidebar: the workspace being looked at if they belong to it, else
83 * their first; its repositories; and the repository being looked at.
84 */
85async function shellFor(
86 user: User,
87 params: { owner?: string; repo?: string },
88): Promise<ShellData> {
89 const memberships = user.workspaces ?? [];
90 const here = params.owner ? memberships.find((m) => m.slug === params.owner?.toLowerCase()) : undefined;
91 const workspace = here ?? memberships[0] ?? null;
92 const path = params.owner && params.repo ? { namespace: params.owner, name: params.repo } : null;
93 const now = new Date();
94 const monthStart = new Date(Date.UTC(now.getUTCFullYear(), now.getUTCMonth(), 1)).toISOString();
95 const [listed, counts, account, usage, limit] = await Promise.all([
96 workspace ? projects.list(workspace.slug, user) : Promise.resolve(null),
97 path ? work.counts(path, user) : Promise.resolve(null),
98 workspace ? billing.account(workspace.slug, user) : Promise.resolve(null),
99 workspace ? billing.usage(workspace.slug, user, monthStart) : Promise.resolve(null),
100 workspace ? billing.limit(workspace.slug, user).catch(() => null) : Promise.resolve(null),
101 ]);
102 return {
103 workspace,
104 // Projects are what the sidebar lists: what the workspace builds and runs.
105 repos: listed?.ok
106 ? listed.value.map((project) => ({
107 namespace: project.workspace,
108 name: project.slug,
109 title: project.name,
110 isPrivate: project.private,
111 }))
112 : [],
113 repo:
114 path && counts?.ok
115 ? {
116 ...path,
117 member: roleIn(user, path.namespace) != null,
118 issues: counts.value.issues,
119 pulls: counts.value.pulls,
120 }
121 : null,
122 // Where the workspace stands against its usage limit, once billing is on.
123 limit:
124 account?.ok && account.value.status.enabled && limit?.ok
125 ? {
126 exposureMicros: limit.value.exposureMicros,
127 ceilingMicros: limit.value.ceilingMicros,
128 state: limit.value.state,
129 comped: limit.value.trust === "internal",
130 }
131 : null,
132 free: account?.ok ? Boolean(account.value.status.free) : false,
133 // While g1t is free every charge is zero, so usage is shown at cost.
134 monthUsageMicros: usage?.ok ? (usage.value.free ? usage.value.usedMicros : usage.value.spentMicros) : null,
135 };
136}
137
138function HeaderLink({ to, children }: { to: string; children: React.ReactNode }) {
139 return (
140 <NavLink
141 to={to}
142 className={({ isActive }) =>
143 `rounded-md px-2.5 py-1.5 text-sm transition-colors hover:bg-raised hover:text-fg ${
144 isActive ? "text-fg" : "text-muted"
145 }`
146 }
147 >
148 {children}
149 </NavLink>
150 );
151}
152
153function Header({ user }: { user: User | null | undefined }) {
154 const submit = useSubmit();
155 return (
156 <header className="sticky top-0 z-40 border-b border-line bg-surface/85 backdrop-blur">
157 <div className="mx-auto flex h-14 max-w-6xl items-center gap-2 px-4">
158 <Link to="/" aria-label="g1t home" className="mr-2">
159 <Logo />
160 </Link>
161 <Form action="/search" role="search" className="relative hidden grow sm:block sm:max-w-xs">
162 <Search
163 size={14}
164 className="pointer-events-none absolute top-1/2 left-3 -translate-y-1/2 text-faint"
165 />
166 <input
167 name="q"
168 {...notACredential()}
169 placeholder="Search repositories"
170 aria-label="Search repositories"
171 className="w-full rounded-md border border-line bg-bg py-1.5 pr-3 pl-8 text-sm outline-none transition-colors placeholder:text-faint hover:border-line-strong focus:border-accent-dim"
172 />
173 </Form>
174 <nav className="flex items-center gap-0.5">
175 <HeaderLink to="/explore">Explore</HeaderLink>
176 <HeaderLink to="/pricing">Pricing</HeaderLink>
177 <HeaderLink to="https://docs.g1t.sh/">Docs</HeaderLink>
178 </nav>
179 <div className="ml-auto flex items-center gap-2">
180 {user ? (
181 <>
182 <Link
183 to="/new"
184 aria-label="New project"
185 className="rounded-md border border-line p-1.5 text-muted transition-colors hover:border-line-strong hover:text-fg"
186 >
187 <Plus size={16} />
188 </Link>
189 <DropdownMenu>
190 <DropdownMenuTrigger
191 aria-label="Account menu"
192 className="flex items-center gap-1.5 rounded-md p-1 outline-none transition-colors hover:bg-raised data-[state=open]:bg-raised"
193 >
194 <Avatar name={user.username} image={user.avatar} size={24} />
195 <ChevronDown size={14} className="text-faint" />
196 </DropdownMenuTrigger>
197 <DropdownMenuContent align="end" className="w-56">
198 <DropdownMenuLabel>
199 Signed in as{" "}
200 <span className="font-mono font-medium text-fg">
201 {user.username}
202 </span>
203 </DropdownMenuLabel>
204 <DropdownMenuSeparator />
205 {(user.workspaces ?? []).map((membership) => (
206 <DropdownMenuItem asChild key={membership.slug}>
207 <Link to={`/${membership.slug}`}>
208 <Avatar name={membership.slug} image={membership.avatar} size={16} square />
209 <span className="min-w-0 truncate">{membership.name || membership.slug}</span>
210 </Link>
211 </DropdownMenuItem>
212 ))}
213 <DropdownMenuItem asChild>
214 <Link to="/workspaces/new">
215 <Plus />
216 New workspace
217 </Link>
218 </DropdownMenuItem>
219 <DropdownMenuSeparator />
220 <DropdownMenuItem asChild>
221 <Link to="/">
222 <LayoutDashboard />
223 Mission control
224 </Link>
225 </DropdownMenuItem>
226 <DropdownMenuItem asChild>
227 <Link to="/new">
228 <Plus />
229 New project
230 </Link>
231 </DropdownMenuItem>
232 <DropdownMenuSeparator />
233 <DropdownMenuItem asChild>
234 <Link to="/settings">
235 <Settings />
236 Settings
237 </Link>
238 </DropdownMenuItem>
239 <DropdownMenuItem asChild>
240 <Link to="https://docs.g1t.sh/quickstart/">
241 <BookOpen />
242 Documentation
243 </Link>
244 </DropdownMenuItem>
245 <DropdownMenuSeparator />
246 {/* Submitted from here: the menu closes on select, and a button
247 that has left the page cannot submit a form. */}
248 <DropdownMenuItem
249 onSelect={() => submit(null, { method: "post", action: "/logout" })}
250 >
251 <LogOut />
252 Sign out
253 </DropdownMenuItem>
254 </DropdownMenuContent>
255 </DropdownMenu>
256 </>
257 ) : (
258 <>
259 <HeaderLink to="/login">Sign in</HeaderLink>
260 <ButtonLink to="/register">Sign up</ButtonLink>
261 </>
262 )}
263 </div>
264 </div>
265 </header>
266 );
267}
268
269const FOOTER_LINKS: { title: string; links: [string, string][] }[] = [
270 {
271 title: "Product",
272 links: [
273 ["Explore repositories", "/explore"],
274 ["Pricing", "/pricing"],
275 ["g1t agents", "https://docs.g1t.sh/guides/g1t-agents/"],
276 ["Bring your own agent", "https://docs.g1t.sh/guides/bring-your-own-agent/"],
277 ["Integrations", "https://docs.g1t.sh/guides/integrations/"],
278 ["Model providers", "https://docs.g1t.sh/guides/models/"],
279 ["Sign up", "/register"],
280 ],
281 },
282 {
283 title: "Developers",
284 links: [
285 ["Quickstart", "https://docs.g1t.sh/quickstart/"],
286 ["How g1t works", "https://docs.g1t.sh/concepts/overview/"],
287 ["API reference", "https://docs.g1t.sh/reference/api/"],
288 ["OpenAPI", "https://api.g1t.sh/openapi.json"],
289 ["llms.txt", "/llms.txt"],
290 ],
291 },
292 {
293 title: "Project",
294 links: [
295 ["Source on g1t", "/syntaqx/g1t"],
296 ["Source on GitHub", "https://github.com/syntaqx/g1t"],
297 ["MIT license", "/syntaqx/g1t/blob/main/LICENSE"],
298 ],
299 },
300];
301
302function Footer() {
303 return (
304 <footer className="mt-24 border-t border-line">
305 <div className="mx-auto flex max-w-6xl flex-wrap gap-x-20 gap-y-8 px-4 py-12">
306 <div className="grow">
307 <Logo />
308 <p className="mt-3 max-w-xs text-sm text-muted">
309 A git forge for agents. Open source under the MIT license, built on
310 Cloudflare Workers and Artifacts.
311 </p>
312 </div>
313 {FOOTER_LINKS.map((group) => (
314 <div key={group.title}>
315 <p className="text-sm font-medium">{group.title}</p>
316 <ul className="mt-3 space-y-2 text-sm text-muted">
317 {group.links.map(([label, to]) => (
318 <li key={to}>
319 {/* Plain links: some targets are files or other hosts. */}
320 <a href={to} className="hover:text-fg">
321 {label}
322 </a>
323 </li>
324 ))}
325 </ul>
326 </div>
327 ))}
328 </div>
329 <div className="border-t border-line">
330 <p className="mx-auto max-w-6xl px-4 py-5 text-xs text-faint">
331 g1t is open source software, built on Cloudflare Workers and
332 Artifacts.
333 </p>
334 </div>
335 </footer>
336 );
337}
338
339export function Layout({ children }: { children: React.ReactNode }) {
340 // Undefined when the root loader itself failed.
341 const root = useRouteLoaderData<typeof loader>("root");
342 const user = root?.user;
343 const banner = user && !user.verified && (
344 <Form
345 method="post"
346 action="/verify"
347 className="flex flex-wrap items-center justify-center gap-x-3 gap-y-1 border-b border-warn/30 bg-warn/10 px-4 py-2 text-sm"
348 >
349 <span>
350 Confirm your email address to create repositories and push. We
351 sent you a link.
352 </span>
353 <button type="submit" className="font-medium underline underline-offset-4">
354 Send it again
355 </button>
356 </Form>
357 );
358 return (
359 <html lang="en">
360 <head>
361 <meta charSet="utf-8" />
362 <meta name="viewport" content="width=device-width, initial-scale=1" />
363 <meta name="theme-color" content="#0f0f11" />
364 <Meta />
365 <Links />
366 </head>
367 <body className="flex min-h-screen flex-col">
368 {user && root?.shell ? (
369 <AppShell user={user} shell={root.shell} banner={banner}>
370 {children}
371 </AppShell>
372 ) : (
373 <>
374 <Progress />
375 <Header user={user} />
376 {banner}
377 <div className="grow">{children}</div>
378 <Footer />
379 </>
380 )}
381 <ScrollRestoration />
382 <Scripts />
383 </body>
384 </html>
385 );
386}
387
388export default function App() {
389 return <Outlet />;
390}
391
392export function ErrorBoundary({ error }: Route.ErrorBoundaryProps) {
393 let title = "Something went wrong";
394 let details = "An unexpected error occurred. Try again in a moment.";
395 let stack: string | undefined;
396
397 if (isRouteErrorResponse(error)) {
398 if (error.status === 404) {
399 title = "Page not found";
400 details = "There is nothing at this address, or you do not have access to it.";
401 } else if (typeof error.data === "string" && error.data) {
402 details = error.data;
403 }
404 } else if (import.meta.env.DEV && error instanceof Error) {
405 details = error.message;
406 stack = error.stack;
407 }
408
409 return (
410 <main className="mx-auto max-w-xl px-4 py-32 text-center">
411 <h1 className="text-2xl font-semibold tracking-tight">{title}</h1>
412 <p className="mt-3 text-muted">{details}</p>
413 <div className="mt-8">
414 <ButtonLink to="/" variant="quiet">
415 Back to home
416 </ButtonLink>
417 </div>
418 {stack && (
419 <pre className="mt-8 overflow-x-auto rounded-lg border border-line p-4 text-left text-xs">
420 <code>{stack}</code>
421 </pre>
422 )}
423 </main>
424 );
425}