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.

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