| 1 | import { Plus } from "lucide-react"; |
| 2 | import { Outlet, data, useLocation } from "react-router"; |
| 3 | |
| 4 | import type { Route } from "./+types/layout"; |
| 5 | import { page } from "../../lib/meta"; |
| 6 | import { Avatar, ButtonLink, Pill } from "../../components/ui"; |
| 7 | import { WelcomeBanner } from "../../components/welcome"; |
| 8 | import { clearWelcome, welcomes } from "../../lib/invites"; |
| 9 | import { notFound } from "../../lib/not-found.server"; |
| 10 | import { redirectIfRenamed } from "../../lib/renamed.server"; |
| 11 | import { rememberWorkspace } from "../../lib/workspace-choice"; |
| 12 | import { identity } from "../../lib/services.server"; |
| 13 | import { getViewer, roleIn } from "../../lib/session.server"; |
| 14 | |
| 15 | export function meta({ loaderData, params, ...args }: Route.MetaArgs) { |
| 16 | return page(args, { title: `${loaderData?.workspace.name ?? params.owner} · g1t` }); |
| 17 | } |
| 18 | |
| 19 | export async function loader({ params, context, request }: Route.LoaderArgs) { |
| 20 | const workspace = await identity.getWorkspace(params.owner); |
| 21 | if (!workspace) { |
| 22 | // A workspace's old name, after a rename: its pages are at the new one. |
| 23 | await redirectIfRenamed(request, params.owner); |
| 24 | throw notFound("workspace"); |
| 25 | } |
| 26 | const role = roleIn(getViewer(context), workspace.slug); |
| 27 | // Opening one of your workspaces makes it the one you are in. |
| 28 | if (!role) return { workspace, role, welcome: false }; |
| 29 | const secure = new URL(request.url).protocol === "https:"; |
| 30 | const headers = new Headers({ "Set-Cookie": rememberWorkspace(workspace.slug, secure) }); |
| 31 | // Just joined with an invite: welcomed once (routes/invite.tsx). |
| 32 | const welcome = welcomes(request.headers.get("cookie"), workspace.slug); |
| 33 | if (welcome) headers.append("Set-Cookie", clearWelcome(secure)); |
| 34 | return data({ workspace, role, welcome }, { headers }); |
| 35 | } |
| 36 | |
| 37 | /** A workspace's own pages, each with its title and what it is for. */ |
| 38 | const PAGES: Record<string, { title: string; about: string }> = { |
| 39 | settings: { title: "General", about: "The workspace's name, icon, address and description, and deleting it." }, |
| 40 | people: { |
| 41 | title: "Members", |
| 42 | about: "Members create repositories and have the base permission on each one. Owners are Admins on every repository, and also manage members, tokens, billing and integrations.", |
| 43 | }, |
| 44 | repositories: { |
| 45 | title: "Repositories", |
| 46 | about: "Every repository in the workspace: who can see it, whether it is archived, and the ones deleted recently.", |
| 47 | }, |
| 48 | tokens: { |
| 49 | title: "Access tokens", |
| 50 | about: "Tokens that belong to the workspace, not a person: for CI, integrations and agents that work for the whole team.", |
| 51 | }, |
| 52 | packages: { |
| 53 | title: "Packages", |
| 54 | about: "What the workspace publishes and installs, beside its code: container images, npm, Composer and Go, with the same members, roles and tokens.", |
| 55 | }, |
| 56 | usage: { title: "Usage", about: "What the workspace's agents cost, run by run, by repository, pull request and model." }, |
| 57 | billing: { title: "Billing and plans", about: "The g1t plan, the trial, your spend limit and caps, prepaying, and every charge." }, |
| 58 | agents: { |
| 59 | title: "Agent fleet", |
| 60 | about: "Every agent at work across the workspace's projects: what each holds, what it is doing now, and what it has cost.", |
| 61 | }, |
| 62 | memory: { |
| 63 | title: "Workspace memory", |
| 64 | about: "What holds across all of the workspace's projects, given to every agent in every one of them, beside each project's own memory.", |
| 65 | }, |
| 66 | context: { |
| 67 | title: "Context", |
| 68 | about: "Everything the workspace builds and runs, and what it knows: a catalog built from its projects, memory with a review queue, one search over all of it, and a scorecard for each project.", |
| 69 | }, |
| 70 | audit: { |
| 71 | title: "Audit log", |
| 72 | about: "Every action agents took with their run credentials, and every change people and tokens made: by whom, on whose behalf, to what, and whether it was allowed.", |
| 73 | }, |
| 74 | guardrails: { |
| 75 | title: "Guardrails", |
| 76 | about: "What agents may reach, run, spend and take in every project's sandboxes. Each project can override these under its settings.", |
| 77 | }, |
| 78 | webhooks: { |
| 79 | title: "Webhooks", |
| 80 | about: "Every repository's events, sent to your own addresses as they happen. A repository can also have its own, under its settings.", |
| 81 | }, |
| 82 | secrets: { |
| 83 | title: "Secrets and variables", |
| 84 | about: "Shared with every repository, or the ones you link: read by workflows, deployments, or both. A repository's own row of the same key wins.", |
| 85 | }, |
| 86 | runners: { |
| 87 | title: "Self-hosted runners", |
| 88 | about: "Your own machines, which run workflow jobs, and if you choose agents' work, for free. They connect out to g1t; nothing reaches in.", |
| 89 | }, |
| 90 | integrations: { |
| 91 | title: "Integrations", |
| 92 | about: "Model providers, alerts and trackers. Secrets are sealed when saved, and agents never see them.", |
| 93 | }, |
| 94 | }; |
| 95 | |
| 96 | export default function WorkspaceLayout({ loaderData }: Route.ComponentProps) { |
| 97 | const { workspace, role, welcome } = loaderData; |
| 98 | // The sidebar finds the workspace's pages, for everyone, so its pages |
| 99 | // need a title, not the workspace's whole header again. |
| 100 | const parts = (useLocation().pathname.split("/-/")[1] ?? "").split("/").filter(Boolean); |
| 101 | const page = PAGES[parts[0] ?? ""]; |
| 102 | // A page within one (a single package) has its own heading. |
| 103 | if (page && parts.length > 1) { |
| 104 | return ( |
| 105 | <div className="mx-auto max-w-5xl px-4 py-10 sm:px-8"> |
| 106 | <Outlet /> |
| 107 | </div> |
| 108 | ); |
| 109 | } |
| 110 | if (page) { |
| 111 | return ( |
| 112 | <div className="mx-auto max-w-5xl px-4 py-10 sm:px-8"> |
| 113 | <header className="mb-8 border-b border-line pb-6"> |
| 114 | <h1 className="text-2xl font-semibold tracking-tight">{page.title}</h1> |
| 115 | <p className="mt-1.5 text-sm text-muted">{page.about}</p> |
| 116 | </header> |
| 117 | <Outlet /> |
| 118 | </div> |
| 119 | ); |
| 120 | } |
| 121 | return ( |
| 122 | <> |
| 123 | {/* The workspace's own header band. */} |
| 124 | <div className="border-b border-line bg-surface/60"> |
| 125 | <div className="mx-auto max-w-6xl px-4 pt-8 pb-8"> |
| 126 | {welcome && ( |
| 127 | <div className="mb-6"> |
| 128 | <WelcomeBanner title={`You're in ${workspace.name}`}> |
| 129 | You joined as a {role}. Its projects, issues and agents are all here. |
| 130 | </WelcomeBanner> |
| 131 | </div> |
| 132 | )} |
| 133 | <div className="flex flex-wrap items-center gap-4"> |
| 134 | <Avatar name={workspace.slug} image={workspace.avatar} size={52} square /> |
| 135 | <div className="min-w-0 grow"> |
| 136 | <div className="flex flex-wrap items-center gap-2.5"> |
| 137 | <h1 className="truncate text-2xl font-semibold tracking-tight"> |
| 138 | {workspace.name} |
| 139 | </h1> |
| 140 | {role && <Pill>{role}</Pill>} |
| 141 | </div> |
| 142 | <p className="font-mono text-sm text-muted">g1t.sh/{workspace.slug}</p> |
| 143 | </div> |
| 144 | {role && ( |
| 145 | <ButtonLink to={`/new?workspace=${workspace.slug}`}> |
| 146 | <Plus size={15} /> |
| 147 | New project |
| 148 | </ButtonLink> |
| 149 | )} |
| 150 | </div> |
| 151 | {workspace.description && ( |
| 152 | <p className="mt-4 max-w-2xl text-sm text-muted">{workspace.description}</p> |
| 153 | )} |
| 154 | </div> |
| 155 | </div> |
| 156 | <div className="mx-auto max-w-6xl px-4 py-8"> |
| 157 | <Outlet /> |
| 158 | </div> |
| 159 | </> |
| 160 | ); |
| 161 | } |