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.

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