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.

265 linesCodeBlameRaw
1/**
2 * The rail's built-in apps, the apps a workspace added, and the ones each
3 * person pins to their rail.
4 *
5 * Built-in apps (Home, Work, Chat, Notifications, Code, Artifacts, Routines,
6 * People and Workspace) are always in the rail; their own pages (Projects, Usage,
7 * Teams and the rest) are in their sidebars. Apps are the integrations
8 * the workspace connected, each opening where it is looked after. Each
9 * person pins the ones they want.
10 *
11 * An installed app's key says what it is: `int-<connector>` for an
12 * integration (@g1t/contracts/connectors). Integrations are known from the
13 * catalog alone, so a pin can be read without asking a service. A key that
14 * names nothing pinnable (a built-in page pinned before, a plugin or
15 * connector from before they were retired) is dropped wherever pins are
16 * read.
17 *
18 * Pins are each person's own, per workspace, in the order they set, and
19 * kept with their account by the identity service (`dock_pins`), so the
20 * dock is the same on every device (lib/dock.server.ts). A cookie
21 * (`g1t_dock`) keeps a copy on each device: the rail still draws from it
22 * when identity cannot be reached, and pins from before they were kept
23 * with the account carry over from it until the first change.
24 * No Workers or React imports, so it can be tested under Node.
25 */
26import { CONNECTORS, type Connector, connectorPath } from "@g1t/contracts/connectors";
27
28/** The apps that are always in the rail. */
29export type BuiltinApp = "home" | "work" | "chat" | "notifications" | "code" | "artifacts" | "routines" | "people" | "workspace";
30
31export type BuiltinInfo = {
32 key: BuiltinApp;
33 name: string;
34 /** Its page in the workspace `slug`. */
35 path: (slug: string) => string;
36 /** Part of Code: only for members with Code access. */
37 code?: boolean;
38};
39
40const under = (page: string) => (slug: string) => `/${slug}/-/${page}`;
41
42/** The rail's built-in apps, in its order. */
43export const BUILTINS: BuiltinInfo[] = [
44 { key: "home", name: "Home", path: under("home") },
45 { key: "work", name: "Work", path: under("work") },
46 { key: "chat", name: "Chat", path: under("chat") },
47 { key: "notifications", name: "Notifications", path: () => "/notifications" },
48 { key: "code", name: "Code", path: under("overview"), code: true },
49 { key: "artifacts", name: "Artifacts", path: under("artifacts") },
50 { key: "routines", name: "Routines", path: under("routines") },
51 { key: "people", name: "People", path: under("people") },
52 { key: "workspace", name: "Workspace", path: under("workspace") },
53];
54
55/**
56 * The rail's apps from its top, in order: Home, Explore (g1t beyond your
57 * workspaces, which is no built-in app), Work with its sessions, Chat,
58 * Code for a member with Code access, Artifacts and Routines. People and
59 * Workspace stand at its foot.
60 */
61export const RAIL_APPS = ["home", "explore", "work", "chat", "code", "artifacts", "routines"] as const;
62
63export type RailApp = (typeof RAIL_APPS)[number];
64
65/** The rail's apps for a member, with Code only when they have Code access. */
66export function railApps(code: boolean): RailApp[] {
67 return RAIL_APPS.filter((key) => code || key !== "code");
68}
69
70/** A phone's bottom bar, before More: Work for every member, Code under More. */
71export const PHONE_TABS = ["home", "work", "chat", "notifications"] as const satisfies readonly BuiltinApp[];
72
73/**
74 * The built-in apps the phone's More sheet has, in its order: Code for a
75 * member with Code access, then the rest of the rail's. Explore, which is
76 * no built-in app, heads the sheet before them; Sessions are Work's.
77 */
78export function phoneMoreApps(code: boolean): BuiltinApp[] {
79 const rest = BUILTINS.map((app) => app.key).filter((key) => !(PHONE_TABS as readonly string[]).includes(key) && key !== "code");
80 return code ? ["code", ...rest] : rest;
81}
82
83/** The built-in app `key`. */
84export function appOf(key: BuiltinApp): BuiltinInfo {
85 return BUILTINS.find((app) => app.key === key)!;
86}
87
88/** An installed app's key, as pins and forms carry it: `int-sentry`. */
89export type PinnableApp = string;
90
91/** An app the workspace added: an integration it connected. */
92export type InstalledApp = {
93 key: PinnableApp;
94 name: string;
95 /** What it is, in a few words. */
96 about: string;
97 /** The listing it was added from: `integration:sentry`. */
98 listing: string;
99 /** Where it is from, in a word or two: g1t. */
100 from: string;
101 /** What draws its mark: a connector's id and name, and its provider when it has one. */
102 connector: Pick<Connector, "id" | "name" | "provider">;
103 /** Its page in the workspace `slug`. */
104 path: (slug: string) => string;
105 /**
106 * Whether the person can open it. Every member can open an integration's
107 * page; an app limited to some people shows Request access to the rest.
108 */
109 usable: boolean;
110 /** Whether it can be pinned to the rail: only what the catalog alone knows. */
111 pinnable: boolean;
112};
113
114/** Where an integration is looked after: its setup page, by kind, or its own page. */
115const SECTION_BY_CATEGORY: Partial<Record<Connector["category"], string>> = { monitoring: "alerts", issues: "trackers" };
116
117function integrationPath(connector: Connector): (slug: string) => string {
118 const section = connector.provider ? SECTION_BY_CATEGORY[connector.category] : undefined;
119 if (section) return under(`integrations/${section}`);
120 const href = connector.href?.workspace;
121 return href ? (slug) => connectorPath(href, slug) : under("integrations");
122}
123
124/** Identity keeps keys of at most this many characters (MAX_DOCK_APP_KEY). */
125const MAX_KEY = 32;
126
127/** The installed-app key for an integration: `int-sentry`. */
128export function integrationAppKey(connectorId: string): PinnableApp {
129 return `int-${connectorId}`;
130}
131
132/**
133 * The app a key names, from the catalog alone (no service is asked), or
134 * null when it names nothing that can be installed: a retired key, a
135 * built-in page, or a connector that is not available for a workspace.
136 */
137export function installedAppOf(key: string): InstalledApp | null {
138 if (key.length > MAX_KEY || !/^int-[a-z0-9][a-z0-9-]*$/.test(key)) return null;
139 const connector = CONNECTORS.find((c) => c.id === key.slice(4));
140 if (!connector || connector.status !== "available" || !connector.scopes.includes("workspace")) return null;
141 return {
142 key,
143 name: connector.name,
144 about: connector.description,
145 listing: `integration:${connector.id}`,
146 from: "g1t",
147 connector: { id: connector.id, name: connector.name, provider: connector.provider },
148 path: integrationPath(connector),
149 usable: true,
150 pinnable: true,
151 };
152}
153
154/** Whether `key` names an app that can be pinned. */
155export function isPinnable(key: string): key is PinnableApp {
156 return installedAppOf(key) != null;
157}
158
159/**
160 * The apps the workspace added: the integrations it has connected (by
161 * connector id, lib/connected.server.ts) in catalog order. `manage`, when a
162 * connection has one, is where its app opens.
163 */
164export function installedApps(connected: Record<string, { manage: string | null }>): InstalledApp[] {
165 const integrations = CONNECTORS.filter((connector) => connected[connector.id]).map((connector) => {
166 const app = installedAppOf(integrationAppKey(connector.id));
167 const manage = connected[connector.id]?.manage;
168 return app && manage ? { ...app, path: () => manage } : app;
169 });
170 return integrations.filter((app): app is InstalledApp => app != null);
171}
172
173/** An app as a loader sends it: everything but its path, which is worked out for the workspace. */
174export type InstalledAppData = Omit<InstalledApp, "path"> & { href: string };
175
176export function appData(app: InstalledApp, slug: string): InstalledAppData {
177 const { path, ...rest } = app;
178 return { ...rest, href: path(slug) };
179}
180
181/** The cookie that keeps this device's copy of each person's pins, per workspace. */
182export const DOCK_COOKIE = "g1t_dock";
183
184/**
185 * Most workspaces the cookie remembers pins for, and most pins in each.
186 * Identity keeps up to 24 (`MAX_DOCK_PINS`); this stays under it.
187 */
188const MAX_WORKSPACES = 20;
189export const MAX_APP_PINS = 12;
190
191/**
192 * Pins as identity keeps them, read against the apps there are: keys that
193 * are not an app people pin (one since retired, say) are dropped, repeats
194 * too, in the order they were set.
195 */
196export function pinsFrom(keys: readonly string[]): PinnableApp[] {
197 return [...new Set(keys.filter(isPinnable))].slice(0, MAX_APP_PINS);
198}
199
200/**
201 * The pins to draw in the workspace `slug`: the account's when identity
202 * has them (`stored`), else this device's cookie, for pins never saved to
203 * the account or when identity could not be asked.
204 */
205export function pinsToShow(stored: readonly string[] | null | undefined, cookie: string | null | undefined, slug: string): PinnableApp[] {
206 return stored ? pinsFrom(stored) : pinsIn(cookie, slug);
207}
208
209/** Every workspace's pins, from the cookie's value: `acme:projects.usage,beta:teams`. */
210export function readDock(value: string | null | undefined): Record<string, PinnableApp[]> {
211 const dock: Record<string, PinnableApp[]> = {};
212 for (const entry of (value ?? "").split(",")) {
213 const [slug, keys] = entry.split(":");
214 if (!slug || !/^[a-z0-9-]+$/.test(slug) || keys === undefined) continue;
215 const pins = [...new Set(keys.split(".").filter(isPinnable))].slice(0, MAX_APP_PINS);
216 dock[slug] = pins;
217 }
218 return dock;
219}
220
221/** The pins in the workspace `slug`, in the order they were pinned. */
222export function pinsIn(value: string | null | undefined, slug: string): PinnableApp[] {
223 return readDock(value)[slug.toLowerCase()] ?? [];
224}
225
226/** The cookie's value with the workspace `slug`'s pins set to `pins`: the latest workspace first. */
227export function writeDock(value: string | null | undefined, slug: string, pins: PinnableApp[]): string {
228 const dock = readDock(value);
229 const key = slug.toLowerCase();
230 delete dock[key];
231 const entries = [[key, [...new Set(pins.filter(isPinnable))].slice(0, MAX_APP_PINS)] as const, ...Object.entries(dock)].slice(0, MAX_WORKSPACES);
232 return entries.map(([name, keys]) => `${name}:${keys.join(".")}`).join(",");
233}
234
235/** One change to someone's pins, as the pin buttons post it; null when it asks for nothing that makes sense. */
236export function appPinFromForm(form: FormData): { app: PinnableApp; pinned: boolean } | null {
237 const app = String(form.get("app") ?? "");
238 const intent = form.get("intent");
239 if (!isPinnable(app) || (intent !== "pin" && intent !== "unpin")) return null;
240 return { app, pinned: intent === "pin" };
241}
242
243/** The pins after pinning or unpinning `app`: a new pin goes at the end. */
244export function withPin(pins: PinnableApp[], app: PinnableApp, pinned: boolean): PinnableApp[] {
245 const rest = pins.filter((key) => key !== app);
246 return pinned ? [...rest, app] : rest;
247}
248
249/** The Set-Cookie header that keeps the rail, for a year. */
250export function dockCookie(value: string, secure: boolean): string {
251 return `${DOCK_COOKIE}=${encodeURIComponent(value)}; Path=/; Max-Age=31536000; SameSite=Lax${secure ? "; Secure" : ""}`;
252}
253
254/** The cookie that remembers whether the in-context sidebar is folded away on a computer. */
255export const SIDEBAR_COOKIE = "g1t_sidebar";
256
257/** Whether the sidebar is folded away, from the cookie's value. */
258export function sidebarClosed(value: string | null | undefined): boolean {
259 return value === "closed";
260}
261
262/** The cookie, as the page sets it when the sidebar is folded away or brought back. */
263export function sidebarCookie(closed: boolean, secure: boolean): string {
264 return `${SIDEBAR_COOKIE}=${closed ? "closed" : "open"}; Path=/; Max-Age=31536000; SameSite=Lax${secure ? "; Secure" : ""}`;
265}