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