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.

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