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.

458 linesCodeBlameRaw
1/**
2 * Work items, drawn: a row of the Work list that reads on a phone, an
3 * item's repositories as chips, and the Work list's filters as menus on a
4 * computer and as a sheet that rises from the bottom on a phone. Where
5 * Work's pages are, and the filters, are worked out in lib/work-items.ts.
6 */
7import { Check, GitPullRequest, MessageSquare, Milestone as MilestoneIcon, SlidersHorizontal, X } from "lucide-react";
8import { type ReactNode, useState } from "react";
9import { Link } from "react-router";
10
11import { type ItemRepo, type Label, type LinkRole, type Milestone, type WorkItem, appetiteLetter } from "@g1t/contracts";
12
13import { FilterMenu, LabelChip, Swatch } from "./labels";
14import { BottomSheet } from "./mobile";
15import { TimeAgo } from "./ui";
16import { Badge } from "./ui/badge";
17import { Button } from "./ui/button";
18import { Hint } from "./ui/hint";
19import { AssigneeStack } from "./work";
20import { ItemIcon } from "./work-icons";
21import { cn } from "../lib/cn";
22import type { LabelColors } from "../lib/labels";
23import {
24 HORIZON_LABELS,
25 LINK_ROLES,
26 LIST_HORIZONS,
27 ROLE_LABELS,
28 type WorkFilters,
29 filterCount,
30 filtered,
31 itemPath,
32 repoChips,
33 shortPath,
34 workListPath,
35} from "../lib/work-items";
36
37/** How a repository is linked, as a quiet word beside its name. */
38export function RoleBadge({ role }: { role: LinkRole }) {
39 return <Badge tone={role === "target" ? "accent" : "neutral"}>{ROLE_LABELS[role]}</Badge>;
40}
41
42/**
43 * An item's repositories in a row: the first two (its origin first), then
44 * "+2" for the rest, so a row fits a phone. Each says how it is linked on
45 * hover, and a landed one is marked.
46 */
47export function RepoChips({ repos, workspace, max = 2, className }: { repos: ItemRepo[]; workspace: string; max?: number; className?: string }) {
48 if (repos.length === 0) return null;
49 const { shown, more } = repoChips(repos, max);
50 const rest = repos
51 .slice(shown.length)
52 .map((repo) => shortPath(repo.path, workspace))
53 .join(", ");
54 return (
55 <span className={cn("inline-flex min-w-0 items-center gap-1", className)}>
56 {shown.map((repo) => (
57 <Hint
58 key={repo.repoId}
59 label={`${repo.path || "A deleted repository"}${repo.deleted ? ", deleted" : ""}: ${ROLE_LABELS[repo.role].toLowerCase()}${repo.landed ? ", landed" : ""}`}
60 >
61 <span
62 className={cn(
63 "inline-flex max-w-32 min-w-0 items-center gap-1 rounded-md border px-1.5 py-px font-mono text-[0.6875rem] leading-4",
64 repo.deleted
65 ? "border-dashed border-line text-faint line-through"
66 : repo.landed
67 ? "border-merged/40 text-merged"
68 : repo.role === "target"
69 ? "border-line-strong text-fg-soft"
70 : "border-line text-muted",
71 )}
72 >
73 {repo.landed && <Check size={10} className="shrink-0" />}
74 <span className="truncate">{shortPath(repo.path, workspace)}</span>
75 </span>
76 </Hint>
77 ))}
78 {more > 0 && (
79 <Hint label={rest}>
80 <span className="shrink-0 rounded-md border border-line px-1 py-px text-[0.6875rem] leading-4 text-muted">+{more}</span>
81 </Hint>
82 )}
83 </span>
84 );
85}
86
87/**
88 * Where an open work item stands in the plan, beside its title: urgent,
89 * Now or Next as a small badge (Triage, Later and Set aside are said only
90 * by the list's filter), and its appetite's letter when someone set one.
91 */
92export function PlanBadges({ item }: { item: WorkItem }) {
93 if (item.state !== "open") return null;
94 const horizon = item.horizon === "now" || item.horizon === "next" ? item.horizon : null;
95 if (!item.urgent && !horizon && !item.appetite) return null;
96 return (
97 <span className="inline-flex shrink-0 items-center gap-1">
98 {item.urgent && <Badge tone="danger">Urgent</Badge>}
99 {horizon && <Badge tone={horizon === "now" ? "accent" : "neutral"}>{HORIZON_LABELS[horizon]}</Badge>}
100 {item.appetite && (
101 <Hint label={`Appetite: ${item.appetite}`}>
102 <Badge className="px-1.5" aria-label={`Appetite: ${item.appetite}`}>
103 {appetiteLetter(item.appetite)}
104 </Badge>
105 </Hint>
106 )}
107 </span>
108 );
109}
110
111/**
112 * One row of the Work list: its state, title, where it stands in the plan
113 * and its labels, then its number, who opened it and when, its
114 * repositories and milestone; its assignees, pull requests and comments
115 * at the end. On a phone the meta wraps under the title and the counts sit
116 * with it, so nothing scrolls sideways.
117 */
118export function WorkItemRow({ item, colors }: { item: WorkItem; colors: LabelColors }) {
119 return (
120 <li className="transition-colors hover:bg-surface">
121 <Link prefetch="intent" to={itemPath(item.workspace, item.number)} className="flex min-w-0 items-start gap-3 px-4 py-3">
122 <span className="mt-0.5">
123 <ItemIcon item={item} />
124 </span>
125 <span className="min-w-0 grow">
126 <span className="flex flex-wrap items-center gap-x-2 gap-y-1">
127 <span className="min-w-0 font-medium wrap-break-word">{item.title}</span>
128 <PlanBadges item={item} />
129 {item.labels.map((name) => (
130 <LabelChip key={name} name={name} color={colors[name]} />
131 ))}
132 </span>
133 <span className="mt-1 flex flex-wrap items-center gap-x-2 gap-y-1 text-xs text-faint">
134 <span className="whitespace-nowrap">
135 #{item.number} · <TimeAgo at={item.state === "closed" && item.closedAt ? item.closedAt : item.createdAt} />
136 </span>
137 <span className="hidden whitespace-nowrap sm:inline">
138 {item.state === "closed" ? "closed" : "opened"} by {item.author.username}
139 </span>
140 <RepoChips repos={item.repos} workspace={item.workspace} />
141 {item.milestone && (
142 <span className="inline-flex min-w-0 items-center gap-1">
143 <MilestoneIcon size={11} className="shrink-0" />
144 <span className="max-w-36 truncate">{item.milestone.title}</span>
145 </span>
146 )}
147 <span className="ml-auto flex shrink-0 items-center gap-3 text-muted sm:hidden">
148 <Counts item={item} />
149 </span>
150 </span>
151 </span>
152 <span className="mt-0.5 hidden shrink-0 items-center gap-3 text-xs text-muted sm:flex">
153 <AssigneeStack people={item.assignees} />
154 <Counts item={item} />
155 </span>
156 </Link>
157 </li>
158 );
159}
160
161function Counts({ item }: { item: WorkItem }) {
162 return (
163 <>
164 {item.pullCount > 0 && (
165 <span className="flex items-center gap-1" aria-label={`${item.pullCount} pull ${item.pullCount === 1 ? "request" : "requests"}`}>
166 <GitPullRequest size={13} />
167 {item.pullCount}
168 </span>
169 )}
170 {item.commentCount > 0 && (
171 <span className="flex items-center gap-1" aria-label={`${item.commentCount} ${item.commentCount === 1 ? "comment" : "comments"}`}>
172 <MessageSquare size={13} />
173 {item.commentCount}
174 </span>
175 )}
176 </>
177 );
178}
179
180/** What the Work list's filters offer: each may have failed to load, and says so in its menu. */
181export type FilterChoices = {
182 repos: string[] | null;
183 labels: Label[] | null;
184 milestones: Milestone[] | null;
185 /** The workspace's people, for a member; null when there are none to offer. */
186 people: string[] | null;
187 /** The viewer, for "Assigned to you". */
188 me: string | null;
189};
190
191/**
192 * The Work list's filters: on a computer, a menu each (horizon, repository,
193 * its role, label, milestone, assignee); on a phone, one Filters button that
194 * says how many are on and opens them as a sheet from the bottom, within
195 * reach of a thumb. Each choice is a link, so the address keeps them.
196 */
197export function WorkFiltersBar({ workspace, filters, choices }: { workspace: string; filters: WorkFilters; choices: FilterChoices }) {
198 const [open, setOpen] = useState(false);
199 const to = (change: Partial<WorkFilters>) => workListPath(workspace, filters, change);
200 const milestoneTitle = choices.milestones?.find((m) => m.number === filters.milestone)?.title ?? (filters.milestone ? `#${filters.milestone}` : null);
201 const count = filterCount(filters);
202 const clear = workListPath(workspace, { state: filters.state });
203 const assigneeLabel = filters.assignee === "@me" || filters.assignee === choices.me ? "You" : filters.assignee;
204 return (
205 <>
206 <div className="hidden flex-wrap items-center gap-2 sm:flex">
207 <FilterMenu
208 label="Horizon"
209 active={filters.horizon ? HORIZON_LABELS[filters.horizon] : undefined}
210 clearTo={filters.horizon ? to({ horizon: null }) : undefined}
211 searchPlaceholder="Filter horizons"
212 emptyText="No horizon matches."
213 options={LIST_HORIZONS.map((horizon) => ({
214 key: horizon,
215 to: to({ horizon }),
216 keywords: HORIZON_LABELS[horizon],
217 selected: filters.horizon === horizon,
218 label: <span>{HORIZON_LABELS[horizon]}</span>,
219 }))}
220 />
221 <FilterMenu
222 label="Repository"
223 active={filters.repo ? shortPath(filters.repo, workspace) : undefined}
224 clearTo={filters.repo ? to({ repo: "", role: null }) : undefined}
225 searchPlaceholder="Filter repositories"
226 emptyText={choices.repos ? "No repository matches." : "Repositories could not be loaded just now."}
227 options={(choices.repos ?? []).map((name) => ({
228 key: name,
229 to: to({ repo: name }),
230 keywords: name,
231 selected: shortPath(filters.repo, workspace) === name,
232 label: <span className="truncate font-mono text-[0.8125rem]">{name}</span>,
233 }))}
234 />
235 {filters.repo && (
236 <FilterMenu
237 label="Role"
238 active={filters.role ? ROLE_LABELS[filters.role] : undefined}
239 clearTo={filters.role ? to({ role: null }) : undefined}
240 searchPlaceholder="Filter roles"
241 emptyText="No role matches."
242 options={LINK_ROLES.map((role) => ({
243 key: role,
244 to: to({ role }),
245 keywords: role,
246 selected: filters.role === role,
247 label: <span>{ROLE_LABELS[role]}</span>,
248 }))}
249 />
250 )}
251 <FilterMenu
252 label="Label"
253 active={filters.label || undefined}
254 clearTo={filters.label ? to({ label: "" }) : undefined}
255 searchPlaceholder="Filter labels"
256 emptyText={choices.labels ? "No label matches." : "Labels could not be loaded just now."}
257 options={(choices.labels ?? []).map((label) => ({
258 key: label.name,
259 to: to({ label: label.name }),
260 keywords: `${label.name} ${label.description}`,
261 selected: label.name === filters.label,
262 label: (
263 <>
264 <Swatch color={label.color} />
265 <span className="truncate">{label.name}</span>
266 </>
267 ),
268 }))}
269 />
270 <FilterMenu
271 label="Milestone"
272 active={milestoneTitle ?? undefined}
273 clearTo={filters.milestone ? to({ milestone: null }) : undefined}
274 searchPlaceholder="Filter milestones"
275 emptyText={choices.milestones ? "No open milestone matches." : "Milestones could not be loaded just now."}
276 options={(choices.milestones ?? []).map((milestone) => ({
277 key: String(milestone.number),
278 to: to({ milestone: milestone.number }),
279 keywords: `${milestone.title} ${milestone.number}`,
280 selected: milestone.number === filters.milestone,
281 label: <span className="truncate">{milestone.title}</span>,
282 }))}
283 />
284 {(choices.people || choices.me) && (
285 <FilterMenu
286 label="Assignee"
287 active={assigneeLabel || undefined}
288 clearTo={filters.assignee ? to({ assignee: "" }) : undefined}
289 searchPlaceholder="Filter people"
290 emptyText="Nobody matches."
291 options={assigneeOptions(choices).map((name) => ({
292 key: name,
293 to: to({ assignee: name }),
294 keywords: name,
295 selected: name === filters.assignee,
296 label: <span className="truncate">{name === choices.me ? `${name} (you)` : name}</span>,
297 }))}
298 />
299 )}
300 {filtered(filters) && (
301 <Link to={clear} className="text-xs text-muted hover:text-fg">
302 Clear filters
303 </Link>
304 )}
305 </div>
306
307 <div className="flex items-center gap-2 sm:hidden">
308 <Button type="button" variant="outline" size="sm" onClick={() => setOpen(true)} aria-haspopup="dialog" className="h-10 gap-2 px-3.5">
309 <SlidersHorizontal size={15} />
310 Filters
311 {count > 0 && <span className="rounded-full bg-accent px-1.5 text-[0.6875rem] leading-4.5 font-bold text-bg tabular-nums">{count}</span>}
312 </Button>
313 {filters.horizon && <Badge tone="accent">{HORIZON_LABELS[filters.horizon]}</Badge>}
314 {filters.repo && (
315 <span className="min-w-0 truncate font-mono text-xs text-muted">
316 {shortPath(filters.repo, workspace)}
317 {filters.role && ` · ${ROLE_LABELS[filters.role].toLowerCase()}`}
318 </span>
319 )}
320 </div>
321 <BottomSheet open={open} onOpenChange={setOpen} title="Filters">
322 <div className="flex items-center justify-between px-3 pt-1 pb-2">
323 <h2 className="text-base font-semibold">Filters</h2>
324 {count > 0 && (
325 <Link to={clear} onClick={() => setOpen(false)} className="inline-flex h-10 items-center gap-1 px-2 text-sm text-muted active:text-fg">
326 <X size={14} />
327 Clear all
328 </Link>
329 )}
330 </div>
331 <SheetChoices
332 title="Horizon"
333 onPick={() => setOpen(false)}
334 choices={[
335 { key: "any", label: "Any horizon", to: to({ horizon: null }), on: !filters.horizon },
336 ...LIST_HORIZONS.map((horizon) => ({ key: horizon, label: HORIZON_LABELS[horizon], to: to({ horizon }), on: filters.horizon === horizon })),
337 ]}
338 />
339 <SheetChoices
340 title="Repository"
341 failed={choices.repos == null}
342 onPick={() => setOpen(false)}
343 choices={[
344 { key: "any", label: "Any repository", to: to({ repo: "", role: null }), on: !filters.repo },
345 ...(choices.repos ?? []).map((name) => ({
346 key: name,
347 label: <span className="font-mono text-sm">{name}</span>,
348 to: to({ repo: name }),
349 on: shortPath(filters.repo, workspace) === name,
350 })),
351 ]}
352 />
353 {filters.repo && (
354 <SheetChoices
355 title="Linked as"
356 onPick={() => setOpen(false)}
357 choices={[
358 { key: "any", label: "Any role", to: to({ role: null }), on: !filters.role },
359 ...LINK_ROLES.map((role) => ({ key: role, label: ROLE_LABELS[role], to: to({ role }), on: filters.role === role })),
360 ]}
361 />
362 )}
363 <SheetChoices
364 title="Label"
365 failed={choices.labels == null}
366 onPick={() => setOpen(false)}
367 choices={[
368 { key: "any", label: "Any label", to: to({ label: "" }), on: !filters.label },
369 ...(choices.labels ?? []).map((label) => ({
370 key: label.name,
371 label: (
372 <span className="inline-flex items-center gap-2">
373 <Swatch color={label.color} />
374 {label.name}
375 </span>
376 ),
377 to: to({ label: label.name }),
378 on: label.name === filters.label,
379 })),
380 ]}
381 />
382 <SheetChoices
383 title="Milestone"
384 failed={choices.milestones == null}
385 onPick={() => setOpen(false)}
386 choices={[
387 { key: "any", label: "Any milestone", to: to({ milestone: null }), on: !filters.milestone },
388 ...(choices.milestones ?? []).map((milestone) => ({
389 key: String(milestone.number),
390 label: milestone.title,
391 to: to({ milestone: milestone.number }),
392 on: milestone.number === filters.milestone,
393 })),
394 ]}
395 />
396 {(choices.people || choices.me) && (
397 <SheetChoices
398 title="Assignee"
399 onPick={() => setOpen(false)}
400 choices={[
401 { key: "any", label: "Anyone", to: to({ assignee: "" }), on: !filters.assignee },
402 ...assigneeOptions(choices).map((name) => ({
403 key: name,
404 label: name === choices.me ? `${name} (you)` : name,
405 to: to({ assignee: name }),
406 on: name === filters.assignee,
407 })),
408 ]}
409 />
410 )}
411 </BottomSheet>
412 </>
413 );
414}
415
416/** The people an item can be filtered by assignee: the viewer first, then everyone else in the workspace. */
417function assigneeOptions(choices: FilterChoices): string[] {
418 const others = (choices.people ?? []).filter((name) => name !== choices.me);
419 return choices.me ? [choices.me, ...others] : others;
420}
421
422/** One filter in the phone's sheet: a heading and its choices, 48px rows, the one on ticked. */
423export function SheetChoices({
424 title,
425 choices,
426 failed = false,
427 onPick,
428}: {
429 title: string;
430 choices: { key: string; label: ReactNode; to: string; on: boolean }[];
431 failed?: boolean;
432 onPick: () => void;
433}) {
434 return (
435 <section className="border-t border-line pt-2 pb-1">
436 <h3 className="px-3 pt-1 pb-1 text-xs font-medium text-faint">{title}</h3>
437 {failed && <p className="px-3 pb-2 text-xs text-muted">{title === "Repository" ? "Repositories" : `${title}s`} could not be loaded just now.</p>}
438 <ul>
439 {choices.map((choice) => (
440 <li key={choice.key}>
441 <Link
442 to={choice.to}
443 onClick={onPick}
444 aria-current={choice.on ? "true" : undefined}
445 className={cn(
446 "flex h-12 w-full items-center gap-3 rounded-md px-3 text-[0.9375rem] transition-colors active:bg-raised",
447 choice.on ? "font-medium text-fg" : "text-fg-soft",
448 )}
449 >
450 <span className="flex w-5 shrink-0 justify-center">{choice.on && <Check size={16} className="text-accent" />}</span>
451 <span className="min-w-0 grow truncate">{choice.label}</span>
452 </Link>
453 </li>
454 ))}
455 </ul>
456 </section>
457 );
458}