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