Skip to content
241 linesCodeBlameRaw
1import { Bell, Building2, Code2, LayoutGrid, Lock, MessagesSquare, Pin, Search, Shapes, Sparkles, Store, House, 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 { TierBadge } from "./marketplace";
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 agents: <Sparkles size={size} />,
19 code: <Code2 size={size} />,
20 artifacts: <Shapes 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, until extensions bring their own. */
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 dock 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 dock, 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 * dock 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-center gap-2">
100 <span className="truncate text-sm font-medium text-fg">{app.name}</span>
101 <TierBadge tier={app.tier} focusable={false} />
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 {app.usable ? <TierBadge tier={app.tier} focusable={false} className="px-1 text-[0.5625rem] leading-3.5" /> : <span className="block truncate text-[0.625rem] text-accent">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 && (
129 <Hint label={pinned ? "Unpin from your dock" : "Pin to your dock"}>
130 <button
131 type="button"
132 aria-label={pinned ? `Unpin ${app.name} from your dock` : `Pin ${app.name} to your dock`}
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 the Marketplace. */
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 <Store 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 come from the Marketplace: integrations your workspace connects, and extensions later. Each one you can use shows here, to pin to your dock.</p>
158 <Link to={`/${slug}/-/marketplace`} 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 <Store size={14} />
160 Browse the Marketplace
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 the Marketplace.
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 return (
183 <div className="flex flex-col gap-2">
184 {installed == null && loader.data ? (
185 <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>
186 ) : installed == null ? (
187 <div className="grid grid-cols-4 gap-1 p-1 max-sm:grid-cols-3" aria-busy="true" aria-label="Loading apps">
188 {Array.from({ length: 4 }, (_, n) => (
189 <div key={n} className="flex flex-col items-center gap-2 px-1 pt-3 pb-2.5">
190 <Skeleton className="size-10 rounded-xl" />
191 <Skeleton className="h-2.5 w-12" />
192 </div>
193 ))}
194 </div>
195 ) : installed.length === 0 ? (
196 <NoApps slug={slug} onOpen={onClose} compact />
197 ) : (
198 <>
199 <label className="flex h-9 items-center gap-2 rounded-lg border border-line bg-bg px-2.5 text-sm focus-within:border-line-strong">
200 <Search size={15} className="shrink-0 text-faint" />
201 <input
202 value={query}
203 onChange={(event) => setQuery(event.target.value)}
204 placeholder="Find an app"
205 aria-label="Find an app"
206 autoComplete="off"
207 className="min-w-0 grow bg-transparent outline-none placeholder:text-faint"
208 />
209 </label>
210 {apps.length === 0 ? (
211 <p className="px-2 py-6 text-center text-sm text-muted">No installed app matches.</p>
212 ) : (
213 <div className="grid max-h-[min(23.5rem,60dvh)] grid-cols-4 gap-1 overflow-y-auto max-sm:grid-cols-3">
214 {apps.map((app) => (
215 <AppTile key={app.key} app={app} pinned={pins.includes(app.key)} onToggle={onToggle} onOpen={onClose} />
216 ))}
217 </div>
218 )}
219 </>
220 )}
221 <div className="flex items-center justify-between border-t border-line pt-2">
222 <Link
223 to={`/${slug}/-/apps`}
224 onClick={onClose}
225 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"
226 >
227 <LayoutGrid size={14} />
228 All apps
229 </Link>
230 <Link
231 to={`/${slug}/-/marketplace`}
232 onClick={onClose}
233 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"
234 >
235 <Store size={14} />
236 Marketplace
237 </Link>
238 </div>
239 </div>
240 );
241}