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.

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