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.

42 linesCodeBlameRaw
1/**
2 * Which of your workspaces the sidebar and Mission control are about.
3 *
4 * The page's own workspace wins: a workspace's pages, and a project in one
5 * of your workspaces, are about that workspace, so the switcher always
6 * names the workspace you are looking at. A project somewhere you do not
7 * belong (a public one, or one shared with you) leaves the chosen one, and
8 * the choice itself changes only from the switcher or a workspace's pages.
9 */
10
11export const WORKSPACE_COOKIE = "g1t_ws";
12
13type Membership = { slug: string };
14
15/** The chosen workspace if you still belong to it, else your first. */
16export function chosenWorkspace<M extends Membership>(memberships: M[], chosen: string | null | undefined): M | null {
17 const wanted = chosen?.trim().toLowerCase();
18 return memberships.find((m) => m.slug.toLowerCase() === wanted) ?? memberships[0] ?? null;
19}
20
21/**
22 * The workspace a page is about: the one in its address when it is one of
23 * yours, whether the page is the workspace's own or a project in it, else
24 * the chosen one.
25 */
26export function workspaceFor<M extends Membership>(memberships: M[], chosen: string | null | undefined, params: { owner?: string; repo?: string }): M | null {
27 if (params.owner) {
28 const here = memberships.find((m) => m.slug.toLowerCase() === params.owner!.toLowerCase());
29 if (here) return here;
30 }
31 return chosenWorkspace(memberships, chosen);
32}
33
34/** The Set-Cookie header that remembers a choice, for a year. */
35export function rememberWorkspace(slug: string, secure: boolean): string {
36 return `${WORKSPACE_COOKIE}=${encodeURIComponent(slug.toLowerCase())}; Path=/; Max-Age=31536000; SameSite=Lax${secure ? "; Secure" : ""}`;
37}
38
39/** The Set-Cookie header that forgets the choice: after its workspace is deleted. */
40export function forgetWorkspace(secure: boolean): string {
41 return `${WORKSPACE_COOKIE}=; Path=/; Max-Age=0; SameSite=Lax${secure ? "; Secure" : ""}`;
42}