Skip to content

g1t/apps/web/app/lib/workspace-choice.ts

46 lines1,940 bytesCodeBlame

Pick any line to see why it is the way it is: the commit, the pull request and issue it came from, and what the agent was thinking.

Team plan, an open-source pool, monthly trials and honest metering; the sidebar for everyone; a workspace that stays put1/**
2 * Which of your workspaces the sidebar and Mission control are about.
3 *
Usage, Billing settings and prepaid AI credit; fixes from the UX audit4 * 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.
Team plan, an open-source pool, monthly trials and honest metering; the sidebar for everyone; a workspace that stays put9 */
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
Usage, Billing settings and prepaid AI credit; fixes from the UX audit23 * yours, whether the page is the workspace's own or a project in it, else
24 * the chosen one.
Team plan, an open-source pool, monthly trials and honest metering; the sidebar for everyone; a workspace that stays put25 */
26export function workspaceFor<M extends Membership>(
27 memberships: M[],
28 chosen: string | null | undefined,
29 params: { owner?: string; repo?: string },
30): M | null {
Usage, Billing settings and prepaid AI credit; fixes from the UX audit31 if (params.owner) {
Team plan, an open-source pool, monthly trials and honest metering; the sidebar for everyone; a workspace that stays put32 const here = memberships.find((m) => m.slug.toLowerCase() === params.owner!.toLowerCase());
33 if (here) return here;
34 }
35 return chosenWorkspace(memberships, chosen);
36}
37
38/** The Set-Cookie header that remembers a choice, for a year. */
39export function rememberWorkspace(slug: string, secure: boolean): string {
40 return `${WORKSPACE_COOKIE}=${encodeURIComponent(slug.toLowerCase())}; Path=/; Max-Age=31536000; SameSite=Lax${secure ? "; Secure" : ""}`;
41}
Invite-only launch: sign in with GitHub, repository access and lifecycle, many emails, a new look42
43/** The Set-Cookie header that forgets the choice: after its workspace is deleted. */
44export function forgetWorkspace(secure: boolean): string {
45 return `${WORKSPACE_COOKIE}=; Path=/; Max-Age=0; SameSite=Lax${secure ? "; Secure" : ""}`;
46}