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