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.

249 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/** A phone's bottom bar, before More: Work for every member, Code under More. */
56export const PHONE_TABS = ["home", "work", "chat", "notifications"] as const satisfies readonly BuiltinApp[];
57
58/**
59 * The built-in apps the phone's More sheet has, in its order: Code first,
60 * for a member with Code access, then the rest of the rail's.
61 */
62export function phoneMoreApps(code: boolean): BuiltinApp[] {
63 const rest = BUILTINS.map((app) => app.key).filter((key) => !(PHONE_TABS as readonly string[]).includes(key) && key !== "code");
64 return code ? ["code", ...rest] : rest;
65}
66
67/** The built-in app `key`. */
68export function appOf(key: BuiltinApp): BuiltinInfo {
69 return BUILTINS.find((app) => app.key === key)!;
70}
71
72/** An installed app's key, as pins and forms carry it: `int-sentry`. */
73export type PinnableApp = string;
74
75/** An app the workspace added: an integration it connected. */
76export type InstalledApp = {
77 key: PinnableApp;
78 name: string;
79 /** What it is, in a few words. */
80 about: string;
81 /** The listing it was added from: `integration:sentry`. */
82 listing: string;
83 /** Where it is from, in a word or two: g1t. */
84 from: string;
85 /** What draws its mark: a connector's id and name, and its provider when it has one. */
86 connector: Pick<Connector, "id" | "name" | "provider">;
87 /** Its page in the workspace `slug`. */
88 path: (slug: string) => string;
89 /**
90 * Whether the person can open it. Every member can open an integration's
91 * page; an app limited to some people shows Request access to the rest.
92 */
93 usable: boolean;
94 /** Whether it can be pinned to the rail: only what the catalog alone knows. */
95 pinnable: boolean;
96};
97
98/** Where an integration is looked after: its setup page, by kind, or its own page. */
99const SECTION_BY_CATEGORY: Partial<Record<Connector["category"], string>> = { monitoring: "alerts", issues: "trackers" };
100
101function integrationPath(connector: Connector): (slug: string) => string {
102 const section = connector.provider ? SECTION_BY_CATEGORY[connector.category] : undefined;
103 if (section) return under(`integrations/${section}`);
104 const href = connector.href?.workspace;
105 return href ? (slug) => connectorPath(href, slug) : under("integrations");
106}
107
108/** Identity keeps keys of at most this many characters (MAX_DOCK_APP_KEY). */
109const MAX_KEY = 32;
110
111/** The installed-app key for an integration: `int-sentry`. */
112export function integrationAppKey(connectorId: string): PinnableApp {
113 return `int-${connectorId}`;
114}
115
116/**
117 * The app a key names, from the catalog alone (no service is asked), or
118 * null when it names nothing that can be installed: a retired key, a
119 * built-in page, or a connector that is not available for a workspace.
120 */
121export function installedAppOf(key: string): InstalledApp | null {
122 if (key.length > MAX_KEY || !/^int-[a-z0-9][a-z0-9-]*$/.test(key)) return null;
123 const connector = CONNECTORS.find((c) => c.id === key.slice(4));
124 if (!connector || connector.status !== "available" || !connector.scopes.includes("workspace")) return null;
125 return {
126 key,
127 name: connector.name,
128 about: connector.description,
129 listing: `integration:${connector.id}`,
130 from: "g1t",
131 connector: { id: connector.id, name: connector.name, provider: connector.provider },
132 path: integrationPath(connector),
133 usable: true,
134 pinnable: true,
135 };
136}
137
138/** Whether `key` names an app that can be pinned. */
139export function isPinnable(key: string): key is PinnableApp {
140 return installedAppOf(key) != null;
141}
142
143/**
144 * The apps the workspace added: the integrations it has connected (by
145 * connector id, lib/connected.server.ts) in catalog order. `manage`, when a
146 * connection has one, is where its app opens.
147 */
148export function installedApps(connected: Record<string, { manage: string | null }>): InstalledApp[] {
149 const integrations = CONNECTORS.filter((connector) => connected[connector.id]).map((connector) => {
150 const app = installedAppOf(integrationAppKey(connector.id));
151 const manage = connected[connector.id]?.manage;
152 return app && manage ? { ...app, path: () => manage } : app;
153 });
154 return integrations.filter((app): app is InstalledApp => app != null);
155}
156
157/** An app as a loader sends it: everything but its path, which is worked out for the workspace. */
158export type InstalledAppData = Omit<InstalledApp, "path"> & { href: string };
159
160export function appData(app: InstalledApp, slug: string): InstalledAppData {
161 const { path, ...rest } = app;
162 return { ...rest, href: path(slug) };
163}
164
165/** The cookie that keeps this device's copy of each person's pins, per workspace. */
166export const DOCK_COOKIE = "g1t_dock";
167
168/**
169 * Most workspaces the cookie remembers pins for, and most pins in each.
170 * Identity keeps up to 24 (`MAX_DOCK_PINS`); this stays under it.
171 */
172const MAX_WORKSPACES = 20;
173export const MAX_APP_PINS = 12;
174
175/**
176 * Pins as identity keeps them, read against the apps there are: keys that
177 * are not an app people pin (one since retired, say) are dropped, repeats
178 * too, in the order they were set.
179 */
180export function pinsFrom(keys: readonly string[]): PinnableApp[] {
181 return [...new Set(keys.filter(isPinnable))].slice(0, MAX_APP_PINS);
182}
183
184/**
185 * The pins to draw in the workspace `slug`: the account's when identity
186 * has them (`stored`), else this device's cookie, for pins never saved to
187 * the account or when identity could not be asked.
188 */
189export function pinsToShow(stored: readonly string[] | null | undefined, cookie: string | null | undefined, slug: string): PinnableApp[] {
190 return stored ? pinsFrom(stored) : pinsIn(cookie, slug);
191}
192
193/** Every workspace's pins, from the cookie's value: `acme:projects.usage,beta:teams`. */
194export function readDock(value: string | null | undefined): Record<string, PinnableApp[]> {
195 const dock: Record<string, PinnableApp[]> = {};
196 for (const entry of (value ?? "").split(",")) {
197 const [slug, keys] = entry.split(":");
198 if (!slug || !/^[a-z0-9-]+$/.test(slug) || keys === undefined) continue;
199 const pins = [...new Set(keys.split(".").filter(isPinnable))].slice(0, MAX_APP_PINS);
200 dock[slug] = pins;
201 }
202 return dock;
203}
204
205/** The pins in the workspace `slug`, in the order they were pinned. */
206export function pinsIn(value: string | null | undefined, slug: string): PinnableApp[] {
207 return readDock(value)[slug.toLowerCase()] ?? [];
208}
209
210/** The cookie's value with the workspace `slug`'s pins set to `pins`: the latest workspace first. */
211export function writeDock(value: string | null | undefined, slug: string, pins: PinnableApp[]): string {
212 const dock = readDock(value);
213 const key = slug.toLowerCase();
214 delete dock[key];
215 const entries = [[key, [...new Set(pins.filter(isPinnable))].slice(0, MAX_APP_PINS)] as const, ...Object.entries(dock)].slice(0, MAX_WORKSPACES);
216 return entries.map(([name, keys]) => `${name}:${keys.join(".")}`).join(",");
217}
218
219/** One change to someone's pins, as the pin buttons post it; null when it asks for nothing that makes sense. */
220export function appPinFromForm(form: FormData): { app: PinnableApp; pinned: boolean } | null {
221 const app = String(form.get("app") ?? "");
222 const intent = form.get("intent");
223 if (!isPinnable(app) || (intent !== "pin" && intent !== "unpin")) return null;
224 return { app, pinned: intent === "pin" };
225}
226
227/** The pins after pinning or unpinning `app`: a new pin goes at the end. */
228export function withPin(pins: PinnableApp[], app: PinnableApp, pinned: boolean): PinnableApp[] {
229 const rest = pins.filter((key) => key !== app);
230 return pinned ? [...rest, app] : rest;
231}
232
233/** The Set-Cookie header that keeps the rail, for a year. */
234export function dockCookie(value: string, secure: boolean): string {
235 return `${DOCK_COOKIE}=${encodeURIComponent(value)}; Path=/; Max-Age=31536000; SameSite=Lax${secure ? "; Secure" : ""}`;
236}
237
238/** The cookie that remembers whether the in-context sidebar is folded away on a computer. */
239export const SIDEBAR_COOKIE = "g1t_sidebar";
240
241/** Whether the sidebar is folded away, from the cookie's value. */
242export function sidebarClosed(value: string | null | undefined): boolean {
243 return value === "closed";
244}
245
246/** The cookie, as the page sets it when the sidebar is folded away or brought back. */
247export function sidebarCookie(closed: boolean, secure: boolean): string {
248 return `${SIDEBAR_COOKIE}=${closed ? "closed" : "open"}; Path=/; Max-Age=31536000; SameSite=Lax${secure ? "; Secure" : ""}`;
249}