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.

246 linesCodeBlameRaw
1import { Bell, Building2, Code2, House, LayoutGrid, Lock, MessagesSquare, Pin, Plug, Repeat, Search, Shapes, Users } from "lucide-react";
2import { type ReactNode, useEffect, useMemo, useState } from "react";
3import { Link, useFetcher, useFetchers } from "react-router";
4
5import { ConnectorMark } from "./connectors";
6import { Card } from "./ui/card";
7import { Skeleton } from "./ui/skeleton";
8import { type BuiltinApp, type InstalledAppData, type PinnableApp, appPinFromForm, installedAppOf, withPin } from "../lib/apps";
9import { cn } from "../lib/cn";
10import { Hint } from "./ui/hint";
11
12/** Each built-in app's icon, at the size asked for. */
13export function appIcon(key: BuiltinApp, size = 18): ReactNode {
14 const icons: Record<BuiltinApp, ReactNode> = {
15 home: <House size={size} />,
16 chat: <MessagesSquare size={size} />,
17 notifications: <Bell size={size} />,
18 code: <Code2 size={size} />,
19 artifacts: <Shapes size={size} />,
20 routines: <Repeat size={size} />,
21 people: <Users size={size} />,
22 workspace: <Building2 size={size} />,
23 };
24 return icons[key];
25}
26
27/** An installed app's mark: its connector's, or its letters on a steady colour. */
28export function InstalledMark({ app, size = 22 }: { app: { connector: InstalledAppData["connector"] }; size?: number }) {
29 return <ConnectorMark view={{ id: app.connector.id, name: app.connector.name, provider: app.connector.provider ?? null }} size={size} />;
30}
31
32/** A pinned app as the rail draws it, from its key alone; null when the key names nothing installable. */
33export function pinnedApp(key: PinnableApp, slug: string): { key: string; name: string; to: string; icon: (size: number) => ReactNode } | null {
34 const app = installedAppOf(key);
35 if (!app) return null;
36 return { key, name: app.name, to: app.path(slug), icon: (size) => <InstalledMark app={app} size={size} /> };
37}
38
39/** Every pin form shares this key, so the rail, the launcher and the Apps page see one change. */
40const PIN_FETCHER = "app-pin";
41
42/**
43 * Someone's pins in the workspace `slug`, as saved (`saved`, from the
44 * root's data), with any pin or unpin on its way already applied, so the
45 * rail changes the moment a pin is pressed. `toggle` pins or unpins.
46 */
47export function useAppPins(slug: string, saved: PinnableApp[]) {
48 const fetcher = useFetcher({ key: PIN_FETCHER });
49 const fetchers = useFetchers();
50 const pins = useMemo(() => {
51 let pins = saved;
52 for (const pending of fetchers) {
53 if (pending.key !== PIN_FETCHER || !pending.formData) continue;
54 const change = appPinFromForm(pending.formData);
55 if (change) pins = withPin(pins, change.app, change.pinned);
56 }
57 return pins;
58 }, [saved, fetchers]);
59 const toggle = (app: PinnableApp) => {
60 const form = new FormData();
61 form.set("intent", pins.includes(app) ? "unpin" : "pin");
62 form.set("app", app);
63 fetcher.submit(form, { method: "post", action: `/${slug}/-/apps` });
64 };
65 return { pins, toggle };
66}
67
68/**
69 * One installed app: its mark and name, opening it; and a pin that shows
70 * on hover or focus and stays lit while it is pinned. An app the person
71 * can't use says Request access instead, and has no pin.
72 */
73export function AppTile({
74 app,
75 pinned,
76 onToggle,
77 onOpen,
78 card = false,
79}: {
80 app: InstalledAppData;
81 pinned: boolean;
82 onToggle: (app: PinnableApp) => void;
83 onOpen?: () => void;
84 /** On the Apps page: a card with what the app is. */
85 card?: boolean;
86}) {
87 const body = (
88 <>
89 <span className="relative flex shrink-0">
90 <InstalledMark app={app} size={40} />
91 {!app.usable && (
92 <span className="absolute -right-1 -bottom-1 flex size-4 items-center justify-center rounded-full bg-surface text-faint ring-1 ring-line">
93 <Lock size={9} />
94 </span>
95 )}
96 </span>
97 {card ? (
98 <span className="min-w-0">
99 <span className="flex min-w-0 items-baseline gap-2">
100 <span className="truncate text-sm font-medium text-fg">{app.name}</span>
101 <span className="shrink-0 text-[0.6875rem] text-faint">{app.from}</span>
102 </span>
103 <span className="block truncate text-xs text-muted">{app.usable ? app.about : "Request access"}</span>
104 </span>
105 ) : (
106 <span className="flex w-full min-w-0 flex-col items-center gap-1">
107 <span className="block w-full truncate text-xs font-medium text-fg-soft">{app.name}</span>
108 <span className={cn("block truncate text-[0.625rem]", app.usable ? "text-faint" : "text-accent")}>{app.usable ? app.from : "Request access"}</span>
109 </span>
110 )}
111 </>
112 );
113 const linkClass = cn(
114 "flex w-full rounded-xl text-center outline-none focus-visible:ring-2 focus-visible:ring-accent",
115 card ? "items-center gap-3 p-3.5 pr-11 text-left" : "flex-col items-center gap-1.5 px-1 pt-3 pb-2.5",
116 );
117 return (
118 <div
119 className={cn(
120 "group/tile relative rounded-xl transition-colors",
121 card ? "border border-line bg-surface hover:border-line-strong" : "hover:bg-raised focus-within:bg-raised",
122 )}
123 >
124 {/* An app the person can't use leads (`href`) to where they ask for it. */}
125 <Link to={app.href} prefetch="intent" onClick={onOpen} className={linkClass}>
126 {body}
127 </Link>
128 {app.usable && app.pinnable && (
129 <Hint label={pinned ? "Unpin from your rail" : "Pin to your rail"}>
130 <button
131 type="button"
132 aria-label={pinned ? `Unpin ${app.name} from your rail` : `Pin ${app.name} to your rail`}
133 aria-pressed={pinned}
134 onClick={() => onToggle(app.key)}
135 className={cn(
136 "absolute flex size-6 items-center justify-center rounded-md outline-none transition-opacity hover:bg-line focus-visible:opacity-100 focus-visible:ring-2 focus-visible:ring-accent [@media(hover:none)]:opacity-100",
137 card ? "top-1/2 right-2.5 -translate-y-1/2" : "top-1 right-1",
138 pinned ? "text-accent opacity-100" : "text-faint opacity-0 group-hover/tile:opacity-100 hover:text-fg",
139 )}
140 >
141 <Pin size={13} className={pinned ? "fill-current" : undefined} />
142 </button>
143 </Hint>
144 )}
145 </div>
146 );
147}
148
149/** When nothing is installed: what Apps are, and the way to Integrations. */
150export function NoApps({ slug, onOpen, compact = false }: { slug: string; onOpen?: () => void; compact?: boolean }) {
151 return (
152 <div className={cn("flex flex-col items-center text-center", compact ? "px-4 py-6" : "rounded-xl border border-dashed border-line px-6 py-12")}>
153 <span className="flex size-10 items-center justify-center rounded-xl bg-raised text-muted ring-1 ring-line">
154 <Plug size={18} />
155 </span>
156 <p className="mt-3 text-sm font-medium">No apps installed yet</p>
157 <p className="mt-1 max-w-xs text-[0.8125rem] text-muted">Apps are the integrations your workspace connects. Each one you can use shows here.</p>
158 <Link to={`/${slug}/-/integrations`} onClick={onOpen} className="mt-4 inline-flex h-8 items-center gap-1.5 rounded-md bg-fg px-3 text-[0.8125rem] font-medium text-bg hover:bg-fg-hover">
159 <Plug size={14} />
160 Integrations
161 </Link>
162 </div>
163 );
164}
165
166/**
167 * The Apps launcher's body: a search, every app the workspace installed
168 * that the person can use (read from the Apps page as it opens), and the
169 * way to all of them and to Integrations.
170 */
171export function AppsLauncher({ slug, pins, onToggle, onClose }: { slug: string; pins: PinnableApp[]; onToggle: (app: PinnableApp) => void; onClose: () => void }) {
172 const [query, setQuery] = useState("");
173 const loader = useFetcher<{ apps: InstalledAppData[] | null }>();
174 useEffect(() => {
175 if (loader.state === "idle" && !loader.data) loader.load(`/${slug}/-/apps`);
176 // Once per opening: the launcher's body mounts when it opens.
177 // eslint-disable-next-line react-hooks/exhaustive-deps
178 }, [slug]);
179 const installed = loader.data?.apps;
180 const wanted = query.trim().toLowerCase();
181 const apps = (installed ?? []).filter((app) => !wanted || app.name.toLowerCase().includes(wanted) || app.about.toLowerCase().includes(wanted));
182 const loading = installed == null && !loader.data;
183 const failed = installed == null && !!loader.data;
184 return (
185 <div className="flex flex-col gap-2">
186 {/* The search is there from the first frame, so the launcher keeps its shape while the apps are read. */}
187 {(loading || (installed && installed.length > 0)) && (
188 <Card asChild tone="bg" radius="lg" className="flex h-9 items-center gap-2 px-2.5 text-sm focus-within:border-line-strong">
189 <label>
190 <Search size={15} className="shrink-0 text-faint" />
191 <input
192 value={query}
193 onChange={(event) => setQuery(event.target.value)}
194 placeholder="Find an app"
195 aria-label="Find an app"
196 autoComplete="off"
197 className="min-w-0 grow bg-transparent outline-none placeholder:text-faint"
198 />
199 </label>
200 </Card>
201 )}
202 {failed ? (
203 <p className="px-2 py-6 text-center text-sm text-muted">Apps can't be listed right now. Try again in a moment.</p>
204 ) : loading ? (
205 <div className="grid grid-cols-4 gap-1 max-sm:grid-cols-3" aria-busy="true" aria-label="Loading apps">
206 {Array.from({ length: 4 }, (_, n) => (
207 <div key={n} className="flex flex-col items-center gap-2 px-1 pt-3 pb-2.5">
208 {/* On the launcher's raised surface the usual raised block would vanish: a line-coloured one shows. */}
209 <Skeleton className="size-10 rounded-xl bg-line" />
210 <Skeleton className="h-2.5 w-12 bg-line" />
211 <Skeleton className="h-2 w-8 bg-line" />
212 </div>
213 ))}
214 </div>
215 ) : installed!.length === 0 ? (
216 <NoApps slug={slug} onOpen={onClose} compact />
217 ) : apps.length === 0 ? (
218 <p className="px-2 py-6 text-center text-sm text-muted">No installed app matches.</p>
219 ) : (
220 <div className="grid max-h-[min(23.5rem,60dvh)] grid-cols-4 gap-1 overflow-y-auto max-sm:grid-cols-3">
221 {apps.map((app) => (
222 <AppTile key={app.key} app={app} pinned={pins.includes(app.key)} onToggle={onToggle} onOpen={onClose} />
223 ))}
224 </div>
225 )}
226 <div className="flex items-center justify-between border-t border-line pt-2">
227 <Link
228 to={`/${slug}/-/apps`}
229 onClick={onClose}
230 className="flex h-8 items-center gap-1.5 rounded-md px-2 text-[0.8125rem] text-muted transition-colors hover:bg-line hover:text-fg"
231 >
232 <LayoutGrid size={14} />
233 All apps
234 </Link>
235 <Link
236 to={`/${slug}/-/integrations`}
237 onClick={onClose}
238 className="flex h-8 items-center gap-1.5 rounded-md px-2 text-[0.8125rem] text-muted transition-colors hover:bg-line hover:text-fg"
239 >
240 <Plug size={14} />
241 Integrations
242 </Link>
243 </div>
244 </div>
245 );
246}