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

415 lines14,455 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] = 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 ]);
101 return {
102 workspace,
103 // Projects are what the sidebar lists: what the workspace builds and runs.
104 repos: listed?.ok
105 ? listed.value.map((project) => ({
106 namespace: project.workspace,
107 name: project.slug,
108 title: project.name,
109 isPrivate: project.private,
110 }))
111 : [],
112 repo:
113 path && counts?.ok
114 ? {
115 ...path,
116 member: roleIn(user, path.namespace) != null,
117 issues: counts.value.issues,
118 pulls: counts.value.pulls,
119 }
120 : null,
121 // While g1t is being built out nothing is charged, so no credit is shown.
122 creditMicros:
123 account?.ok && account.value.status.enabled && !account.value.status.free ? account.value.balanceMicros : null,
124 free: account?.ok ? Boolean(account.value.status.free) : false,
125 // While g1t is free every charge is zero, so usage is shown at cost.
126 monthUsageMicros: usage?.ok ? (usage.value.free ? usage.value.usedMicros : usage.value.spentMicros) : null,
127 };
128}
129
130function HeaderLink({ to, children }: { to: string; children: React.ReactNode }) {
131 return (
132 <NavLink
133 to={to}
134 className={({ isActive }) =>
135 `rounded-md px-2.5 py-1.5 text-sm transition-colors hover:bg-raised hover:text-fg ${
136 isActive ? "text-fg" : "text-muted"
137 }`
138 }
139 >
140 {children}
141 </NavLink>
142 );
143}
144
145function Header({ user }: { user: User | null | undefined }) {
146 const submit = useSubmit();
147 return (
148 <header className="sticky top-0 z-40 border-b border-line bg-surface/85 backdrop-blur">
149 <div className="mx-auto flex h-14 max-w-6xl items-center gap-2 px-4">
150 <Link to="/" aria-label="g1t home" className="mr-2">
151 <Logo />
152 </Link>
153 <Form action="/search" role="search" className="relative hidden grow sm:block sm:max-w-xs">
154 <Search
155 size={14}
156 className="pointer-events-none absolute top-1/2 left-3 -translate-y-1/2 text-faint"
157 />
158 <input
159 name="q"
160 {...notACredential()}
161 placeholder="Search repositories"
162 aria-label="Search repositories"
163 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"
164 />
165 </Form>
166 <nav className="flex items-center gap-0.5">
167 <HeaderLink to="/explore">Explore</HeaderLink>
168 <HeaderLink to="https://docs.g1t.sh/">Docs</HeaderLink>
169 </nav>
170 <div className="ml-auto flex items-center gap-2">
171 {user ? (
172 <>
173 <Link
174 to="/new"
175 aria-label="New project"
176 className="rounded-md border border-line p-1.5 text-muted transition-colors hover:border-line-strong hover:text-fg"
177 >
178 <Plus size={16} />
179 </Link>
180 <DropdownMenu>
181 <DropdownMenuTrigger
182 aria-label="Account menu"
183 className="flex items-center gap-1.5 rounded-md p-1 outline-none transition-colors hover:bg-raised data-[state=open]:bg-raised"
184 >
185 <Avatar name={user.username} size={24} />
186 <ChevronDown size={14} className="text-faint" />
187 </DropdownMenuTrigger>
188 <DropdownMenuContent align="end" className="w-56">
189 <DropdownMenuLabel>
190 Signed in as{" "}
191 <span className="font-mono font-medium text-fg">
192 {user.username}
193 </span>
194 </DropdownMenuLabel>
195 <DropdownMenuSeparator />
196 {(user.workspaces ?? []).map((membership) => (
197 <DropdownMenuItem asChild key={membership.slug}>
198 <Link to={`/${membership.slug}`}>
199 <Avatar name={membership.slug} size={16} square />
200 {membership.slug}
201 </Link>
202 </DropdownMenuItem>
203 ))}
204 <DropdownMenuItem asChild>
205 <Link to="/workspaces/new">
206 <Plus />
207 New workspace
208 </Link>
209 </DropdownMenuItem>
210 <DropdownMenuSeparator />
211 <DropdownMenuItem asChild>
212 <Link to="/">
213 <LayoutDashboard />
214 Mission control
215 </Link>
216 </DropdownMenuItem>
217 <DropdownMenuItem asChild>
218 <Link to="/new">
219 <Plus />
220 New project
221 </Link>
222 </DropdownMenuItem>
223 <DropdownMenuSeparator />
224 <DropdownMenuItem asChild>
225 <Link to="/settings">
226 <Settings />
227 Settings
228 </Link>
229 </DropdownMenuItem>
230 <DropdownMenuItem asChild>
231 <Link to="https://docs.g1t.sh/quickstart/">
232 <BookOpen />
233 Documentation
234 </Link>
235 </DropdownMenuItem>
236 <DropdownMenuSeparator />
237 {/* Submitted from here: the menu closes on select, and a button
238 that has left the page cannot submit a form. */}
239 <DropdownMenuItem
240 onSelect={() => submit(null, { method: "post", action: "/logout" })}
241 >
242 <LogOut />
243 Sign out
244 </DropdownMenuItem>
245 </DropdownMenuContent>
246 </DropdownMenu>
247 </>
248 ) : (
249 <>
250 <HeaderLink to="/login">Sign in</HeaderLink>
251 <ButtonLink to="/register">Sign up</ButtonLink>
252 </>
253 )}
254 </div>
255 </div>
256 </header>
257 );
258}
259
260const FOOTER_LINKS: { title: string; links: [string, string][] }[] = [
261 {
262 title: "Product",
263 links: [
264 ["Explore repositories", "/explore"],
265 ["g1t agents", "https://docs.g1t.sh/guides/g1t-agents/"],
266 ["Bring your own agent", "https://docs.g1t.sh/guides/bring-your-own-agent/"],
267 ["Integrations", "https://docs.g1t.sh/guides/integrations/"],
268 ["Model providers", "https://docs.g1t.sh/guides/models/"],
269 ["Sign up", "/register"],
270 ],
271 },
272 {
273 title: "Developers",
274 links: [
275 ["Quickstart", "https://docs.g1t.sh/quickstart/"],
276 ["How g1t works", "https://docs.g1t.sh/concepts/overview/"],
277 ["API reference", "https://docs.g1t.sh/reference/api/"],
278 ["OpenAPI", "https://api.g1t.sh/openapi.json"],
279 ["llms.txt", "/llms.txt"],
280 ],
281 },
282 {
283 title: "Project",
284 links: [
285 ["Source on g1t", "/syntaqx/g1t"],
286 ["Source on GitHub", "https://github.com/syntaqx/g1t"],
287 ["MIT license", "/syntaqx/g1t/blob/main/LICENSE"],
288 ],
289 },
290];
291
292function Footer() {
293 return (
294 <footer className="mt-24 border-t border-line">
295 <div className="mx-auto flex max-w-6xl flex-wrap gap-x-20 gap-y-8 px-4 py-12">
296 <div className="grow">
297 <Logo />
298 <p className="mt-3 max-w-xs text-sm text-muted">
299 A git forge for agents. Open source under the MIT license, built on
300 Cloudflare Workers and Artifacts.
301 </p>
302 </div>
303 {FOOTER_LINKS.map((group) => (
304 <div key={group.title}>
305 <p className="text-sm font-medium">{group.title}</p>
306 <ul className="mt-3 space-y-2 text-sm text-muted">
307 {group.links.map(([label, to]) => (
308 <li key={to}>
309 {/* Plain links: some targets are files or other hosts. */}
310 <a href={to} className="hover:text-fg">
311 {label}
312 </a>
313 </li>
314 ))}
315 </ul>
316 </div>
317 ))}
318 </div>
319 <div className="border-t border-line">
320 <p className="mx-auto max-w-6xl px-4 py-5 text-xs text-faint">
321 g1t is open source software, built on Cloudflare Workers and
322 Artifacts.
323 </p>
324 </div>
325 </footer>
326 );
327}
328
329export function Layout({ children }: { children: React.ReactNode }) {
330 // Undefined when the root loader itself failed.
331 const root = useRouteLoaderData<typeof loader>("root");
332 const user = root?.user;
333 const banner = user && !user.verified && (
334 <Form
335 method="post"
336 action="/verify"
337 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"
338 >
339 <span>
340 Confirm your email address to create repositories and push. We
341 sent you a link.
342 </span>
343 <button type="submit" className="font-medium underline underline-offset-4">
344 Send it again
345 </button>
346 </Form>
347 );
348 return (
349 <html lang="en">
350 <head>
351 <meta charSet="utf-8" />
352 <meta name="viewport" content="width=device-width, initial-scale=1" />
353 <meta name="theme-color" content="#0f0f11" />
354 <Meta />
355 <Links />
356 </head>
357 <body className="flex min-h-screen flex-col">
358 {user && root?.shell ? (
359 <AppShell user={user} shell={root.shell} banner={banner}>
360 {children}
361 </AppShell>
362 ) : (
363 <>
364 <Progress />
365 <Header user={user} />
366 {banner}
367 <div className="grow">{children}</div>
368 <Footer />
369 </>
370 )}
371 <ScrollRestoration />
372 <Scripts />
373 </body>
374 </html>
375 );
376}
377
378export default function App() {
379 return <Outlet />;
380}
381
382export function ErrorBoundary({ error }: Route.ErrorBoundaryProps) {
383 let title = "Something went wrong";
384 let details = "An unexpected error occurred. Try again in a moment.";
385 let stack: string | undefined;
386
387 if (isRouteErrorResponse(error)) {
388 if (error.status === 404) {
389 title = "Page not found";
390 details = "There is nothing at this address, or you do not have access to it.";
391 } else if (typeof error.data === "string" && error.data) {
392 details = error.data;
393 }
394 } else if (import.meta.env.DEV && error instanceof Error) {
395 details = error.message;
396 stack = error.stack;
397 }
398
399 return (
400 <main className="mx-auto max-w-xl px-4 py-32 text-center">
401 <h1 className="text-2xl font-semibold tracking-tight">{title}</h1>
402 <p className="mt-3 text-muted">{details}</p>
403 <div className="mt-8">
404 <ButtonLink to="/" variant="quiet">
405 Back to home
406 </ButtonLink>
407 </div>
408 {stack && (
409 <pre className="mt-8 overflow-x-auto rounded-lg border border-line p-4 text-left text-xs">
410 <code>{stack}</code>
411 </pre>
412 )}
413 </main>
414 );
415}