fndn/ui

Showcase

Issue tracker

An issue tracker: projects in a sidebar, search on Ctrl/⌘ K, a filtered list with bulk actions, an issue panel with its fields and comments, and settings with a danger zone.

Source

Only the system's components and token utilities; the layout is Tailwind.

showcases/web-app/App.tsx
1/* An issue tracker for the Northwind team: a sidebar with the inbox, the issue views and the
2 * projects; a top bar with the breadcrumb, search (Ctrl/⌘ K) and the account menu; the issue list
3 * with filters and bulk actions; an issue panel beside the list (or instead of it when the window is
4 * narrow); project settings with a danger zone; toasts for what happened. All state lives here,
5 * the views are in Issues.tsx, IssueDetail.tsx, Inbox.tsx, Settings.tsx and NewIssue.tsx.
6 *
7 * Keyboard: Ctrl/⌘ K searches issues and commands, C creates an issue. Both are bound on the
8 * app's root, so they work while the app has focus and leave the page's own keys alone.
9 * Below 720px the app shows one view at a time, picked at the bottom.
10 *
11 * A rounded app: the page that hosts it has <html data-shape="rounded"> (index.json tells the
12 * docs site), so no button, toggle or switcher here names a shape, and its dialogs follow. */
13import { useMemo, useRef, useState, type KeyboardEvent, type MouseEvent } from "react";
14import { Bell, CheckCircle, Circle, Cube, Gear, ListChecks, MagnifyingGlass, Plus, SignOut, User, UserPlus } from "@phosphor-icons/react";
15import { Avatar, Breadcrumb, Button, CommandPalette, Kbd, LoadingState, Menu, Select, SegmentedControl, SidebarNav, Splitter, Toast, toast, type CommandPaletteItem, type MenuEntry } from "@fndn/react";
16import { ISSUES, ME, NOTIFICATIONS, PROJECTS, TODAY, personOf, statusOf, type Issue } from "./data";
17import { Inbox } from "./Inbox";
18import { IssueDetail, describe, type IssueChange } from "./IssueDetail";
19import { Issues, NO_FILTER, type Bulk } from "./Issues";
20import { NewIssue, type Draft } from "./NewIssue";
21import { Settings } from "./Settings";
22import { useWidth } from "./useWidth";
23
24/** inbox · my issues · all issues · one project's issues · that project's settings */
25type View = "inbox" | "mine" | "all" | "project" | "settings";
26const mod = typeof navigator !== "undefined" && /Mac|iPhone|iPad/.test(navigator.userAgent) ? "⌘" : "Ctrl";
27const issues = (n: number) => (n === 1 ? "1 issue" : `${n} issues`);
28
29const ACCOUNT_MENU: MenuEntry[] = [
30 { type: "group", label: `${ME.label} · ${ME.email}`, items: [
31 { value: "mine", label: "My issues", icon: <User data-slot="icon" weight="bold" /> },
32 { value: "settings", label: "Project settings", icon: <Gear data-slot="icon" weight="bold" /> },
33 { value: "invite", label: "Invite teammates", icon: <UserPlus data-slot="icon" weight="bold" /> },
34 ] },
35 { type: "separator" },
36 { value: "sign-out", label: "Sign out", icon: <SignOut data-slot="icon" weight="bold" /> },
37];
38
39export default function App() {
40 const [root, width] = useWidth();
41 const phone = width > 0 && width < 720;
42 /* the issue panel sits beside the list only when both have room (the list keeps its titles
43 * readable); narrower, the issue takes the list's place at a reading width, as Linear's */
44 const split = width >= 1100;
45
46 const [all, setAll] = useState<Issue[]>(ISSUES);
47 const [projects, setProjects] = useState(PROJECTS);
48 const [inbox, setInbox] = useState(NOTIFICATIONS);
49 const [note, setNote] = useState<string | null>(null);
50 const [view, setView] = useState<View>("project");
51 const [project, setProject] = useState("web-app");
52 const [openId, setOpenId] = useState<string | null>(null);
53 const [selected, setSelected] = useState<string[]>([]);
54 const [filter, setFilter] = useState(NO_FILTER);
55 const [loading, setLoading] = useState(false);
56 const [collapsed, setCollapsed] = useState(false);
57 const [palette, setPalette] = useState(false);
58 const [creating, setCreating] = useState(false);
59 const [sizes, setSizes] = useState([58, 42]);
60 const timer = useRef<ReturnType<typeof setTimeout>>(undefined);
61
62 const current = projects.find((p) => p.value === project) ?? projects[0];
63 const open = all.find((i) => i.id === openId) ?? null;
64 const scoped = useMemo(
65 () => (view === "mine" ? all.filter((i) => i.assignee === ME.value) : view === "all" ? all : all.filter((i) => i.project === project)),
66 [all, view, project],
67 );
68
69 /* ── navigation ─────────────────────────────────────────── */
70 const go = (to: View, p = project) => {
71 if (to !== view || p !== project) setFilter(NO_FILTER);
72 /* wide, the inbox opens on the newest notification (Linear's), which is then read */
73 if (to === "inbox" && view !== "inbox" && split && !note && inbox[0]) pickNote(inbox[0].id);
74 setView(to);
75 setSelected([]);
76 if (to !== "project" || p !== project) setOpenId(null);
77 /* another project's issues "load" for a moment; the views already on screen show at once */
78 if (to === "project" && p !== project) {
79 clearTimeout(timer.current);
80 setLoading(true);
81 timer.current = setTimeout(() => setLoading(false), 500);
82 }
83 setProject(p);
84 };
85 const openIssue = (id: string) => {
86 const issue = all.find((i) => i.id === id);
87 if (!issue) return;
88 /* from the inbox or settings, or for another project's issue: that project's list */
89 if (view === "inbox" || view === "settings" || (view === "project" && issue.project !== project)) go("project", issue.project);
90 setOpenId(id);
91 };
92
93 /* ── changes ────────────────────────────────────────────── */
94 const now = () => "just now";
95 const event = (text: string) => ({ id: `e${Date.now()}${Math.random()}`, who: ME.value, when: now(), kind: "event" as const, text });
96 const change = (id: string, patch: IssueChange) => {
97 const text = describe(patch);
98 setAll((list) => list.map((i) => (i.id === id ? { ...i, ...patch, activity: text ? [...i.activity, event(text)] : i.activity } : i)));
99 };
100 const comment = (id: string, body: string) =>
101 setAll((list) => list.map((i) => (i.id === id ? { ...i, activity: [...i.activity, { id: `m${Date.now()}`, who: ME.value, when: now(), kind: "comment", body }] } : i)));
102
103 const bulk = (ids: string[], what: Bulk) => {
104 const before = all;
105 if ("remove" in what) {
106 setAll((list) => list.filter((i) => !ids.includes(i.id)));
107 if (openId && ids.includes(openId)) setOpenId(null);
108 toast({ title: `${issues(ids.length)} deleted`, action: { label: "Undo", onClick: () => setAll(before) } });
109 return;
110 }
111 ids.forEach((id) => change(id, what));
112 const to = "status" in what ? `moved to ${statusOf(what.status).label}` : what.assignee ? `assigned to ${what.assignee === ME.value ? "you" : personOf(what.assignee)?.label}` : "unassigned";
113 toast({ title: `${issues(ids.length)} ${to}`, tone: "green", duration: 3000, action: { label: "Undo", onClick: () => setAll(before) } });
114 };
115
116 const create = (draft: Draft) => {
117 const key = projects.find((p) => p.value === draft.project)?.key ?? "WEB";
118 const next = Math.max(0, ...all.filter((i) => i.id.startsWith(`${key}-`)).map((i) => Number(i.id.split("-")[1]))) + 1;
119 const id = `${key}-${next}`;
120 setAll((list) => [{ ...draft, id, due: null, labels: [], activity: [{ id: `c${id}`, who: ME.value, when: now(), kind: "event", text: "created the issue" }] }, ...list]);
121 setCreating(false);
122 if (view === "project" && draft.project !== project) go("project", draft.project);
123 setOpenId(id);
124 toast({ title: `${id} created`, description: draft.title, tone: "green", duration: 4000 });
125 };
126
127 const removeProject = (archived: boolean) => {
128 const gone = current!;
129 const rest = projects.filter((p) => p.value !== gone.value);
130 setProjects(rest);
131 if (!archived) setAll((list) => list.filter((i) => i.project !== gone.value));
132 go("all", rest[0]?.value ?? "");
133 toast(archived
134 ? { title: `${gone.label} archived`, description: "Restore it from workspace settings.", action: { label: "Undo", onClick: () => setProjects(projects) } }
135 : { title: `${gone.label} deleted`, tone: "red" });
136 };
137
138 /* ── commands: the palette and the shortcuts ─────────────── */
139 /* the system's command palette (Linear's ⌘K): the first row starts highlighted, so Enter runs it;
140 * rows are listed under their group, each with its icon; an issue shows its key as meta */
141 const commands = useMemo((): CommandPaletteItem[] => [
142 { value: "new", label: "Create issue", group: "Actions", shortcut: ["C"], icon: <Plus data-slot="icon" weight="bold" /> },
143 { value: "go:inbox", label: "Go to inbox", group: "Navigation", icon: <Bell data-slot="icon" weight="bold" /> },
144 { value: "go:mine", label: "Go to my issues", group: "Navigation", icon: <User data-slot="icon" weight="bold" /> },
145 { value: "go:settings", label: "Open project settings", group: "Navigation", icon: <Gear data-slot="icon" weight="bold" /> },
146 ...projects.map((p) => ({ value: `project:${p.value}`, label: `Open ${p.label}`, group: "Projects", icon: <Cube data-slot="icon" weight="bold" /> })),
147 ...all.map((i) => ({
148 value: `issue:${i.id}`, label: i.title, meta: i.id, group: "Issues", keywords: [i.id],
149 icon: i.status === "done" ? <CheckCircle data-slot="icon" weight="bold" /> : <Circle data-slot="icon" weight="bold" />,
150 })),
151 ], [all, projects]);
152 const run = (value: string) => {
153 if (value === "new") setCreating(true);
154 else if (value.startsWith("go:")) go(value.slice(3) as View);
155 else if (value.startsWith("project:")) go("project", value.slice(8));
156 else if (value.startsWith("issue:")) openIssue(value.slice(6));
157 };
158 const account = (value: string) => {
159 if (value === "mine" || value === "settings") go(value);
160 else if (value === "invite") toast({ title: "Invite link copied", description: "Anyone with it can join Northwind as a member for 7 days.", duration: 4000 });
161 else if (value === "sign-out") toast({ title: "Signing out is turned off in this demo" });
162 };
163 const typing = (el: Element) => el.closest("input, textarea, [contenteditable=true], [role=combobox], [role=dialog], [role=alertdialog]");
164 const onKeyDown = (e: KeyboardEvent) => {
165 if ((e.ctrlKey || e.metaKey) && !e.altKey && e.key.toLowerCase() === "k") { e.preventDefault(); e.stopPropagation(); setPalette(true); return; }
166 if (e.key === "c" && !e.ctrlKey && !e.metaKey && !e.altKey && !typing(e.target as Element)) { e.preventDefault(); setCreating(true); }
167 };
168 /* the sidebar's links are the app's routes: onNavigate keeps the page where it is and the app
169 * follows the link in its own state */
170 const navigate = (value: string, _href: string, e: MouseEvent<HTMLAnchorElement>) => {
171 e.preventDefault();
172 if (projects.some((p) => p.value === value)) go("project", value); else go(value as View);
173 };
174 const unread = inbox.filter((n) => !n.read).length;
175
176 /* ── the views ──────────────────────────────────────────── */
177 const sidebarCurrent = view === "project" ? project : view;
178 const sidebar = (
179 <div className="flex h-full shrink-0 border-e border-line bg-surface pt-1">
180 <SidebarNav
181 size="sm" label="Northwind" logo={<ListChecks data-slot="icon" weight="bold" />}
182 workspaces={[{ value: "northwind", label: "Northwind" }]} workspace="northwind"
183 actions={[{ value: "invite", label: "Invite teammates", icon: "user-plus" }, { value: "settings", label: "Project settings", icon: "gear" }]}
184 onWorkspaceAction={account}
185 items={[
186 { value: "inbox", label: "Inbox", href: "#inbox", icon: "bell", badge: unread ? String(unread) : undefined },
187 { value: "mine", label: "My issues", href: "#mine", icon: <User data-slot="icon" weight="bold" /> },
188 { value: "all", label: "All issues", href: "#all", icon: <ListChecks data-slot="icon" weight="bold" /> },
189 { value: "settings", label: "Project settings", href: "#settings", icon: "gear" },
190 ]}
191 sections={[{ value: "projects", label: "Projects", items: projects.map((p) => ({ value: p.value, label: p.label, href: `#${p.value}`, icon: "cube" })) }]}
192 current={sidebarCurrent}
193 onNavigate={navigate}
194 collapsed={collapsed} onCollapsedChange={setCollapsed}
195 />
196 </div>
197 );
198
199 const scopeName = view === "mine" ? "My issues" : view === "all" ? "All issues" : current?.label ?? "Northwind";
200 const projectPicker = phone && view === "project" && (
201 <Select size="sm" variant="ghost" className="font-semibold" aria-label="Project" items={projects} value={project} onValueChange={(v) => v && go("project", v)} />
202 );
203 const list = loading ? (
204 <div className="grid h-full place-items-center"><LoadingState variant="dots" label={`Loading ${scopeName} issues`} /></div>
205 ) : (
206 <Issues
207 key={`${view}:${project}`} issues={scoped} scope={scopeName} filter={filter} onFilterChange={setFilter} openId={openId} onOpen={setOpenId}
208 selected={selected} onSelectedChange={setSelected} onBulk={bulk} onNew={() => setCreating(true)} phone={phone} start={projectPicker}
209 />
210 );
211 const detail = open && (
212 <IssueDetail
213 issue={open} replaces={!split} onClose={() => setOpenId(null)}
214 onChange={(patch) => change(open.id, patch)} onComment={(body) => comment(open.id, body)}
215 />
216 );
217 /* side by side, the splitter stays mounted (the list keeps its scroll); a closed panel has no size */
218 const issueViews = !split ? (open ? detail : list) : (
219 <Splitter
220 className="h-full" handleLabel="Resize the issue panel" size={open ? sizes : [100, 0]}
221 onSizeChange={(s) => { if (!open) return; if (s[1] === 0) setOpenId(null); else setSizes(s); }}
222 panels={[{ id: "list", minSize: 40, content: list }, { id: "issue", minSize: 30, maxSize: 55, collapsible: true, content: detail || null }]}
223 />
224 );
225 /* the inbox: wide, the notification's issue beside the list; narrow, the issue in its project */
226 const noteIssue = all.find((i) => i.id === inbox.find((n) => n.id === note)?.issue) ?? null;
227 const pickNote = (id: string) => {
228 const n = inbox.find((x) => x.id === id);
229 if (!n) return;
230 setInbox((list) => list.map((x) => (x.id === id ? { ...x, read: true } : x)));
231 if (split) setNote(id); else openIssue(n.issue);
232 };
233 const main =
234 view === "inbox" ? (
235 <Inbox
236 items={inbox} issues={all} current={note} onSelect={pickNote} split={split}
237 onMarkAllRead={() => setInbox((list) => list.map((n) => ({ ...n, read: true })))}
238 preview={noteIssue && (
239 <IssueDetail
240 issue={noteIssue} replaces={false} onClose={() => setNote(null)}
241 onChange={(patch) => change(noteIssue.id, patch)} onComment={(body) => comment(noteIssue.id, body)}
242 />
243 )}
244 />
245 )
246 : view === "settings" && current ? (
247 <Settings
248 key={current.value} project={current} issueCount={all.filter((i) => i.project === current.value).length}
249 onRename={(label, key) => setProjects((ps) => ps.map((p) => (p.value === current.value ? { ...p, label, key } : p)))}
250 onArchive={() => removeProject(true)} onDelete={() => removeProject(false)}
251 />
252 ) : issueViews;
253
254 const crumbs = [
255 { label: "Northwind", href: "#all" },
256 ...(view === "project" || view === "settings" ? [{ label: current?.label ?? "", href: "#project" }] : []),
257 { label: view === "inbox" ? "Inbox" : view === "settings" ? "Settings" : view === "mine" ? "My issues" : view === "all" ? "All issues" : "Issues", href: open ? "#list" : undefined },
258 ...(open && view !== "inbox" && view !== "settings" ? [{ label: open.id }] : []),
259 ];
260 const crumb = (href: string | undefined) => {
261 if (href === "#all") go("all");
262 else if (href === "#project") go("project");
263 else if (href === "#list") setOpenId(null);
264 };
265
266 const header = (
267 <header className="flex h-12 shrink-0 items-center gap-3 border-b border-line px-4 max-sm:gap-1.5 max-sm:ps-3 max-sm:pe-2">
268 <Breadcrumb className="min-w-0 flex-1" label="You are here" items={crumbs} maxItems={phone ? 2 : undefined} onNavigate={crumb} />
269 {phone ? (
270 <>
271 <Button variant="ghost" size="md" square muted aria-label="Search issues and commands" icon={<MagnifyingGlass data-slot="icon" weight="bold" />} onClick={() => setPalette(true)} />
272 <Button variant="ghost" size="md" square muted aria-label="New issue" icon={<Plus data-slot="icon" weight="bold" />} onClick={() => setCreating(true)} />
273 </>
274 ) : (
275 <span className="relative flex w-64 max-w-[40%]">
276 <Button variant="outline" size="sm" className="w-full justify-start pe-16 text-ink-3" icon={<MagnifyingGlass data-slot="icon" weight="bold" />} aria-keyshortcuts="Control+K Meta+K" onClick={() => setPalette(true)} data-app="search">
277 Search issues
278 </Button>
279 <span className="pointer-events-none absolute inset-y-0 end-1.5 flex items-center" aria-hidden><Kbd keys={[mod, "K"]} /></span>
280 </span>
281 )}
282 <Menu items={ACCOUNT_MENU} placement="bottom-end" onSelect={account}>
283 <Button variant="ghost" size={phone ? "md" : "sm"} square aria-label={`Account: ${ME.label}`} icon={<Avatar name={ME.label} color={ME.color} size="20" />} data-app="account" />
284 </Menu>
285 </header>
286 );
287
288 const phoneNav = (
289 <nav aria-label="Views" className="shrink-0 border-t border-line p-2">
290 <SegmentedControl
291 fill size="md" className="w-full" aria-label="View" value={view === "project" ? "issues" : view === "all" ? "issues" : view}
292 onValueChange={(v) => go(v === "issues" ? "project" : (v as View))}
293 options={[
294 { value: "inbox", label: "Inbox", count: unread || undefined },
295 { value: "issues", label: "Issues" },
296 { value: "mine", label: "Mine" },
297 { value: "settings", label: "Settings" },
298 ]}
299 />
300 </nav>
301 );
302
303 return (
304 <div
305 ref={root} tabIndex={-1} onKeyDown={onKeyDown} data-showcase="web-app" data-layout={phone ? "phone" : split ? "split" : "single"} data-today={TODAY}
306 className="flex h-full bg-page text-ink outline-none"
307 >
308 {!phone && sidebar}
309 <div className="flex min-w-0 flex-1 flex-col">
310 {header}
311 <main className="min-h-0 flex-1">{main}</main>
312 {phone && phoneNav}
313 </div>
314
315 <CommandPalette
316 items={commands} limit={8} hotkey={false} open={palette} onOpenChange={setPalette} onSelect={run}
317 title="Search" label="Search issues and commands" placeholder="Search issues, projects and commands…"
318 emptyTitle="Nothing matches" emptyHint="Try an issue number such as WEB-482, or a word from its title."
319 />
320 <NewIssue open={creating} onOpenChange={setCreating} project={view === "project" ? project : "web-app"} onCreate={create} />
321 <Toast />
322 </div>
323 );
324}