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