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