Skip to content
416 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. A page that
32 * is not here yet is a `soon` row: what the group will hold, not a link.
33 * `path` is under `-/`.
34 */
35export type WorkspaceSettingsPage = { key: string; label: string; path: string; owner?: boolean; soon?: string };
36
37export type WorkspaceSettingsGroup = { key: string; title: string; pages: WorkspaceSettingsPage[] };
38
39export const WORKSPACE_SETTINGS_GROUPS: readonly WorkspaceSettingsGroup[] = [
40 {
41 key: "general",
42 title: "General",
43 pages: [{ key: "settings", label: "General", path: "settings", owner: true }],
44 },
45 {
46 key: "access",
47 title: "Access",
48 pages: [
49 { key: "members", label: "Members", path: "members" },
50 { key: "invitations", label: "Invitations", path: "members/invitations", owner: true },
51 { key: "permissions", label: "Permissions", path: "permissions", owner: true },
52 { key: "tokens", label: "Access tokens", path: "tokens" },
53 { key: "personal-access-tokens", label: "Personal access tokens", path: "personal-access-tokens", owner: true },
54 ],
55 },
56 {
57 key: "money",
58 title: "Money",
59 pages: [
60 { key: "spend", label: "Spend", path: "spend" },
61 { key: "usage", label: "Usage", path: "usage" },
62 { key: "gateway", label: "AI Gateway", path: "gateway" },
63 { key: "billing", label: "Billing and plans", path: "billing" },
64 ],
65 },
66 {
67 key: "compute",
68 title: "Compute",
69 pages: [{ key: "runners", label: "Runners", path: "runners", owner: true }],
70 },
71 {
72 key: "code",
73 title: "Code",
74 pages: [
75 { key: "repositories", label: "Repositories", path: "repositories" },
76 { key: "actions", label: "Actions", path: "actions" },
77 { key: "secrets", label: "Secrets and variables", path: "secrets" },
78 { key: "webhooks", label: "Webhooks", path: "webhooks" },
79 {
80 key: "deployments",
81 label: "Deployments",
82 path: "soon/deployments",
83 soon: "Defaults for every project's deployments: environments, who approves production, and the domains the workspace owns.",
84 },
85 {
86 key: "projects",
87 label: "Project defaults",
88 path: "soon/projects",
89 soon: "What a new project starts with: its visibility, branch rules, merge method and the agents on it.",
90 },
91 ],
92 },
93 {
94 key: "agents",
95 title: "Agents",
96 pages: [
97 {
98 key: "agent-settings",
99 label: "Agent settings",
100 path: "soon/agent-settings",
101 soon: "Budgets and the models agents may use are on the Agents overview today; who may create agents is under Permissions.",
102 },
103 ],
104 },
105 {
106 key: "chat",
107 title: "Chat",
108 pages: [
109 { key: "chat-settings", label: "Chat settings", path: "settings/chat" },
110 { key: "emoji", label: "Emoji", path: "emoji" },
111 ],
112 },
113 {
114 key: "artifacts",
115 title: "Artifacts",
116 pages: [
117 {
118 key: "artifact-settings",
119 label: "Spaces and sharing",
120 path: "soon/artifact-settings",
121 soon: "Which spaces a workspace starts with, and how a new artifact is shared by default.",
122 },
123 ],
124 },
125 {
126 key: "security",
127 title: "Security",
128 pages: [
129 { key: "security-settings", label: "Security settings", path: "security/settings" },
130 { key: "guardrails", label: "Guardrails", path: "guardrails" },
131 { key: "rules", label: "Rules", path: "rules" },
132 { key: "audit", label: "Audit log", path: "audit" },
133 ],
134 },
135 {
136 key: "integrations",
137 title: "Integrations",
138 pages: [
139 { key: "integrations", label: "Integrations", path: "integrations" },
140 {
141 key: "applications",
142 label: "Applications",
143 path: "soon/applications",
144 soon: "OAuth applications the workspace registers, and the ones it lets its members authorize.",
145 },
146 ],
147 },
148];
149
150/**
151 * Which of Workspace mode's pages `pathname` is, under the workspace
152 * `slug`, as a group key and a page key; null for the overview and for
153 * anything else. A page's own subpages (one ruleset, a receipt) count as
154 * the page; the deepest match wins, so Invitations is its own page and
155 * not a subpage of Members.
156 */
157export function workspaceSettingsPage(pathname: string, slug: string): { group: string; page: string } | null {
158 const parts = pagePath(pathname).split("/").filter(Boolean);
159 if (parts[0]?.toLowerCase() !== slug.toLowerCase() || parts[1] !== "-") return null;
160 const rest = parts.slice(2).join("/");
161 let found: { group: string; page: string; depth: number } | null = null;
162 for (const group of WORKSPACE_SETTINGS_GROUPS) {
163 for (const page of group.pages) {
164 if (page.soon) continue;
165 if (rest === page.path || rest.startsWith(`${page.path}/`)) {
166 const depth = page.path.split("/").length;
167 if (!found || depth > found.depth) found = { group: group.key, page: page.key, depth };
168 }
169 }
170 }
171 return found ? { group: found.group, page: found.page } : null;
172}
173
174/** `?tab=` as people write it, from the tabs the workspace page once had: the page it means. */
175const TAB_WORDS: Record<string, string> = {
176 overview: "",
177 projects: "-/projects",
178 repositories: "-/projects",
179 repos: "-/projects",
180 packages: "-/packages",
181 people: "-/people",
182 members: "-/members",
183 teams: "-/teams",
184 insights: "-/insights",
185 settings: "-/settings",
186};
187
188/** Workspace pages that moved, by their old name under `-/`. */
189const MOVED: Record<string, string> = {
190 // Home was called Today for a while.
191 today: "-/home",
192 "soon/teams": "-/teams",
193 "soon/insights": "-/insights",
194};
195
196/** Workspace pages that moved with everything under them, by their old name under `-/`: the rest of the path follows. */
197const MOVED_UNDER: Record<string, string> = {
198 // Agent templates were the Marketplace's Agents tab.
199 "marketplace/agents": "-/agents/templates",
200};
201
202/** The page a request is for: a click's data request ends in `.data`. */
203export function pagePath(pathname: string): string {
204 return pathname.replace(/\.data$/, "").replace(/\/+$/, "") || "/";
205}
206
207/**
208 * The page at `pathname` under the workspace `slug` instead of its first
209 * segment, keeping its query: where an old name or an alias leads. A click
210 * asks for the page's data at `<path>.data?_routes=…`; the address given is
211 * the page's own, which the browser then asks for as usual.
212 */
213export function underWorkspace(pathname: string, search: string, slug: string): string {
214 const params = new URLSearchParams(search);
215 params.delete("_routes");
216 const query = params.toString();
217 const segments = pagePath(pathname).split("/");
218 // segments[0] is the empty string before the leading slash.
219 segments[1] = slug;
220 return `${segments.join("/")}${query ? `?${query}` : ""}`;
221}
222
223/**
224 * Where an old address of a workspace's pages is now, keeping its query;
225 * null when it has not moved. `/<workspace>?tab=projects` and the like
226 * open that page.
227 */
228export function workspaceRedirect(pathname: string, search = ""): string | null {
229 // A click asks for the page's data at `<path>.data?_routes=…`; the page
230 // is the same one a full load asks for.
231 const params = new URLSearchParams(search);
232 params.delete("_routes");
233 search = params.toString();
234 const parts = pagePath(pathname).split("/").filter(Boolean);
235 const slug = parts[0];
236 if (!slug) return null;
237 if (parts.length === 1 && params.has("tab")) {
238 const to = TAB_WORDS[(params.get("tab") ?? "").toLowerCase()];
239 if (to === undefined) return null;
240 params.delete("tab");
241 const rest = params.toString();
242 return `/${slug}${to ? `/${to}` : ""}${rest ? `?${rest}` : ""}`;
243 }
244 if (parts[1] !== "-") return null;
245 const rest = parts.slice(2).join("/");
246 const under = Object.keys(MOVED_UNDER).find((from) => rest === from || rest.startsWith(`${from}/`));
247 const to = under ? `${MOVED_UNDER[under]}${rest.slice(under.length)}` : MOVED[rest];
248 if (to === undefined) return null;
249 return `/${slug}${to ? `/${to}` : ""}${search && search !== "?" ? (search.startsWith("?") ? search : `?${search}`) : ""}`;
250}
251
252/**
253 * The rail's modes. Home is the front page;
254 * Chat, Agents, Code and Artifacts are where work happens; Notifications
255 * spans them; People is who belongs; Workspace is the workspace itself,
256 * its settings area: who has access, money, policies and how each part is
257 * set up; Apps is everything installed that you
258 * can use. `account` is your own settings, under the avatar. `site` is
259 * g1t's own public pages (a profile, Explore, Search): no workspace's, so
260 * no mode is lit and no mode's sidebar sits beside them. Each mode has a
261 * sidebar of its own, or none, and which one is lit follows the address.
262 */
263export type ModeKey =
264 | "home"
265 | "chat"
266 | "notifications"
267 | "agents"
268 | "code"
269 | "artifacts"
270 | "people"
271 | "workspace"
272 | "apps"
273 | "account"
274 | "site";
275
276/** Workspace pages under `-/`, by the mode they belong to. Anything else of the workspace's is Code's. */
277const PAGE_MODES: Record<string, ModeKey> = {
278 home: "home",
279 chat: "chat",
280 artifacts: "artifacts",
281 agents: "agents",
282 context: "agents",
283 memory: "agents",
284 apps: "apps",
285 marketplace: "apps",
286 people: "people",
287 members: "workspace",
288 permissions: "workspace",
289 teams: "people",
290 "org-chart": "people",
291 workspace: "workspace",
292 spend: "workspace",
293 usage: "workspace",
294 gateway: "workspace",
295 billing: "workspace",
296 integrations: "workspace",
297 guardrails: "workspace",
298 rules: "workspace",
299 audit: "workspace",
300 settings: "workspace",
301 repositories: "workspace",
302 tokens: "workspace",
303 "personal-access-tokens": "workspace",
304 secrets: "workspace",
305 actions: "workspace",
306 runners: "workspace",
307 webhooks: "workspace",
308 emoji: "workspace",
309};
310
311/** First segments that are g1t's own pages, never a workspace: none is any mode's. */
312const SITE_PAGES = new Set(["explore", "search", "support", "policies", "security", "status", "u", "invite", "workspaces"]);
313
314/**
315 * The mode a path is in, for the workspace `slug`. A repository (anyone's)
316 * and a new project are Code's; g1t's own pages (Explore, search, a
317 * profile) are `site`: public, and no workspace's mode.
318 */
319export function modeOf(pathname: string, slug: string | null): ModeKey {
320 const path = pagePath(pathname);
321 if (path === "/") return "home";
322 if (path === "/notifications" || path.startsWith("/notifications/")) return "notifications";
323 if (path === "/settings" || path.startsWith("/settings/")) return "account";
324 const parts = path.split("/").filter(Boolean);
325 if (SITE_PAGES.has(parts[0] ?? "")) return "site";
326 if (slug && parts[0]?.toLowerCase() === slug.toLowerCase()) {
327 if (parts.length === 1) return "home";
328 if (parts[1] === "-") {
329 // The workspace's security settings are a policy; its alerts are Code's.
330 if (parts[2] === "security" && parts[3] === "settings") return "workspace";
331 return PAGE_MODES[parts[2] ?? ""] ?? "code";
332 }
333 }
334 return "code";
335}
336
337/** Where each mode's button in the rail goes, in the workspace `slug`. */
338export function modeHome(mode: ModeKey, slug: string): string {
339 switch (mode) {
340 case "home":
341 return homePagePath(slug);
342 case "notifications":
343 return "/notifications";
344 case "chat":
345 return `/${slug}/-/chat`;
346 case "artifacts":
347 return `/${slug}/-/artifacts`;
348 case "agents":
349 return `/${slug}/-/agents`;
350 case "code":
351 return `/${slug}/-/overview`;
352 case "people":
353 return `/${slug}/-/people`;
354 case "workspace":
355 return `/${slug}/-/workspace`;
356 case "apps":
357 return `/${slug}/-/apps`;
358 case "account":
359 return "/settings";
360 case "site":
361 return "/explore";
362 }
363}
364
365/** A workspace's front page: Home. */
366export function homePagePath(slug: string): string {
367 return `/${slug}/-/home`;
368}
369
370/**
371 * Where g1t's front door (`/`) and a workspace's own address lead someone
372 * in the workspace `slug`: Home, or Code's Overview when the query asks
373 * for its panels (a tab, or `?agent=new` to put an agent on something).
374 */
375export function homePath(slug: string, search = ""): string {
376 const params = new URLSearchParams(search);
377 params.delete("_routes");
378 params.delete("index");
379 const query = params.toString();
380 if (params.has("agent") || params.has("tab") || params.has("sort")) return `/${slug}/-/overview?${query}`;
381 return homePagePath(slug);
382}
383
384/** The page a member without Code access sees in place of anything of Code's. */
385export function codeAccessPath(slug: string, from?: string): string {
386 return `/${slug}/-/code-access${from ? `?from=${encodeURIComponent(from)}` : ""}`;
387}
388
389/** The workspace pages under `-/` that are Code's: closed to a member without Code access. */
390const CODE_PAGES = new Set(["overview", "projects", "repositories", "packages", "security", "rules", "runners", "actions", "context", "memory", "insights", "soon"]);
391
392/**
393 * Where a member without Code access goes instead of `pathname`, or null
394 * when the page is open to them. `noCode` is the workspaces (by slug) where
395 * they lack it; `chosen` is the one they are in. g1t's front door (`/`) is
396 * Home in that workspace, and so is the workspace's own address; its
397 * Code pages and every repository page is the page that says to ask an
398 * owner.
399 */
400export function codeGate(pathname: string, search: string, noCode: readonly string[], chosen: string | null): string | null {
401 if (noCode.length === 0) return null;
402 const path = pagePath(pathname);
403 if (path === "/") {
404 const slug = chosen && noCode.includes(chosen.toLowerCase()) ? chosen.toLowerCase() : null;
405 return slug ? homePagePath(slug) : null;
406 }
407 const parts = path.split("/").filter(Boolean);
408 const slug = parts[0]?.toLowerCase();
409 if (!slug || !noCode.includes(slug)) return null;
410 const from = `${path}${search && search !== "?" ? search : ""}`;
411 // The workspace's own page is Home.
412 if (parts.length === 1) return homePagePath(slug);
413 if (parts[1] === "-") return CODE_PAGES.has(parts[2] ?? "") ? codeAccessPath(slug, from) : null;
414 // A repository, and everything in it.
415 return codeAccessPath(slug, from);
416}