g1t/apps/web/app/lib/workspace-choice.ts
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 put | 1 | /** |
| 2 | * Which of your workspaces the sidebar and Mission control are about. | |
| 3 | * | |
| 4 | * It changes only when you choose one: from the switcher, or by opening a | |
| 5 | * workspace's own pages. Looking at a project in another workspace, or a | |
| 6 | * public one, leaves it alone, so coming back finds everything as it was. | |
| 7 | */ | |
| 8 | ||
| 9 | export const WORKSPACE_COOKIE = "g1t_ws"; | |
| 10 | ||
| 11 | type Membership = { slug: string }; | |
| 12 | ||
| 13 | /** The chosen workspace if you still belong to it, else your first. */ | |
| 14 | export function chosenWorkspace<M extends Membership>(memberships: M[], chosen: string | null | undefined): M | null { | |
| 15 | const wanted = chosen?.trim().toLowerCase(); | |
| 16 | return memberships.find((m) => m.slug.toLowerCase() === wanted) ?? memberships[0] ?? null; | |
| 17 | } | |
| 18 | ||
| 19 | /** | |
| 20 | * The workspace a page is about: the one in its address when it is one of | |
| 21 | * yours and the page is the workspace's own (not a project in it), else the | |
| 22 | * chosen one. | |
| 23 | */ | |
| 24 | export function workspaceFor<M extends Membership>( | |
| 25 | memberships: M[], | |
| 26 | chosen: string | null | undefined, | |
| 27 | params: { owner?: string; repo?: string }, | |
| 28 | ): M | null { | |
| 29 | if (params.owner && !params.repo) { | |
| 30 | const here = memberships.find((m) => m.slug.toLowerCase() === params.owner!.toLowerCase()); | |
| 31 | if (here) return here; | |
| 32 | } | |
| 33 | return chosenWorkspace(memberships, chosen); | |
| 34 | } | |
| 35 | ||
| 36 | /** The Set-Cookie header that remembers a choice, for a year. */ | |
| 37 | export function rememberWorkspace(slug: string, secure: boolean): string { | |
| 38 | return `${WORKSPACE_COOKIE}=${encodeURIComponent(slug.toLowerCase())}; Path=/; Max-Age=31536000; SameSite=Lax${secure ? "; Secure" : ""}`; | |
| 39 | } |