Skip to content
279 linesCodeBlameRaw
1/**
2 * The rail's built-in apps, the apps a workspace added in Customize, and
3 * the ones each person pins to their rail.
4 *
5 * Built-in apps (Home, Chat, Notifications, Agents, Code, Artifacts,
6 * People and Workspace) are always in the rail; their own pages (Projects,
7 * Usage, Teams and the rest) are in their sidebars. Apps are what the
8 * workspace added in Customize: the plugins it installed (each opening its
9 * page), the connectors it connected, and the integrations it connected,
10 * each opening where it is looked after. Each person pins the ones they
11 * want.
12 *
13 * An installed app's key says what it is: `int-<connector>` for an
14 * integration (@g1t/contracts/connectors), `plg-<install>` for a plugin,
15 * `con-<connector>` for a connector. Only integrations are pinned: they
16 * are known from the catalog alone, so a pin can be read without asking a
17 * service. A key that names nothing pinnable (a built-in page pinned
18 * before Apps were Customize's, an extension from before plugins) is
19 * dropped wherever pins are read.
20 *
21 * Pins are each person's own, per workspace, in the order they set, and
22 * kept with their account by the identity service (`dock_pins`), so the
23 * dock is the same on every device (lib/dock.server.ts). A cookie
24 * (`g1t_dock`) keeps a copy on each device: the rail still draws from it
25 * when identity cannot be reached, and pins from before they were kept
26 * with the account carry over from it until the first change.
27 * No Workers or React imports, so it can be tested under Node.
28 */
29import type { PluginInstall, WorkspaceConnector } from "@g1t/contracts";
30import { CONNECTORS, type Connector, connectorPath } from "@g1t/contracts/connectors";
31
32import { connectorPagePath, pluginPath } from "./customize.ts";
33
34/** The apps that are always in the rail. */
35export type BuiltinApp = "home" | "chat" | "notifications" | "agents" | "code" | "artifacts" | "people" | "workspace";
36
37export type BuiltinInfo = {
38 key: BuiltinApp;
39 name: string;
40 /** Its page in the workspace `slug`. */
41 path: (slug: string) => string;
42 /** Part of Code: only for members with Code access. */
43 code?: boolean;
44};
45
46const under = (page: string) => (slug: string) => `/${slug}/-/${page}`;
47
48/** The rail's built-in apps, in its order. */
49export const BUILTINS: BuiltinInfo[] = [
50 { key: "home", name: "Home", path: under("home") },
51 { key: "chat", name: "Chat", path: under("chat") },
52 { key: "notifications", name: "Notifications", path: () => "/notifications" },
53 { key: "agents", name: "Agents", path: under("agents") },
54 { key: "code", name: "Code", path: under("overview"), code: true },
55 { key: "artifacts", name: "Artifacts", path: under("artifacts") },
56 { key: "people", name: "People", path: under("people") },
57 { key: "workspace", name: "Workspace", path: under("workspace") },
58];
59
60/** The built-in app `key`. */
61export function appOf(key: BuiltinApp): BuiltinInfo {
62 return BUILTINS.find((app) => app.key === key)!;
63}
64
65/** An installed app's key, as pins and forms carry it: `int-sentry`. */
66export type PinnableApp = string;
67
68/** An app the workspace added in Customize. */
69export type InstalledApp = {
70 key: PinnableApp;
71 name: string;
72 /** What it is, in a few words. */
73 about: string;
74 /** The listing it was added from: `integration:sentry`, `plugin:pdf@anthropic-agent-skills`, `connector:con_…`. */
75 listing: string;
76 /** Where it is from, in a word or two: g1t, its marketplace, or Connector. */
77 from: string;
78 /** What draws its mark: a connector's id and name, and its provider when it has one. */
79 connector: Pick<Connector, "id" | "name" | "provider">;
80 /** Its page in the workspace `slug`. */
81 path: (slug: string) => string;
82 /**
83 * Whether the person can open it. Every member can open an integration's
84 * page; an app limited to some people shows Request access to the rest.
85 */
86 usable: boolean;
87 /** Whether it can be pinned to the rail: only what the catalog alone knows. */
88 pinnable: boolean;
89};
90
91/** Where an integration is looked after: its setup page, by kind, or its own page. */
92const SECTION_BY_CATEGORY: Partial<Record<Connector["category"], string>> = { ai: "models", monitoring: "alerts", issues: "trackers" };
93
94function integrationPath(connector: Connector): (slug: string) => string {
95 const section = connector.provider ? SECTION_BY_CATEGORY[connector.category] : undefined;
96 if (section) return under(`integrations/${section}`);
97 const href = connector.href?.workspace;
98 return href ? (slug) => connectorPath(href, slug) : under("integrations");
99}
100
101/** Identity keeps keys of at most this many characters (MAX_DOCK_APP_KEY). */
102const MAX_KEY = 32;
103
104/** The installed-app key for an integration: `int-sentry`. */
105export function integrationAppKey(connectorId: string): PinnableApp {
106 return `int-${connectorId}`;
107}
108
109/**
110 * The app a key names, from the catalog alone (no service is asked), or
111 * null when it names nothing that can be installed: a retired key, a
112 * built-in page, or a connector that is not available for a workspace.
113 */
114export function installedAppOf(key: string): InstalledApp | null {
115 if (key.length > MAX_KEY || !/^int-[a-z0-9][a-z0-9-]*$/.test(key)) return null;
116 const connector = CONNECTORS.find((c) => c.id === key.slice(4));
117 if (!connector || connector.status !== "available" || !connector.scopes.includes("workspace")) return null;
118 return {
119 key,
120 name: connector.name,
121 about: connector.description,
122 listing: `integration:${connector.id}`,
123 from: "g1t",
124 connector: { id: connector.id, name: connector.name, provider: connector.provider },
125 path: integrationPath(connector),
126 usable: true,
127 pinnable: true,
128 };
129}
130
131/** An installed plugin as an app: its page in Customize. Switched off (the kill switch), it isn't an app until it is on again. */
132export function pluginApp(install: Pick<PluginInstall, "id" | "ref" | "name" | "display_name" | "description" | "marketplace" | "enabled">): InstalledApp | null {
133 if (!install.enabled) return null;
134 const name = install.display_name ?? install.name;
135 return {
136 key: `plg-${install.id}`,
137 name,
138 about: install.description,
139 listing: `plugin:${install.ref}`,
140 from: install.marketplace,
141 connector: { id: install.ref, name, provider: undefined },
142 path: (slug) => pluginPath(slug, install.ref),
143 usable: true,
144 pinnable: false,
145 };
146}
147
148/** A workspace connector as an app: its page in Customize. */
149export function connectorApp(connector: Pick<WorkspaceConnector, "id" | "title" | "about" | "url">): InstalledApp {
150 return {
151 key: `con-${connector.id}`,
152 name: connector.title,
153 about: connector.about ?? connector.url,
154 listing: `connector:${connector.id}`,
155 from: "Connector",
156 connector: { id: connector.id, name: connector.title, provider: undefined },
157 path: (slug) => connectorPagePath(slug, connector.id),
158 usable: true,
159 pinnable: false,
160 };
161}
162
163/** Whether `key` names an app that can be pinned. */
164export function isPinnable(key: string): key is PinnableApp {
165 return installedAppOf(key) != null;
166}
167
168/**
169 * The apps the workspace added: its plugins that are on, its connectors,
170 * and the integrations it has connected (by connector id,
171 * lib/connected.server.ts) in catalog order. `manage`, when a connection
172 * has one, is where its app opens.
173 */
174export function installedApps(
175 connected: Record<string, { manage: string | null }>,
176 installs: readonly Parameters<typeof pluginApp>[0][] = [],
177 connectors: readonly Parameters<typeof connectorApp>[0][] = [],
178): InstalledApp[] {
179 const integrations = CONNECTORS.filter((connector) => connected[connector.id]).map((connector) => {
180 const app = installedAppOf(integrationAppKey(connector.id));
181 const manage = connected[connector.id]?.manage;
182 return app && manage ? { ...app, path: () => manage } : app;
183 });
184 return [...installs.map(pluginApp), ...connectors.map(connectorApp), ...integrations].filter((app): app is InstalledApp => app != null);
185}
186
187/** An app as a loader sends it: everything but its path, which is worked out for the workspace. */
188export type InstalledAppData = Omit<InstalledApp, "path"> & { href: string };
189
190export function appData(app: InstalledApp, slug: string): InstalledAppData {
191 const { path, ...rest } = app;
192 return { ...rest, href: path(slug) };
193}
194
195/** The cookie that keeps this device's copy of each person's pins, per workspace. */
196export const DOCK_COOKIE = "g1t_dock";
197
198/**
199 * Most workspaces the cookie remembers pins for, and most pins in each.
200 * Identity keeps up to 24 (`MAX_DOCK_PINS`); this stays under it.
201 */
202const MAX_WORKSPACES = 20;
203export const MAX_APP_PINS = 12;
204
205/**
206 * Pins as identity keeps them, read against the apps there are: keys that
207 * are not an app people pin (one since retired, say) are dropped, repeats
208 * too, in the order they were set.
209 */
210export function pinsFrom(keys: readonly string[]): PinnableApp[] {
211 return [...new Set(keys.filter(isPinnable))].slice(0, MAX_APP_PINS);
212}
213
214/**
215 * The pins to draw in the workspace `slug`: the account's when identity
216 * has them (`stored`), else this device's cookie, for pins never saved to
217 * the account or when identity could not be asked.
218 */
219export function pinsToShow(stored: readonly string[] | null | undefined, cookie: string | null | undefined, slug: string): PinnableApp[] {
220 return stored ? pinsFrom(stored) : pinsIn(cookie, slug);
221}
222
223/** Every workspace's pins, from the cookie's value: `acme:projects.usage,beta:teams`. */
224export function readDock(value: string | null | undefined): Record<string, PinnableApp[]> {
225 const dock: Record<string, PinnableApp[]> = {};
226 for (const entry of (value ?? "").split(",")) {
227 const [slug, keys] = entry.split(":");
228 if (!slug || !/^[a-z0-9-]+$/.test(slug) || keys === undefined) continue;
229 const pins = [...new Set(keys.split(".").filter(isPinnable))].slice(0, MAX_APP_PINS);
230 dock[slug] = pins;
231 }
232 return dock;
233}
234
235/** The pins in the workspace `slug`, in the order they were pinned. */
236export function pinsIn(value: string | null | undefined, slug: string): PinnableApp[] {
237 return readDock(value)[slug.toLowerCase()] ?? [];
238}
239
240/** The cookie's value with the workspace `slug`'s pins set to `pins`: the latest workspace first. */
241export function writeDock(value: string | null | undefined, slug: string, pins: PinnableApp[]): string {
242 const dock = readDock(value);
243 const key = slug.toLowerCase();
244 delete dock[key];
245 const entries = [[key, [...new Set(pins.filter(isPinnable))].slice(0, MAX_APP_PINS)] as const, ...Object.entries(dock)].slice(0, MAX_WORKSPACES);
246 return entries.map(([name, keys]) => `${name}:${keys.join(".")}`).join(",");
247}
248
249/** One change to someone's pins, as the pin buttons post it; null when it asks for nothing that makes sense. */
250export function appPinFromForm(form: FormData): { app: PinnableApp; pinned: boolean } | null {
251 const app = String(form.get("app") ?? "");
252 const intent = form.get("intent");
253 if (!isPinnable(app) || (intent !== "pin" && intent !== "unpin")) return null;
254 return { app, pinned: intent === "pin" };
255}
256
257/** The pins after pinning or unpinning `app`: a new pin goes at the end. */
258export function withPin(pins: PinnableApp[], app: PinnableApp, pinned: boolean): PinnableApp[] {
259 const rest = pins.filter((key) => key !== app);
260 return pinned ? [...rest, app] : rest;
261}
262
263/** The Set-Cookie header that keeps the rail, for a year. */
264export function dockCookie(value: string, secure: boolean): string {
265 return `${DOCK_COOKIE}=${encodeURIComponent(value)}; Path=/; Max-Age=31536000; SameSite=Lax${secure ? "; Secure" : ""}`;
266}
267
268/** The cookie that remembers whether the in-context sidebar is folded away on a computer. */
269export const SIDEBAR_COOKIE = "g1t_sidebar";
270
271/** Whether the sidebar is folded away, from the cookie's value. */
272export function sidebarClosed(value: string | null | undefined): boolean {
273 return value === "closed";
274}
275
276/** The cookie, as the page sets it when the sidebar is folded away or brought back. */
277export function sidebarCookie(closed: boolean, secure: boolean): string {
278 return `${SIDEBAR_COOKIE}=${closed ? "closed" : "open"}; Path=/; Max-Age=31536000; SameSite=Lax${secure ? "; Secure" : ""}`;
279}