| 1 | /** |
| 2 | * Which frame a page is drawn in, and what the sidebar offers someone who |
| 3 | * is not signed in. |
| 4 | * |
| 5 | * Someone signed in always gets the app's frame: the rail and its modes. |
| 6 | * A visitor gets it on projects, workspaces and not-found pages, where the |
| 7 | * sidebar is the project's own menu. g1t's public pages that belong to no |
| 8 | * workspace (a person's profile, Explore, Search) are drawn for a visitor |
| 9 | * in the public frame instead: the top bar with the mark, search and |
| 10 | * signing in, the page at full width, and the footer. So do the front page |
| 11 | * and the pages about signing in, paying and trust. |
| 12 | */ |
| 13 | |
| 14 | import type { Abilities, Capability } from "@g1t/contracts"; |
| 15 | |
| 16 | /** Pages a visitor sees in the marketing frame. */ |
| 17 | const MARKETING = new Set([ |
| 18 | "/", |
| 19 | "/pricing", |
| 20 | "/login", |
| 21 | "/register", |
| 22 | "/logout", |
| 23 | "/verify", |
| 24 | "/confirm-email", |
| 25 | "/forgot", |
| 26 | "/reset", |
| 27 | "/device", |
| 28 | // Who makes g1t, and the promises it keeps: the policies, security, |
| 29 | // support and status. |
| 30 | "/policies", |
| 31 | "/security", |
| 32 | "/support", |
| 33 | "/status", |
| 34 | // These send a visitor to sign in; the frame matters only for a moment. |
| 35 | "/new", |
| 36 | "/settings", |
| 37 | "/workspaces/new", |
| 38 | ]); |
| 39 | |
| 40 | /** g1t's public pages that are no workspace's: a visitor reads them in the public frame. */ |
| 41 | const PUBLIC_PAGES = new Set(["/explore", "/search"]); |
| 42 | |
| 43 | /** Whether the page is drawn in the app's sidebar frame. */ |
| 44 | export function usesAppShell(pathname: string, signedIn: boolean): boolean { |
| 45 | if (signedIn) return true; |
| 46 | const path = pathname.length > 1 ? pathname.replace(/\/+$/, "") : pathname; |
| 47 | if (MARKETING.has(path) || PUBLIC_PAGES.has(path)) return false; |
| 48 | // A person's profile: theirs, not any workspace's. |
| 49 | if (/^\/u\/[^/]+$/.test(path)) return false; |
| 50 | if (path === "/oauth" || path.startsWith("/oauth/")) return false; |
| 51 | // An invite link is the front door: the marketing frame, like /register. |
| 52 | if (path.startsWith("/invite/")) return false; |
| 53 | if (path.startsWith("/policies/")) return false; |
| 54 | // Your settings pages, like /settings: a visitor is sent to sign in. |
| 55 | if (path.startsWith("/settings/")) return false; |
| 56 | return true; |
| 57 | } |
| 58 | |
| 59 | export type SidebarItem = { label: string; to: string }; |
| 60 | |
| 61 | /** The visitor's sidebar menu: where to browse from. */ |
| 62 | export const VISITOR_LINKS: SidebarItem[] = [ |
| 63 | { label: "Explore", to: "/explore" }, |
| 64 | { label: "Search", to: "/search" }, |
| 65 | ]; |
| 66 | |
| 67 | /** The project pages the sidebar lists, by key, for a member or not. */ |
| 68 | export type ProjectPage = |
| 69 | | "code" |
| 70 | | "issues" |
| 71 | | "pulls" |
| 72 | | "agents" |
| 73 | | "actions" |
| 74 | | "deployments" |
| 75 | | "observability" |
| 76 | | "security" |
| 77 | | "insights" |
| 78 | | "settings"; |
| 79 | |
| 80 | /** |
| 81 | * What a page needs beyond a role on the repository, once what the viewer |
| 82 | * may do is known: Security's findings are for people who can push. |
| 83 | */ |
| 84 | export const PAGE_NEEDS: Partial<Record<ProjectPage, Capability>> = { security: "push" }; |
| 85 | |
| 86 | /** |
| 87 | * A project's menu, in order. Security and Settings are for people with a |
| 88 | * role on its repository, and a page in `PAGE_NEEDS` only for those whose |
| 89 | * role has its capability (`can`, once the repository's page has loaded); |
| 90 | * everyone who can see the project sees the rest. |
| 91 | */ |
| 92 | export function projectPages(member: boolean, can?: Partial<Abilities>): ProjectPage[] { |
| 93 | const pages: (ProjectPage | false)[] = [ |
| 94 | "code", |
| 95 | "issues", |
| 96 | "pulls", |
| 97 | "agents", |
| 98 | "actions", |
| 99 | // A public repository's deployments and build logs are for anyone, as its code is. |
| 100 | "deployments", |
| 101 | "observability", |
| 102 | member && "security", |
| 103 | "insights", |
| 104 | member && "settings", |
| 105 | ]; |
| 106 | return pages.filter( |
| 107 | (page): page is ProjectPage => page !== false && (!can || !PAGE_NEEDS[page] || Boolean(can[PAGE_NEEDS[page]!])), |
| 108 | ); |
| 109 | } |