Skip to content

g1t/apps/web/app/routes/workspace/layout.tsx

199 lines9,529 bytesCodeBlame
1import { Package, Plus } from "lucide-react";
2import { Outlet, data, redirect, useLocation, useNavigation } from "react-router";
3
4import type { Route } from "./+types/layout";
5import { page } from "../../lib/meta";
6import { Avatar, ButtonLink, Pill } from "../../components/ui";
7import { WelcomeBanner } from "../../components/welcome";
8import { clearWelcome, welcomes } from "../../lib/invites";
9import { notFound } from "../../lib/not-found.server";
10import { redirectIfRenamed } from "../../lib/renamed.server";
11import { rememberWorkspace } from "../../lib/workspace-choice";
12import { workspacePage, workspaceRedirect } from "../../lib/workspace-nav";
13import { WorkspacePageSkeleton } from "../../components/workspace-skeletons";
14import { identity } from "../../lib/services.server";
15import { getViewer, roleIn } from "../../lib/session.server";
16
17export function meta({ loaderData, params, ...args }: Route.MetaArgs) {
18 return page(args, { title: `${loaderData?.workspace.name ?? params.owner} · g1t` });
19}
20
21export async function loader({ params, context, request }: Route.LoaderArgs) {
22 const url = new URL(request.url);
23 // `?tab=projects` and the like, from the tabs this page once had: that
24 // page's own address.
25 const moved = url.search ? workspaceRedirect(url.pathname, url.search) : null;
26 if (moved) throw redirect(moved);
27 const viewer = getViewer(context);
28 const workspace = await identity.getWorkspace(params.owner);
29 if (!workspace) {
30 // A workspace's old name, after a rename: its pages are at the new one.
31 await redirectIfRenamed(request, params.owner);
32 throw notFound("workspace");
33 }
34 const role = roleIn(viewer, workspace.slug);
35 // Opening one of your workspaces makes it the one you are in.
36 if (!role) return { workspace, role, welcome: false };
37 const secure = new URL(request.url).protocol === "https:";
38 const headers = new Headers({ "Set-Cookie": rememberWorkspace(workspace.slug, secure) });
39 // Just joined with an invite: welcomed once (routes/invite.tsx).
40 const welcome = welcomes(request.headers.get("cookie"), workspace.slug);
41 if (welcome) headers.append("Set-Cookie", clearWelcome(secure));
42 return data({ workspace, role, welcome }, { headers });
43}
44
45/** A workspace's own pages, each with its title and what it is for. */
46const PAGES: Record<string, { title: string; about: string }> = {
47 settings: { title: "General", about: "The workspace's name, icon, address and description, who can create teams, and deleting it." },
48 people: {
49 title: "People",
50 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.",
51 },
52 projects: {
53 title: "Projects",
54 about: "Everything the workspace builds and runs. Search, filter and sort them, and pin the ones you use most to keep them in your sidebar.",
55 },
56 teams: {
57 title: "Teams",
58 about: "Groups of members, given roles on repositories together, mentioned as @workspace/team and asked to review together. Child teams inherit their parent's access.",
59 },
60 repositories: {
61 title: "Repositories",
62 about: "Every repository in the workspace: who can see it, whether it is archived, and the ones deleted recently.",
63 },
64 tokens: {
65 title: "Access tokens",
66 about: "Tokens that belong to the workspace, not a person: for CI, integrations and agents that work for the whole team.",
67 },
68 packages: {
69 title: "Packages",
70 about: "What the workspace publishes and installs, beside its code: container images, npm, Composer, Go, Cargo, Maven, NuGet and RubyGems, with the same members, roles and tokens.",
71 },
72 usage: { title: "Usage", about: "What the workspace's agents cost, run by run, by repository, pull request and model." },
73 billing: { title: "Billing and plans", about: "The g1t plan, the trial, your spend limit and caps, prepaying, and every charge." },
74 agents: {
75 title: "Agent fleet",
76 about: "Every agent at work across the workspace's projects: what each holds, what it is doing now, and what it has cost.",
77 },
78 memory: {
79 title: "Workspace memory",
80 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.",
81 },
82 context: {
83 title: "Context",
84 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.",
85 },
86 audit: {
87 title: "Audit log",
88 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.",
89 },
90 guardrails: {
91 title: "Guardrails",
92 about: "What agents may reach, run, spend and take in every project's sandboxes. Each project can override these under its settings.",
93 },
94 webhooks: {
95 title: "Webhooks",
96 about: "Every repository's events, sent to your own addresses as they happen. A repository can also have its own, under its settings.",
97 },
98 secrets: {
99 title: "Secrets and variables",
100 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.",
101 },
102 runners: {
103 title: "Self-hosted runners",
104 about: "Your own machines, which run workflow jobs, and if you choose agents' work, for free. They connect out to g1t; nothing reaches in.",
105 },
106 security: {
107 title: "Security",
108 about: "Open alerts in every project: secrets found in pushes and history, and vulnerable dependencies. Each project's Security page has the details and the security update g1t opened for each.",
109 },
110 integrations: {
111 title: "Integrations",
112 about: "Model providers, alerts and trackers. Secrets are sealed when saved, and agents never see them.",
113 },
114};
115
116/** A workspace page's heading: its title and what it is for. */
117function PageHeader({ title, about }: { title: string; about: string }) {
118 return (
119 <header className="mb-8 border-b border-line pb-6">
120 <h1 className="text-2xl font-semibold tracking-tight">{title}</h1>
121 <p className="mt-1.5 text-sm text-muted">{about}</p>
122 </header>
123 );
124}
125
126export default function WorkspaceLayout({ loaderData }: Route.ComponentProps) {
127 const { workspace, role, welcome } = loaderData;
128 const { pathname } = useLocation();
129 const going = useNavigation().location;
130 // The workspace's own page is its overview. Its other pages are found
131 // from the sidebar, which lights the one you are on, and the top bar's
132 // trail leads back here; each has a heading of its own.
133 const here = workspacePage(pathname, workspace.slug);
134 // On the way to another of its pages: that page's heading and shape
135 // until it arrives.
136 const next = going && going.pathname !== pathname ? workspacePage(going.pathname, workspace.slug) : null;
137 const shown = next ?? here;
138 const parts = (pathname.split("/-/")[1] ?? "").split("/").filter(Boolean);
139 if (shown !== "overview") {
140 const key = shown ?? parts[0] ?? "";
141 const heading = PAGES[key];
142 // A page within one (a single package or team) and a page that is
143 // coming (Insights) have headings of their own.
144 const titled = heading && (shown != null || parts.length === 1);
145 return (
146 <div className="mx-auto max-w-5xl px-4 py-10 sm:px-8">
147 {titled && <PageHeader title={heading.title} about={heading.about} />}
148 {next ? <WorkspacePageSkeleton page={next} /> : <Outlet />}
149 </div>
150 );
151 }
152 return (
153 <>
154 {/* The workspace's own header band, above its overview. */}
155 <div className="border-b border-line bg-surface/60">
156 <div className="mx-auto max-w-6xl px-4 py-8">
157 {welcome && (
158 <div className="mb-6">
159 <WelcomeBanner title={`You're in ${workspace.name}`}>
160 You joined as a {role}. Its projects, issues and agents are all here.
161 </WelcomeBanner>
162 </div>
163 )}
164 <div className="flex flex-wrap items-center gap-4">
165 <Avatar name={workspace.slug} image={workspace.avatar} size={52} square />
166 <div className="min-w-0 grow">
167 <div className="flex flex-wrap items-center gap-2.5">
168 <h1 className="truncate text-2xl font-semibold tracking-tight">
169 {workspace.name}
170 </h1>
171 {role && <Pill>{role}</Pill>}
172 </div>
173 <p className="font-mono text-sm text-muted">g1t.sh/{workspace.slug}</p>
174 </div>
175 {role ? (
176 <ButtonLink to={`/new?workspace=${workspace.slug}`}>
177 <Plus size={15} />
178 New project
179 </ButtonLink>
180 ) : (
181 // A visitor's sidebar is not this workspace's: its packages
182 // are a link here, as its projects are below.
183 <ButtonLink to={`/${workspace.slug}/-/packages`} variant="quiet" prefetch="intent">
184 <Package size={15} />
185 Packages
186 </ButtonLink>
187 )}
188 </div>
189 {workspace.description && (
190 <p className="mt-4 max-w-2xl text-sm text-muted">{workspace.description}</p>
191 )}
192 </div>
193 </div>
194 <div className="mx-auto max-w-6xl px-4 py-8">
195 {next ? <WorkspacePageSkeleton page="overview" /> : <Outlet />}
196 </div>
197 </>
198 );
199}