Skip to content
278 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 * The workspace's pages that its Settings row drills into. Runners is not
30 * one: it is a page of Workspace mode's own, at `-/runners`.
31 */
32export const SETTINGS_PAGES = [
33 "settings",
34 "repositories",
35 "tokens",
36 "rules",
37 "guardrails",
38 "secrets",
39 "actions",
40 "integrations",
41 "webhooks",
42 "emoji",
43 "billing",
44 "audit",
45] as const;
46
47/** `?tab=` as people write it, from the tabs the workspace page once had: the page it means. */
48const TAB_WORDS: Record<string, string> = {
49 overview: "",
50 projects: "-/projects",
51 repositories: "-/projects",
52 repos: "-/projects",
53 packages: "-/packages",
54 people: "-/people",
55 members: "-/people",
56 teams: "-/teams",
57 insights: "-/insights",
58 settings: "-/settings",
59};
60
61/** Workspace pages that moved, by their old name under `-/`. */
62const MOVED: Record<string, string> = {
63 home: "-/today",
64 members: "-/people",
65 "soon/teams": "-/teams",
66 "soon/insights": "-/insights",
67};
68
69/** The page a request is for: a click's data request ends in `.data`. */
70export function pagePath(pathname: string): string {
71 return pathname.replace(/\.data$/, "").replace(/\/+$/, "") || "/";
72}
73
74/**
75 * The page at `pathname` under the workspace `slug` instead of its first
76 * segment, keeping its query: where an old name or an alias leads. A click
77 * asks for the page's data at `<path>.data?_routes=…`; the address given is
78 * the page's own, which the browser then asks for as usual.
79 */
80export function underWorkspace(pathname: string, search: string, slug: string): string {
81 const params = new URLSearchParams(search);
82 params.delete("_routes");
83 const query = params.toString();
84 const segments = pagePath(pathname).split("/");
85 // segments[0] is the empty string before the leading slash.
86 segments[1] = slug;
87 return `${segments.join("/")}${query ? `?${query}` : ""}`;
88}
89
90/**
91 * Where an old address of a workspace's pages is now, keeping its query;
92 * null when it has not moved. `/<workspace>?tab=projects` and the like
93 * open that page.
94 */
95export function workspaceRedirect(pathname: string, search = ""): string | null {
96 // A click asks for the page's data at `<path>.data?_routes=…`; the page
97 // is the same one a full load asks for.
98 const params = new URLSearchParams(search);
99 params.delete("_routes");
100 search = params.toString();
101 const parts = pagePath(pathname).split("/").filter(Boolean);
102 const slug = parts[0];
103 if (!slug) return null;
104 if (parts.length === 1 && params.has("tab")) {
105 const to = TAB_WORDS[(params.get("tab") ?? "").toLowerCase()];
106 if (to === undefined) return null;
107 params.delete("tab");
108 const rest = params.toString();
109 return `/${slug}${to ? `/${to}` : ""}${rest ? `?${rest}` : ""}`;
110 }
111 if (parts[1] !== "-") return null;
112 const to = MOVED[parts.slice(2).join("/")];
113 if (to === undefined) return null;
114 return `/${slug}${to ? `/${to}` : ""}${search && search !== "?" ? (search.startsWith("?") ? search : `?${search}`) : ""}`;
115}
116
117/**
118 * The dock's modes. Today is the front page;
119 * Chat, Agents, Code and Artifacts are where work happens; Notifications
120 * spans them; People is who belongs; Workspace is the workspace itself:
121 * its money, policies and settings; Apps is everything installed that you
122 * can use. `account` is your own settings, under the avatar. `site` is
123 * g1t's own public pages (a profile, Explore, Search): no workspace's, so
124 * no mode is lit and no mode's sidebar sits beside them. Each mode has a
125 * sidebar of its own, or none, and which one is lit follows the address.
126 */
127export type ModeKey =
128 | "today"
129 | "chat"
130 | "notifications"
131 | "agents"
132 | "code"
133 | "artifacts"
134 | "people"
135 | "workspace"
136 | "apps"
137 | "account"
138 | "site";
139
140/** Workspace pages under `-/`, by the mode they belong to. Anything else of the workspace's is Code's. */
141const PAGE_MODES: Record<string, ModeKey> = {
142 today: "today",
143 chat: "chat",
144 artifacts: "artifacts",
145 agents: "agents",
146 context: "agents",
147 memory: "agents",
148 apps: "apps",
149 marketplace: "apps",
150 people: "people",
151 members: "people",
152 teams: "people",
153 workspace: "workspace",
154 spend: "workspace",
155 usage: "workspace",
156 gateway: "workspace",
157 billing: "workspace",
158 integrations: "workspace",
159 guardrails: "workspace",
160 rules: "workspace",
161 audit: "workspace",
162 settings: "workspace",
163 repositories: "workspace",
164 tokens: "workspace",
165 "personal-access-tokens": "workspace",
166 secrets: "workspace",
167 actions: "workspace",
168 runners: "workspace",
169 webhooks: "workspace",
170 emoji: "workspace",
171};
172
173/** First segments that are g1t's own pages, never a workspace: none is any mode's. */
174const SITE_PAGES = new Set(["explore", "search", "support", "policies", "security", "status", "u", "invite", "workspaces"]);
175
176/**
177 * The mode a path is in, for the workspace `slug`. A repository (anyone's)
178 * and a new project are Code's; g1t's own pages (Explore, search, a
179 * profile) are `site`: public, and no workspace's mode.
180 */
181export function modeOf(pathname: string, slug: string | null): ModeKey {
182 const path = pagePath(pathname);
183 if (path === "/") return "today";
184 if (path === "/notifications" || path.startsWith("/notifications/")) return "notifications";
185 if (path === "/settings" || path.startsWith("/settings/")) return "account";
186 const parts = path.split("/").filter(Boolean);
187 if (SITE_PAGES.has(parts[0] ?? "")) return "site";
188 if (slug && parts[0]?.toLowerCase() === slug.toLowerCase()) {
189 if (parts.length === 1) return "today";
190 if (parts[1] === "-") {
191 // The workspace's security settings are a policy; its alerts are Code's.
192 if (parts[2] === "security" && parts[3] === "settings") return "workspace";
193 return PAGE_MODES[parts[2] ?? ""] ?? "code";
194 }
195 }
196 return "code";
197}
198
199/** Where each mode's button in the dock goes, in the workspace `slug`. */
200export function modeHome(mode: ModeKey, slug: string): string {
201 switch (mode) {
202 case "today":
203 return todayPath(slug);
204 case "notifications":
205 return "/notifications";
206 case "chat":
207 return `/${slug}/-/chat`;
208 case "artifacts":
209 return `/${slug}/-/artifacts`;
210 case "agents":
211 return `/${slug}/-/agents`;
212 case "code":
213 return `/${slug}/-/overview`;
214 case "people":
215 return `/${slug}/-/people`;
216 case "workspace":
217 return `/${slug}/-/workspace`;
218 case "apps":
219 return `/${slug}/-/apps`;
220 case "account":
221 return "/settings";
222 case "site":
223 return "/explore";
224 }
225}
226
227/** A workspace's front page: Today. */
228export function todayPath(slug: string): string {
229 return `/${slug}/-/today`;
230}
231
232/**
233 * Where g1t's front door (`/`) and a workspace's own address lead someone
234 * in the workspace `slug`: Today, or Code's Overview when the query asks
235 * for its panels (a tab, or `?agent=new` to put an agent on something).
236 */
237export function homePath(slug: string, search = ""): string {
238 const params = new URLSearchParams(search);
239 params.delete("_routes");
240 params.delete("index");
241 const query = params.toString();
242 if (params.has("agent") || params.has("tab") || params.has("sort")) return `/${slug}/-/overview?${query}`;
243 return todayPath(slug);
244}
245
246/** The page a member without Code access sees in place of anything of Code's. */
247export function codeAccessPath(slug: string, from?: string): string {
248 return `/${slug}/-/code-access${from ? `?from=${encodeURIComponent(from)}` : ""}`;
249}
250
251/** The workspace pages under `-/` that are Code's: closed to a member without Code access. */
252const CODE_PAGES = new Set(["overview", "projects", "repositories", "packages", "security", "rules", "runners", "actions", "context", "memory", "insights", "soon"]);
253
254/**
255 * Where a member without Code access goes instead of `pathname`, or null
256 * when the page is open to them. `noCode` is the workspaces (by slug) where
257 * they lack it; `chosen` is the one they are in. g1t's front door (`/`) is
258 * Today in that workspace, and so is the workspace's own address; its
259 * Code pages and every repository page is the page that says to ask an
260 * owner.
261 */
262export function codeGate(pathname: string, search: string, noCode: readonly string[], chosen: string | null): string | null {
263 if (noCode.length === 0) return null;
264 const path = pagePath(pathname);
265 if (path === "/") {
266 const slug = chosen && noCode.includes(chosen.toLowerCase()) ? chosen.toLowerCase() : null;
267 return slug ? todayPath(slug) : null;
268 }
269 const parts = path.split("/").filter(Boolean);
270 const slug = parts[0]?.toLowerCase();
271 if (!slug || !noCode.includes(slug)) return null;
272 const from = `${path}${search && search !== "?" ? search : ""}`;
273 // The workspace's own page is Today.
274 if (parts.length === 1) return todayPath(slug);
275 if (parts[1] === "-") return CODE_PAGES.has(parts[2] ?? "") ? codeAccessPath(slug, from) : null;
276 // A repository, and everything in it.
277 return codeAccessPath(slug, from);
278}