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.

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