517 lines19,766 bytesCodeBlame
1/**
2 * The pieces mission control and a project's overview are made of: panels
3 * with a "View all", what needs the viewer, the activity feed with its
4 * filters, the week's pulse with its sparklines, a project's pipeline and
5 * its deploy history.
6 */
7import {
8 ArrowRight,
9 Brain,
10 CircleCheck,
11 CircleDot,
12 CircleSlash,
13 CreditCard,
14 Eye,
15 GitMerge,
16 GitPullRequest,
17 Hand,
18 MessageCircleQuestion,
19 MessageSquare,
20 Play,
21 Rocket,
22 Swords,
23 Terminal,
24 TimerOff,
25 TriangleAlert,
26 UserPlus,
27} from "lucide-react";
28import { type ReactNode, useMemo, useState } from "react";
29import { Link } from "react-router";
30
31import type { DeployStatus } from "@g1t/contracts";
32
33import { cn } from "../lib/cn";
34import { type ActivityGroup, type Need, type NeedKind, type Verb, isAgent, sparkPoints } from "../lib/mission";
35import { Avatar, TimeAgo } from "./ui";
36import { Select, SelectContent, SelectItem, SelectTrigger, SelectValue } from "./ui/select";
37
38// --- Panels -------------------------------------------------------------------
39
40/** A titled section, with a count and a link to everything it summarises. */
41export function Panel({
42 id,
43 title,
44 icon,
45 count,
46 all,
47 extra,
48 children,
49 className,
50}: {
51 id?: string;
52 title: ReactNode;
53 icon?: ReactNode;
54 count?: number | null;
55 all?: { to: string; label?: string } | null;
56 extra?: ReactNode;
57 children: ReactNode;
58 className?: string;
59}) {
60 return (
61 <section id={id} className={cn("scroll-mt-20", className)}>
62 <div className="flex min-h-7 flex-wrap items-center gap-x-3 gap-y-2">
63 <h2 className="flex items-center gap-2 text-sm font-semibold tracking-tight">
64 {icon && <span className="text-faint">{icon}</span>}
65 {title}
66 {count != null && count > 0 && (
67 <span className="rounded-full bg-line px-1.5 text-[0.6875rem] font-medium tabular-nums text-muted">{count}</span>
68 )}
69 </h2>
70 <span className="grow" />
71 {extra}
72 {all && (
73 <Link to={all.to} prefetch="intent" className="inline-flex items-center gap-1 text-xs text-muted hover:text-fg">
74 {all.label ?? "View all"}
75 <ArrowRight size={12} />
76 </Link>
77 )}
78 </div>
79 <div className="mt-3">{children}</div>
80 </section>
81 );
82}
83
84/** Says a section could not be loaded, without taking the page down. */
85export function Unavailable({ what }: { what: string }) {
86 return (
87 <p className="flex items-center gap-2 rounded-xl border border-dashed border-line px-4 py-5 text-sm text-muted">
88 <TriangleAlert size={14} className="text-warn" />
89 {what} could not be loaded just now. It will be back on the next refresh.
90 </p>
91 );
92}
93
94/** A quiet, dashed empty state with an optional call to action. */
95export function Quiet({ children, action }: { children: ReactNode; action?: ReactNode }) {
96 return (
97 <div className="flex flex-wrap items-center gap-3 rounded-xl border border-dashed border-line px-4 py-5 text-sm text-muted">
98 <span className="min-w-0 grow">{children}</span>
99 {action}
100 </div>
101 );
102}
103
104// --- Needs you ----------------------------------------------------------------
105
106const NEED: Record<NeedKind, { icon: ReactNode; tone: string; label: string }> = {
107 limit: { icon: <CreditCard size={15} />, tone: "text-danger", label: "Usage" },
108 deploy: { icon: <Rocket size={15} />, tone: "text-danger", label: "Production" },
109 invitation: { icon: <UserPlus size={15} />, tone: "text-accent", label: "Invitation" },
110 conflict: { icon: <Swords size={15} />, tone: "text-warn", label: "Conflict" },
111 stalled: { icon: <Hand size={15} />, tone: "text-warn", label: "Stopped" },
112 stuck: { icon: <TimerOff size={15} />, tone: "text-warn", label: "Quiet agent" },
113 review: { icon: <Eye size={15} />, tone: "text-info", label: "Review" },
114 checks: { icon: <Terminal size={15} />, tone: "text-danger", label: "Checks" },
115 ready: { icon: <GitMerge size={15} />, tone: "text-accent", label: "Ready" },
116};
117
118/** What is waiting on the viewer, most urgent first, each with its next step. */
119export function NeedsList({ needs, limit = 8 }: { needs: Need[]; limit?: number }) {
120 const [all, setAll] = useState(false);
121 const shown = all ? needs : needs.slice(0, limit);
122 return (
123 <>
124 <ul className="divide-y divide-line overflow-hidden rounded-xl border border-line bg-surface">
125 {shown.map((need) => {
126 const look = NEED[need.kind];
127 return (
128 <li key={need.key} className="flex items-start gap-3 px-4 py-3">
129 <span className={cn("mt-0.5 shrink-0", look.tone)} title={look.label}>
130 {look.icon}
131 </span>
132 <span className="min-w-0 grow">
133 <Link to={need.to} prefetch="intent" className="block truncate text-sm font-medium hover:text-accent">
134 {need.title}
135 </Link>
136 <span className="mt-0.5 block text-xs leading-5 text-muted">
137 {need.where && <span className="font-mono text-faint">{need.where} · </span>}
138 {need.detail}
139 </span>
140 </span>
141 <span className="hidden shrink-0 pt-0.5 text-xs text-faint sm:block">
142 <TimeAgo at={need.at} />
143 </span>
144 <Link
145 to={need.to}
146 prefetch="intent"
147 className="shrink-0 rounded-md border border-line px-2.5 py-1 text-xs font-medium text-fg/85 transition-colors hover:border-line-strong hover:bg-raised hover:text-fg"
148 >
149 {need.action}
150 </Link>
151 </li>
152 );
153 })}
154 </ul>
155 {needs.length > limit && (
156 <button type="button" onClick={() => setAll(!all)} className="mt-2 text-xs text-muted hover:text-fg">
157 {all ? "Show fewer" : `Show ${needs.length - limit} more`}
158 </button>
159 )}
160 </>
161 );
162}
163
164// --- Activity -----------------------------------------------------------------
165
166const VERB: Record<Verb, { icon: ReactNode; tone: string; text: (n: string) => string }> = {
167 landed: { icon: <GitMerge size={14} />, tone: "text-merged", text: (n) => `landed ${n}` },
168 opened_issue: { icon: <CircleDot size={14} />, tone: "text-accent", text: (n) => `opened ${n}` },
169 closed_issue: { icon: <CircleSlash size={14} />, tone: "text-faint", text: (n) => `closed ${n}` },
170 started: { icon: <Play size={14} />, tone: "text-merged", text: (n) => `started on ${n}` },
171 ready: { icon: <GitPullRequest size={14} />, tone: "text-info", text: (n) => `marked ${n} ready for review` },
172 checks_passed: { icon: <Terminal size={14} />, tone: "text-accent", text: (n) => `checks passed on ${n}` },
173 checks_failed: { icon: <Terminal size={14} />, tone: "text-danger", text: (n) => `checks failed on ${n}` },
174 approved: { icon: <CircleCheck size={14} />, tone: "text-accent", text: (n) => `approved ${n}` },
175 changes_requested: { icon: <CircleSlash size={14} />, tone: "text-warn", text: (n) => `asked for changes on ${n}` },
176 commented: { icon: <MessageSquare size={14} />, tone: "text-muted", text: (n) => `commented on ${n}` },
177 asked: { icon: <MessageCircleQuestion size={14} />, tone: "text-merged", text: (n) => `asked the agent on ${n}` },
178 deployed: { icon: <Rocket size={14} />, tone: "text-accent", text: () => "deployed production" },
179 deploy_failed: { icon: <Rocket size={14} />, tone: "text-danger", text: () => "production build failed" },
180 learned: { icon: <Brain size={14} />, tone: "text-merged", text: () => "learned" },
181};
182
183function Refs({ numbers, base }: { numbers: number[]; base: string }) {
184 const shown = numbers.slice(0, 4);
185 return (
186 <>
187 {shown.map((number, index) => (
188 <span key={number}>
189 {index > 0 && (index === shown.length - 1 && numbers.length <= 4 ? " and " : ", ")}
190 <Link to={`${base}/issues/${number}`} prefetch="intent" className="font-medium text-fg hover:underline">
191 #{number}
192 </Link>
193 </span>
194 ))}
195 {numbers.length > 4 && ` and ${numbers.length - 4} more`}
196 </>
197 );
198}
199
200/** One group as a sentence: "landed #4 and #3; started on #5". */
201function GroupLine({ group }: { group: ActivityGroup }) {
202 const base = `/${group.repo.namespace}/${group.repo.name}`;
203 return (
204 <>
205 {group.parts.map((part, index) => {
206 const look = VERB[part.verb];
207 const [before, after] = look.text("\u0000").split("\u0000");
208 return (
209 <span key={part.verb}>
210 {index > 0 && "; "}
211 {part.verb === "learned" ? (
212 <>
213 learned{" "}
214 <Link to={part.to ?? `${base}/memory`} className="text-fg-soft hover:text-fg">
215 “{part.texts[0]}”
216 </Link>
217 {part.texts.length > 1 && ` and ${part.texts.length - 1} more`}
218 </>
219 ) : part.numbers.length > 0 ? (
220 <>
221 {before}
222 <Refs numbers={part.numbers} base={base} />
223 {after}
224 </>
225 ) : part.to ? (
226 <Link to={part.to} className="hover:text-fg">
227 {look.text("")}
228 </Link>
229 ) : (
230 look.text("")
231 )}
232 </span>
233 );
234 })}
235 </>
236 );
237}
238
239type Who = "all" | "agents" | "people";
240
241/**
242 * What moved across projects, newest first, a burst of one actor's work
243 * as one line, filterable by project and by who did it.
244 */
245export function ActivityFeed({
246 groups,
247 showRepo = true,
248 limit = 14,
249 empty,
250}: {
251 groups: ActivityGroup[];
252 showRepo?: boolean;
253 limit?: number;
254 empty: ReactNode;
255}) {
256 const [project, setProject] = useState("all");
257 const [who, setWho] = useState<Who>("all");
258 const [person, setPerson] = useState("all");
259 const [more, setMore] = useState(false);
260 const projects = useMemo(
261 () => [...new Set(groups.map((g) => `${g.repo.namespace}/${g.repo.name}`))].sort(),
262 [groups],
263 );
264 const people = useMemo(
265 () => [...new Set(groups.map((g) => g.actor).filter((a): a is string => a != null))].sort(),
266 [groups],
267 );
268 const filtered = groups.filter(
269 (g) =>
270 (project === "all" || `${g.repo.namespace}/${g.repo.name}` === project) &&
271 (person === "all" || g.actor === person) &&
272 (who === "all" || (who === "agents" ? g.actor == null || isAgent(g.actor) : g.actor != null && !isAgent(g.actor))),
273 );
274 const shown = more ? filtered : filtered.slice(0, limit);
275 return (
276 <div>
277 {groups.length > 0 && (
278 <div className="mb-3 flex flex-wrap items-center gap-2">
279 <div className="inline-flex rounded-lg border border-line bg-bg p-0.5 text-xs">
280 {(["all", "agents", "people"] as const).map((value) => (
281 <button
282 key={value}
283 type="button"
284 onClick={() => setWho(value)}
285 className={cn(
286 "rounded-md px-2.5 py-1 font-medium capitalize transition-colors",
287 who === value ? "bg-raised text-fg" : "text-muted hover:text-fg",
288 )}
289 >
290 {value === "all" ? "Everyone" : value}
291 </button>
292 ))}
293 </div>
294 {showRepo && projects.length > 1 && (
295 <Select value={project} onValueChange={setProject}>
296 <SelectTrigger size="sm" className="w-auto max-w-56" aria-label="Project">
297 <SelectValue />
298 </SelectTrigger>
299 <SelectContent>
300 <SelectItem value="all">All projects</SelectItem>
301 {projects.map((name) => (
302 <SelectItem key={name} value={name}>
303 {name.split("/")[1]}
304 </SelectItem>
305 ))}
306 </SelectContent>
307 </Select>
308 )}
309 {people.length > 1 && (
310 <Select value={person} onValueChange={setPerson}>
311 <SelectTrigger size="sm" className="w-auto max-w-48" aria-label="Who">
312 <SelectValue />
313 </SelectTrigger>
314 <SelectContent>
315 <SelectItem value="all">Anyone</SelectItem>
316 {people.map((name) => (
317 <SelectItem key={name} value={name} icon={<Avatar name={name} size={16} />}>
318 {name}
319 </SelectItem>
320 ))}
321 </SelectContent>
322 </Select>
323 )}
324 </div>
325 )}
326 {shown.length === 0 ? (
327 <Quiet>{groups.length === 0 ? empty : "Nothing matches these filters."}</Quiet>
328 ) : (
329 <ol className="relative space-y-0.5 before:absolute before:top-3 before:bottom-3 before:left-[0.9375rem] before:w-px before:bg-line">
330 {shown.map((group) => {
331 const look = VERB[group.parts[0].verb];
332 const agent = group.actor == null || isAgent(group.actor);
333 return (
334 <li key={group.id} className="relative flex items-start gap-3 rounded-lg px-1 py-1.5 text-sm">
335 <span
336 className={cn(
337 "relative z-10 mt-0.5 flex size-[1.375rem] shrink-0 items-center justify-center rounded-full bg-bg ring-1 ring-line",
338 look.tone,
339 )}
340 >
341 {look.icon}
342 </span>
343 <span className="min-w-0 grow leading-6 text-muted">
344 {group.actor ? (
345 <span className={cn("mr-1.5 inline-flex items-center gap-1.5 align-middle font-medium", agent ? "text-merged" : "text-fg")}>
346 <Avatar name={group.actor} size={16} />
347 {group.actor}
348 </span>
349 ) : (
350 <span className="mr-1 text-fg-soft">g1t:</span>
351 )}
352 <GroupLine group={group} />
353 {showRepo && (
354 <Link
355 to={`/${group.repo.namespace}/${group.repo.name}`}
356 className="ml-1.5 font-mono text-xs text-faint hover:text-muted"
357 >
358 {group.repo.name}
359 </Link>
360 )}
361 {group.count > 2 && <span className="ml-1.5 text-xs text-faint">· {group.count} events</span>}
362 </span>
363 <span className="shrink-0 pt-0.5 text-xs text-faint">
364 <TimeAgo at={group.at} />
365 </span>
366 </li>
367 );
368 })}
369 </ol>
370 )}
371 {filtered.length > limit && (
372 <button type="button" onClick={() => setMore(!more)} className="mt-2 text-xs text-muted hover:text-fg">
373 {more ? "Show fewer" : `Show ${filtered.length - limit} more`}
374 </button>
375 )}
376 </div>
377 );
378}
379
380// --- Pulse --------------------------------------------------------------------
381
382const DAY_LABEL = (offset: number) => {
383 const date = new Date(Date.now() - offset * 86_400_000);
384 return date.toLocaleDateString("en-US", { weekday: "short", month: "short", day: "numeric", timeZone: "UTC" });
385};
386
387/**
388 * A week of one measure as a small line, the last day marked. Each day
389 * has a hover target that says its value.
390 */
391export function Sparkline({
392 values,
393 format,
394 width = 96,
395 height = 28,
396}: {
397 values: number[];
398 format: (value: number) => string;
399 width?: number;
400 height?: number;
401}) {
402 const points = sparkPoints(values, width, height, 3);
403 if (points.length === 0) return null;
404 const line = points.map(([x, y], i) => `${i ? "L" : "M"}${x},${y}`).join(" ");
405 const area = `${line} L${points[points.length - 1][0]},${height} L${points[0][0]},${height} Z`;
406 const [lastX, lastY] = points[points.length - 1];
407 const slot = width / values.length;
408 return (
409 <svg width={width} height={height} viewBox={`0 0 ${width} ${height}`} className="overflow-visible text-accent" role="img" aria-label={values.map(format).join(", ")}>
410 <path d={area} fill="currentColor" opacity={0.12} />
411 <path d={line} fill="none" stroke="currentColor" strokeWidth={2} strokeLinecap="round" strokeLinejoin="round" />
412 <circle cx={lastX} cy={lastY} r={3} fill="currentColor" stroke="var(--color-surface)" strokeWidth={2} />
413 {values.map((value, index) => (
414 <rect key={index} x={index * slot} y={0} width={slot} height={height} fill="transparent" className="hover:fill-fg/5">
415 <title>{`${DAY_LABEL(values.length - 1 - index)}: ${format(value)}`}</title>
416 </rect>
417 ))}
418 </svg>
419 );
420}
421
422/** One measure of the week: a headline number, what it means, and its line. */
423export function PulseTile({
424 label,
425 value,
426 hint,
427 values,
428 format,
429 to,
430}: {
431 label: string;
432 value: string;
433 hint?: string;
434 values?: number[];
435 format?: (value: number) => string;
436 to?: string;
437}) {
438 const body = (
439 <>
440 <p className="truncate text-xs text-muted">{label}</p>
441 <div className="mt-1 flex items-end justify-between gap-2">
442 <p className="text-2xl font-semibold tracking-tight tabular-nums">{value}</p>
443 {values && format && values.some((v) => v > 0) && (
444 <span className="mb-1">
445 <Sparkline values={values} format={format} width={56} height={22} />
446 </span>
447 )}
448 </div>
449 <p className="mt-1.5 truncate text-[0.6875rem] text-faint" title={hint}>
450 {hint}
451 </p>
452 </>
453 );
454 const box = "block rounded-xl border border-line bg-surface p-4 transition-colors";
455 return to ? (
456 <Link to={to} className={`${box} hover:border-line-strong`}>
457 {body}
458 </Link>
459 ) : (
460 <div className={box}>{body}</div>
461 );
462}
463
464// --- Deploys ------------------------------------------------------------------
465
466const STRIP_TONE: Record<DeployStatus, string> = {
467 ready: "bg-accent",
468 // Served once, until a newer build or a take-down: it worked.
469 replaced: "bg-accent/45",
470 down: "bg-line-strong",
471 failed: "bg-danger",
472 building: "bg-warn animate-pulse",
473 queued: "bg-line-strong animate-pulse",
474 skipped: "bg-line",
475};
476
477/** The last builds as a row of bars, oldest on the left; each links to its build. */
478export function DeployStrip({
479 builds,
480 base,
481 slots = 20,
482}: {
483 builds: { id: string; status: DeployStatus; kind: string; commit: string; createdAt: string }[];
484 base: string;
485 slots?: number;
486}) {
487 const shown = builds.slice(0, slots).reverse();
488 return (
489 <div className="flex h-8 items-end gap-[3px]" aria-label="Recent builds, oldest first">
490 {Array.from({ length: slots - shown.length }, (_, i) => (
491 <span key={`empty-${i}`} className="h-2 flex-1 rounded-sm bg-line/60" />
492 ))}
493 {shown.map((build) => (
494 <Link
495 key={build.id}
496 to={`${base}/deployments/${build.id}`}
497 title={`${build.kind === "production" ? "Production" : "Preview"} · ${build.commit.slice(0, 7)} · ${build.status} · ${new Date(build.createdAt).toLocaleString("en-US")}`}
498 className={cn("flex-1 rounded-sm transition-opacity hover:opacity-80", STRIP_TONE[build.status], build.kind === "production" ? "h-8" : "h-5")}
499 />
500 ))}
501 </div>
502 );
503}
504
505/** A small meter for a share, 0 to 1. */
506export function Meter({ value, tone = "bg-accent" }: { value: number | null; tone?: string }) {
507 return (
508 <span className="block h-1.5 w-full overflow-hidden rounded-full bg-line">
509 {value != null && <span className={cn("block h-full rounded-full", tone)} style={{ width: `${Math.round(value * 100)}%` }} />}
510 </span>
511 );
512}
513
514export function percent(value: number | null): string {
515 return value == null ? "—" : `${Math.round(value * 100)}%`;
516}
517