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