Skip to content

Where people and agents ship software together. The open-source git platform for the whole job: issues, agents, checks and deploys to the edge.

444 linesCodeBlameRaw
1/**
2 * A workspace's pages and where the sidebar finds them. `g1t.sh/<workspace>`
3 * is the workspace's own page, its header and overview; Projects, Packages,
4 * Teams, People, Insights and Settings are pages of their own under `-/`,
5 * each reached from the sidebar and each with its own heading. The sidebar
6 * is always the current workspace's.
7 */
8
9/** The workspace's pages a person moves between, and which the sidebar lists. */
10export type WorkspacePageKey = "overview" | "projects" | "packages" | "teams" | "people" | "insights";
11
12/** Those pages under `-/`: the overview is the workspace's own address. */
13export const WORKSPACE_PAGES = ["projects", "packages", "teams", "people", "insights"] as const;
14
15/**
16 * Which of those pages a path is, under `/<workspace>`: null for the
17 * workspace's other pages (settings, Usage and the rest), for
18 * one package's or one team's page, and for anything else.
19 */
20export function workspacePage(pathname: string, slug: string): WorkspacePageKey | null {
21 const parts = pagePath(pathname).split("/").filter(Boolean);
22 if (parts[0]?.toLowerCase() !== slug.toLowerCase()) return null;
23 if (parts.length === 1) return "overview";
24 if (parts[1] !== "-" || parts.length !== 3) return null;
25 return (WORKSPACE_PAGES as readonly string[]).includes(parts[2]!) ? (parts[2] as WorkspacePageKey) : null;
26}
27
28/**
29 * Workspace mode's pages, in the groups its sidebar lists them under: the
30 * workspace's settings area, every page one click away. `owner` marks the
31 * pages only owners open; the sidebar shows members the rest.
32 * `path` is under `-/`.
33 */
34export type WorkspaceSettingsPage = { key: string; label: string; path: string; owner?: boolean };
35
36export type WorkspaceSettingsGroup = { key: string; title: string; pages: WorkspaceSettingsPage[] };
37
38export const WORKSPACE_SETTINGS_GROUPS: readonly WorkspaceSettingsGroup[] = [
39 {
40 key: "general",
41 title: "General",
42 pages: [{ key: "settings", label: "General", path: "settings", owner: true }],
43 },
44 {
45 key: "ai",
46 title: "AI",
47 pages: [{ key: "ai", label: "Models", path: "ai" }],
48 },
49 {
50 key: "access",
51 title: "Access",
52 pages: [
53 { key: "members", label: "Members", path: "members" },
54 { key: "invitations", label: "Invitations", path: "members/invitations", owner: true },
55 { key: "permissions", label: "Permissions", path: "permissions", owner: true },
56 { key: "tokens", label: "Access tokens", path: "tokens" },
57 { key: "personal-access-tokens", label: "Personal access tokens", path: "personal-access-tokens", owner: true },
58 ],
59 },
60 {
61 key: "money",
62 title: "Money",
63 pages: [
64 { key: "usage", label: "Usage", path: "usage" },
65 { key: "billing", label: "Billing and plans", path: "billing" },
66 ],
67 },
68 {
69 key: "compute",
70 title: "Compute",
71 pages: [{ key: "runners", label: "Runners", path: "runners", owner: true }],
72 },
73 {
74 key: "code",
75 title: "Code",
76 pages: [
77 { key: "repositories", label: "Repositories", path: "repositories" },
78 { key: "actions", label: "Actions", path: "actions" },
79 { key: "secrets", label: "Secrets and variables", path: "secrets" },
80 { key: "webhooks", label: "Webhooks", path: "webhooks" },
81 ],
82 },
83 {
84 key: "chat",
85 title: "Chat",
86 pages: [
87 { key: "chat-settings", label: "Chat settings", path: "settings/chat" },
88 { key: "emoji", label: "Emoji", path: "emoji" },
89 ],
90 },
91 {
92 key: "security",
93 title: "Security",
94 pages: [
95 { key: "security-settings", label: "Security settings", path: "security/settings" },
96 { key: "guardrails", label: "Guardrails", path: "guardrails" },
97 { key: "rules", label: "Rules", path: "rules" },
98 { key: "audit", label: "Audit log", path: "audit" },
99 ],
100 },
101 {
102 key: "integrations",
103 title: "Integrations",
104 pages: [{ key: "integrations", label: "Integrations", path: "integrations" }],
105 },
106];
107
108/**
109 * Which of Workspace mode's pages `pathname` is, under the workspace
110 * `slug`, as a group key and a page key; null for the overview and for
111 * anything else. A page's own subpages (one ruleset, a receipt) count as
112 * the page; the deepest match wins, so Invitations is its own page and
113 * not a subpage of Members.
114 */
115export function workspaceSettingsPage(pathname: string, slug: string): { group: string; page: string } | null {
116 const parts = pagePath(pathname).split("/").filter(Boolean);
117 if (parts[0]?.toLowerCase() !== slug.toLowerCase() || parts[1] !== "-") return null;
118 const rest = parts.slice(2).join("/");
119 let found: { group: string; page: string; depth: number } | null = null;
120 for (const group of WORKSPACE_SETTINGS_GROUPS) {
121 for (const page of group.pages) {
122 if (rest === page.path || rest.startsWith(`${page.path}/`)) {
123 const depth = page.path.split("/").length;
124 if (!found || depth > found.depth) found = { group: group.key, page: page.key, depth };
125 }
126 }
127 }
128 return found ? { group: found.group, page: found.page } : null;
129}
130
131/** `?tab=` as people write it, from the tabs the workspace page once had: the page it means. */
132const TAB_WORDS: Record<string, string> = {
133 overview: "",
134 projects: "-/projects",
135 repositories: "-/projects",
136 repos: "-/projects",
137 packages: "-/packages",
138 people: "-/people",
139 members: "-/members",
140 teams: "-/teams",
141 insights: "-/insights",
142 settings: "-/settings",
143};
144
145/** Workspace pages that moved, by their old name under `-/`; a new address may carry a query of its own. */
146const MOVED: Record<string, string> = {
147 // Home was called Today for a while.
148 today: "-/home",
149 "soon/teams": "-/teams",
150 "soon/insights": "-/insights",
151};
152
153/** Where the page `rest` (under `-/`) is now, or null when it has not moved. */
154function movedTo(rest: string): string | null {
155 return MOVED[rest] ?? null;
156}
157
158/** `to` with `search` merged into any query of its own, as a path under the workspace. */
159function withQuery(to: string, search: string): string {
160 const [path = "", own = ""] = to.split("?", 2);
161 const params = new URLSearchParams(search);
162 for (const [key, value] of new URLSearchParams(own)) params.set(key, value);
163 const query = params.toString();
164 return `${path ? `/${path}` : ""}${query ? `?${query}` : ""}`;
165}
166
167/** The page a request is for: a click's data request ends in `.data`. */
168export function pagePath(pathname: string): string {
169 return pathname.replace(/\.data$/, "").replace(/\/+$/, "") || "/";
170}
171
172/**
173 * The page at `pathname` under the workspace `slug` instead of its first
174 * segment, keeping its query: where an old name or an alias leads. A click
175 * asks for the page's data at `<path>.data?_routes=…`; the address given is
176 * the page's own, which the browser then asks for as usual.
177 */
178export function underWorkspace(pathname: string, search: string, slug: string): string {
179 const params = new URLSearchParams(search);
180 params.delete("_routes");
181 const query = params.toString();
182 const segments = pagePath(pathname).split("/");
183 // segments[0] is the empty string before the leading slash.
184 segments[1] = slug;
185 return `${segments.join("/")}${query ? `?${query}` : ""}`;
186}
187
188/**
189 * Where an old address of a workspace's pages is now, keeping its query;
190 * null when it has not moved. `/<workspace>?tab=projects` and the like
191 * open that page.
192 */
193export function workspaceRedirect(pathname: string, search = ""): string | null {
194 // A click asks for the page's data at `<path>.data?_routes=…`; the page
195 // is the same one a full load asks for.
196 const params = new URLSearchParams(search);
197 params.delete("_routes");
198 search = params.toString();
199 const parts = pagePath(pathname).split("/").filter(Boolean);
200 const slug = parts[0];
201 if (!slug) return null;
202 if (parts.length === 1 && params.has("tab")) {
203 const to = TAB_WORDS[(params.get("tab") ?? "").toLowerCase()];
204 if (to === undefined) return null;
205 params.delete("tab");
206 const rest = params.toString();
207 return `/${slug}${to ? `/${to}` : ""}${rest ? `?${rest}` : ""}`;
208 }
209 if (parts[1] !== "-") return null;
210 const to = movedTo(parts.slice(2).join("/"));
211 if (to === null) return null;
212 return `/${slug}${withQuery(to, search)}`;
213}
214
215/**
216 * The rail's modes. Home is the front page;
217 * Chat, Code and Artifacts are where work happens; Notifications
218 * spans them; People is who belongs; Workspace is the workspace itself,
219 * its settings area: who has access, money, policies and how each part is
220 * set up; Apps is everything installed that you
221 * can use. `account` is your own settings, under the avatar. `site` is
222 * g1t's own pages (policies, support, a new workspace): no workspace's, so
223 * no mode is lit and no mode's sidebar sits beside them. `browse` is g1t
224 * beyond your workspaces (Explore, search, a profile, another workspace
225 * and its public projects): the rail lights Explore, and Explore's sidebar
226 * sits beside them (`browsingOf`). Each mode has a sidebar of its own, or
227 * none, and which one is lit follows the address.
228 */
229export type ModeKey =
230 | "home"
231 | "chat"
232 | "notifications"
233 | "code"
234 | "artifacts"
235 | "routines"
236 | "people"
237 | "workspace"
238 | "apps"
239 | "account"
240 | "site"
241 | "browse";
242
243/** Workspace pages under `-/`, by the mode they belong to. Anything else of the workspace's is Code's. */
244const PAGE_MODES: Record<string, ModeKey> = {
245 home: "home",
246 chat: "chat",
247 artifacts: "artifacts",
248 routines: "routines",
249 apps: "apps",
250 people: "people",
251 members: "workspace",
252 permissions: "workspace",
253 teams: "people",
254 workspace: "workspace",
255 usage: "workspace",
256 billing: "workspace",
257 integrations: "workspace",
258 ai: "workspace",
259 guardrails: "workspace",
260 rules: "workspace",
261 audit: "workspace",
262 settings: "workspace",
263 repositories: "workspace",
264 tokens: "workspace",
265 "personal-access-tokens": "workspace",
266 secrets: "workspace",
267 actions: "workspace",
268 runners: "workspace",
269 webhooks: "workspace",
270 emoji: "workspace",
271};
272
273/** First segments that are g1t's own pages, never a workspace: none is any mode's. */
274const SITE_PAGES = new Set(["explore", "search", "support", "policies", "security", "status", "u", "invite", "workspaces"]);
275
276/**
277 * The mode a path is in, for the workspace `slug`. A repository (anyone's)
278 * and a new project are Code's; g1t's own pages (Explore, search, a
279 * profile) are `site`: public, and no workspace's mode.
280 */
281export function modeOf(pathname: string, slug: string | null): ModeKey {
282 const path = pagePath(pathname);
283 if (path === "/") return "home";
284 if (path === "/notifications" || path.startsWith("/notifications/")) return "notifications";
285 if (path === "/settings" || path.startsWith("/settings/")) return "account";
286 const parts = path.split("/").filter(Boolean);
287 if (SITE_PAGES.has(parts[0] ?? "")) return "site";
288 if (slug && parts[0]?.toLowerCase() === slug.toLowerCase()) {
289 if (parts.length === 1) return "home";
290 if (parts[1] === "-") {
291 // The workspace's security settings are a policy; its alerts are Code's.
292 if (parts[2] === "security" && parts[3] === "settings") return "workspace";
293 return PAGE_MODES[parts[2] ?? ""] ?? "code";
294 }
295 }
296 // Another workspace's chat, where the viewer is a guest: Chat all the same.
297 if (parts[1] === "-" && parts[2] === "chat") return "chat";
298 return "code";
299}
300
301/** Where each mode's button in the rail goes, in the workspace `slug`. */
302export function modeHome(mode: ModeKey, slug: string): string {
303 switch (mode) {
304 case "home":
305 return homePagePath(slug);
306 case "notifications":
307 return "/notifications";
308 case "chat":
309 return `/${slug}/-/chat`;
310 case "artifacts":
311 return `/${slug}/-/artifacts`;
312 case "routines":
313 return `/${slug}/-/routines`;
314 case "code":
315 return `/${slug}/-/overview`;
316 case "people":
317 return `/${slug}/-/people`;
318 case "workspace":
319 return `/${slug}/-/workspace`;
320 case "apps":
321 return `/${slug}/-/apps`;
322 case "account":
323 return "/settings";
324 case "site":
325 case "browse":
326 return "/explore";
327 }
328}
329
330/** First segments that are g1t's own pages and never anyone's name (packages/contracts names.ts). */
331const NOT_AN_OWNER = new Set([
332 "-", "api", "mcp", "login", "logout", "register", "new", "settings", "search", "admin", "auth", "integrations",
333 "pulls", "issues", "verify", "confirm-email", "forgot", "reset", "device", "workspaces", "u", "oauth", "assets",
334 "avatars", "docs", "explore", "about", "pricing", "policies", "security", "support", "status", "terms", "privacy",
335 "help", "blog", "invite", "invites", "waitlist", "invitations", "v2", "inbox", "notifications", "download",
336 "downloads", "install",
337]);
338
339/**
340 * Whose g1t a browsed page is: a workspace (its overview, its pages, or
341 * one of its projects, `repo`) or a person (their profile).
342 */
343export type BrowsedOwner = { kind: "workspace"; name: string; repo: string | null } | { kind: "person"; name: string };
344
345/** What the shell knows of the viewer, to tell their own pages from the rest of g1t. */
346export type Viewer = {
347 /** The workspace the shell is about. */
348 slug: string | null;
349 /** Every workspace they belong to, by slug. */
350 memberships: readonly string[];
351 /** Repositories shared with them in workspaces they do not belong to: Code lists them as theirs. */
352 shared?: readonly { namespace: string; name: string }[];
353 /** Their own username: their profile is theirs, not someone's being browsed. */
354 username?: string | null;
355};
356
357/**
358 * Whether `pathname` is g1t beyond the viewer's own workspaces, and whose
359 * (`owner`, null for Explore, search and their own profile). Null for
360 * anything of theirs. A workspace they belong to is never browsed, even
361 * when it is not the one they are in: its pages and projects switch the
362 * shell to it (lib/workspace-choice.ts `workspaceFor`), so they are that
363 * workspace's modes. A project shared with them is Code's, whose sidebar
364 * lists it. Without a workspace there is no rail, and nothing is browsed.
365 */
366export function browsingOf(pathname: string, viewer: Viewer): { owner: BrowsedOwner | null } | null {
367 if (!viewer.slug) return null;
368 const parts = pagePath(pathname).split("/").filter(Boolean);
369 const first = parts[0]?.toLowerCase();
370 if (!first) return null;
371 if (first === "explore" || first === "search") return { owner: null };
372 if (first === "u") {
373 const name = parts[1];
374 if (!name) return null;
375 const own = viewer.username != null && name.toLowerCase() === viewer.username.toLowerCase();
376 return { owner: own ? null : { kind: "person", name } };
377 }
378 // g1t's own pages, and files such as robots.txt.
379 if (NOT_AN_OWNER.has(first) || first.includes(".")) return null;
380 if (first === viewer.slug.toLowerCase() || viewer.memberships.some((slug) => slug.toLowerCase() === first)) return null;
381 // Another workspace's channels, as a guest in them, are Chat's (`modeOf`), not Explore's.
382 if (parts[1] === "-" && parts[2] === "chat") return null;
383 const repo = parts[1] && parts[1] !== "-" ? parts[1] : null;
384 if (repo && (viewer.shared ?? []).some((r) => r.namespace.toLowerCase() === first && r.name.toLowerCase() === repo.toLowerCase())) return null;
385 return { owner: { kind: "workspace", name: parts[0]!, repo } };
386}
387
388/** The mode a path is in for this viewer: `browse` beyond their workspaces (`browsingOf`), else `modeOf`'s. */
389export function shellMode(pathname: string, viewer: Viewer): ModeKey {
390 return browsingOf(pathname, viewer) ? "browse" : modeOf(pathname, viewer.slug);
391}
392
393/** A workspace's front page: Home. */
394export function homePagePath(slug: string): string {
395 return `/${slug}/-/home`;
396}
397
398/**
399 * Where g1t's front door (`/`) and a workspace's own address lead someone
400 * in the workspace `slug`: Home, or Code's Overview when the query asks
401 * for its panels (a tab or a sort).
402 */
403export function homePath(slug: string, search = ""): string {
404 const params = new URLSearchParams(search);
405 params.delete("_routes");
406 params.delete("index");
407 const query = params.toString();
408 if (params.has("tab") || params.has("sort")) return `/${slug}/-/overview?${query}`;
409 return homePagePath(slug);
410}
411
412/** The page a member without Code access sees in place of anything of Code's. */
413export function codeAccessPath(slug: string, from?: string): string {
414 return `/${slug}/-/code-access${from ? `?from=${encodeURIComponent(from)}` : ""}`;
415}
416
417/** The workspace pages under `-/` that are Code's: closed to a member without Code access. */
418const CODE_PAGES = new Set(["overview", "projects", "repositories", "packages", "security", "rules", "runners", "actions", "insights", "soon"]);
419
420/**
421 * Where a member without Code access goes instead of `pathname`, or null
422 * when the page is open to them. `noCode` is the workspaces (by slug) where
423 * they lack it; `chosen` is the one they are in. g1t's front door (`/`) is
424 * Home in that workspace, and so is the workspace's own address; its
425 * Code pages and every repository page is the page that says to ask an
426 * owner.
427 */
428export function codeGate(pathname: string, search: string, noCode: readonly string[], chosen: string | null): string | null {
429 if (noCode.length === 0) return null;
430 const path = pagePath(pathname);
431 if (path === "/") {
432 const slug = chosen && noCode.includes(chosen.toLowerCase()) ? chosen.toLowerCase() : null;
433 return slug ? homePagePath(slug) : null;
434 }
435 const parts = path.split("/").filter(Boolean);
436 const slug = parts[0]?.toLowerCase();
437 if (!slug || !noCode.includes(slug)) return null;
438 const from = `${path}${search && search !== "?" ? search : ""}`;
439 // The workspace's own page is Home.
440 if (parts.length === 1) return homePagePath(slug);
441 if (parts[1] === "-") return CODE_PAGES.has(parts[2] ?? "") ? codeAccessPath(slug, from) : null;
442 // A repository, and everything in it.
443 return codeAccessPath(slug, from);
444}