g1t/apps/web/app/components/work.tsx

516 lines16,010 bytesCodeBlame
1import { Bot, CircleCheck, CircleSlash } from "lucide-react";
2import type { CSSProperties, ReactNode } from "react";
3import { Form, Link } from "react-router";
4
5import type { Comment, Issue, Pull, State } from "@g1t/contracts";
6
7import { repoAt } from "../lib/markdown-plugins";
8import { Markdown } from "./markdown";
9import { IssueIcon, PullIcon } from "./work-icons";
10import { MentionTextarea } from "./mention-textarea";
11import { Avatar, Button, TimeAgo } from "./ui";
12import { CheckboxOption } from "./ui/checkbox";
13
14export { IssueIcon, PullIcon };
15
16/** Hues for the labels every repository starts with. */
17const LABEL_HUES: Record<string, number> = {
18 bug: 25,
19 feature: 150,
20 docs: 240,
21 chore: 80,
22 question: 310,
23};
24
25/** A stable hue for any other label, from its name. */
26function hueFor(name: string): number {
27 let hash = 0;
28 for (const char of name) hash = (hash * 31 + char.charCodeAt(0)) % 360;
29 return hash;
30}
31
32export function Label({ name }: { name: string }) {
33 const hue = LABEL_HUES[name] ?? hueFor(name);
34 const style: CSSProperties = {
35 color: `oklch(0.84 0.11 ${hue})`,
36 borderColor: `oklch(0.84 0.11 ${hue} / 0.35)`,
37 backgroundColor: `oklch(0.84 0.11 ${hue} / 0.1)`,
38 };
39 return (
40 <span
41 style={style}
42 className="inline-flex shrink-0 items-center rounded-full border px-2 py-px text-xs font-medium"
43 >
44 {name}
45 </span>
46 );
47}
48
49/** The people an issue is assigned to, as overlapping avatars. */
50export function AssigneeStack({ people }: { people: string[] }) {
51 if (people.length === 0) return null;
52 const shown = people.slice(0, 3);
53 return (
54 <span
55 className="flex shrink-0 items-center"
56 title={`Assigned to ${people.join(", ")}`}
57 >
58 {shown.map((name, index) => (
59 <span key={name} className={`rounded-full ring-2 ring-bg ${index > 0 ? "-ml-1.5" : ""}`}>
60 <Avatar name={name} size={18} />
61 </span>
62 ))}
63 {people.length > shown.length && (
64 <span className="ml-1 text-xs text-faint">+{people.length - shown.length}</span>
65 )}
66 </span>
67 );
68}
69
70/** The agent working on an issue now. */
71export function Assignee({ agent }: { agent: string }) {
72 return (
73 <span
74 className="inline-flex shrink-0 items-center gap-1 rounded-full border border-accent/35 bg-accent/10 px-2 py-px text-xs font-medium text-accent"
75 title={`Assigned to ${agent}`}
76 >
77 <Bot size={12} />
78 {agent}
79 </span>
80 );
81}
82
83const PILL = "inline-flex shrink-0 items-center gap-1.5 rounded-full border px-2.5 py-1 text-xs font-medium";
84
85/** The large state badge at the top of an issue. */
86export function IssueState({ issue }: { issue: Pick<Issue, "state" | "reason"> }) {
87 const style =
88 issue.state === "open"
89 ? "border-accent/30 bg-accent/10 text-accent"
90 : issue.reason === "not_planned"
91 ? "border-line bg-surface text-muted"
92 : "border-merged/30 bg-merged/10 text-merged";
93 return (
94 <span className={`${PILL} ${style}`}>
95 <IssueIcon issue={issue} size={14} />
96 {issue.state === "open"
97 ? "Open"
98 : issue.reason === "not_planned"
99 ? "Closed as not planned"
100 : "Closed"}
101 </span>
102 );
103}
104
105const PULL_STYLES: Record<Pull["status"], string> = {
106 draft: "border-line bg-surface text-muted",
107 open: "border-accent/30 bg-accent/10 text-accent",
108 merged: "border-merged/30 bg-merged/10 text-merged",
109 closed: "border-danger/30 bg-danger/10 text-danger",
110};
111
112const PULL_NAMES: Record<Pull["status"], string> = {
113 draft: "Draft",
114 open: "Open",
115 merged: "Merged",
116 closed: "Closed",
117};
118
119export function PullState({ status }: { status: Pull["status"] }) {
120 return (
121 <span className={`${PILL} ${PULL_STYLES[status]}`}>
122 <PullIcon status={status} size={14} />
123 {PULL_NAMES[status]}
124 </span>
125 );
126}
127
128/** The open and closed switch above a list. `to` is the list's own path. */
129export function StateTabs({
130 to,
131 state,
132 query = "",
133 action,
134}: {
135 to: string;
136 state: State;
137 /** Other query parameters to keep, e.g. `label=bug`. */
138 query?: string;
139 action?: ReactNode;
140}) {
141 const link = (value: State) =>
142 `${to}?${[value === "open" ? "" : "state=closed", query].filter(Boolean).join("&")}`;
143 const tab = (value: State, label: string) => (
144 <Link
145 to={link(value)}
146 className={
147 "rounded-md px-3 py-1.5 text-sm transition-colors " +
148 (state === value ? "bg-raised font-medium text-fg" : "text-muted hover:text-fg")
149 }
150 >
151 {label}
152 </Link>
153 );
154 return (
155 <div className="flex flex-wrap items-center justify-between gap-3">
156 <div className="flex gap-1 rounded-lg border border-line p-1">
157 {tab("open", "Open")}
158 {tab("closed", "Closed")}
159 </div>
160 {action}
161 </div>
162 );
163}
164
165const VERDICTS = {
166 approve: { label: "approved these changes", style: "text-accent" },
167 request_changes: { label: "requested changes", style: "text-danger" },
168} as const;
169
170/**
171 * A person's name (or `children`) linking to their profile at `/u/<name>`.
172 * g1t, and names that are not usernames, stay plain text.
173 */
174export function PersonLink({
175 name,
176 className,
177 label,
178 children,
179}: {
180 name: string;
181 className?: string;
182 label?: string;
183 children?: ReactNode;
184}) {
185 // g1t itself has no profile; its name carries a small label instead.
186 if (name === "g1t" && !children) {
187 return (
188 <span className="inline-flex items-baseline gap-1">
189 <span className={className}>g1t</span>
190 <span className="rounded border border-line px-1 text-[0.625rem] leading-[1.35] font-medium text-muted">bot</span>
191 </span>
192 );
193 }
194 if (name === "g1t" || !/^[a-z0-9-]{1,39}$/i.test(name)) {
195 return <span className={className}>{children ?? name}</span>;
196 }
197 return (
198 <Link to={`/u/${name.toLowerCase()}`} className={className} aria-label={label}>
199 {children ?? name}
200 </Link>
201 );
202}
203
204/**
205 * Comments in order, then the box to add one. On a pull request, `review`
206 * says where its changes are shown and whether the viewer may give a
207 * verdict.
208 */
209/**
210 * One entry in a conversation: who, what they did and when, then what they
211 * wrote. The author's avatar sits beside it, as on any forge.
212 */
213export function TimelineItem({
214 author,
215 action,
216 at,
217 aside,
218 children,
219}: {
220 author: string;
221 /** What they did, after their name: "commented", "opened this". */
222 action: ReactNode;
223 at?: string;
224 /** Shown at the right of the header. */
225 aside?: ReactNode;
226 children?: ReactNode;
227}) {
228 return (
229 <div className="flex gap-3">
230 <span className="mt-1 hidden shrink-0 sm:block">
231 <PersonLink name={author} label={`${author}'s profile`}>
232 <Avatar name={author} size={32} />
233 </PersonLink>
234 </span>
235 <article className="min-w-0 grow overflow-hidden rounded-xl border border-line bg-surface">
236 <header className="flex flex-wrap items-center gap-x-2 gap-y-1 border-b border-line bg-raised/40 px-4 py-2 text-sm text-muted">
237 <span className="sm:hidden">
238 <Avatar name={author} size={18} />
239 </span>
240 <PersonLink name={author} className="font-medium text-fg hover:underline" />
241 {action}
242 {at && <TimeAgo at={at} />}
243 {aside && <span className="ml-auto min-w-0">{aside}</span>}
244 </header>
245 {children && <div className="px-4 py-3">{children}</div>}
246 </article>
247 </div>
248 );
249}
250
251type Review = { changesUrl: string; canJudge: boolean };
252
253/**
254 * Text with each `#12` linked to the issue or pull request of that number,
255 * and each file named in backticks shown as code, linked to its diff when
256 * `filesUrl` says where the changes are.
257 */
258function WithReferences({ text, base, filesUrl }: { text: string; base?: string; filesUrl?: string }) {
259 if (!base) return <>{text}</>;
260 return (
261 <>
262 {text.split(/(#\d+|`[^`\n]+`)/).map((part, index) =>
263 /^`[^`]+`$/.test(part) ? (
264 filesUrl ? (
265 <Link
266 key={index}
267 to={`${filesUrl}#file-${part.slice(1, -1)}`}
268 className="font-mono text-xs text-fg hover:underline"
269 >
270 {part.slice(1, -1)}
271 </Link>
272 ) : (
273 <code key={index} className="font-mono text-xs text-fg">
274 {part.slice(1, -1)}
275 </code>
276 )
277 ) : /^#\d+$/.test(part) ? (
278 // Issues and pull requests share numbers; the issue page forwards.
279 <Link
280 key={index}
281 to={`${base}/issues/${part.slice(1)}`}
282 className="font-medium text-fg hover:underline"
283 >
284 {part}
285 </Link>
286 ) : (
287 part
288 ),
289 )}
290 </>
291 );
292}
293
294/** Something that happened, as one line on the conversation's rail. */
295function TimelineEvent({ comment, base, filesUrl }: { comment: Comment; base?: string; filesUrl?: string }) {
296 return (
297 <div className="flex items-center gap-3 text-sm text-muted">
298 <span className="hidden w-8 shrink-0 justify-center sm:flex">
299 <span className="size-2 rounded-full border border-line-strong bg-bg" />
300 </span>
301 <span className="flex min-w-0 flex-wrap items-center gap-x-1.5 gap-y-0.5">
302 <Avatar name={comment.author.username} size={16} />
303 <PersonLink name={comment.author.username} className="font-medium text-fg hover:underline" />
304 <span>
305 <WithReferences text={comment.body} base={base} filesUrl={filesUrl} />
306 </span>
307 <span className="text-faint">
308 · <TimeAgo at={comment.createdAt} />
309 </span>
310 </span>
311 </div>
312 );
313}
314
315/**
316 * The conversation of an issue or a pull request, oldest first: what
317 * people and agents wrote, and between those, what happened.
318 */
319export function CommentList({
320 comments,
321 review,
322 base,
323}: {
324 comments: Comment[];
325 review?: Review;
326 /** The repository's path, so that `#12` in an event can be linked. */
327 base?: string;
328}) {
329 return (
330 <>
331 {comments.map((comment) => {
332 if (comment.kind === "event") {
333 return <TimelineEvent key={comment.id} comment={comment} base={base} filesUrl={review?.changesUrl} />;
334 }
335 const verdict = comment.verdict && VERDICTS[comment.verdict];
336 return (
337 <TimelineItem
338 key={comment.id}
339 author={comment.author.username}
340 at={comment.createdAt}
341 action={
342 verdict ? (
343 <span className={`flex items-center gap-1 font-medium ${verdict.style}`}>
344 {comment.verdict === "approve" ? (
345 <CircleCheck size={14} />
346 ) : (
347 <CircleSlash size={14} />
348 )}
349 {verdict.label}
350 </span>
351 ) : (
352 <span>commented</span>
353 )
354 }
355 aside={
356 comment.path &&
357 review && (
358 <Link
359 to={`${review.changesUrl}#file-${comment.path}`}
360 className="block truncate font-mono text-xs text-faint hover:text-fg"
361 >
362 {comment.path}
363 {comment.line != null && `:${comment.line}`}
364 </Link>
365 )
366 }
367 >
368 {comment.body && <Markdown source={comment.body} repo={repoAt(base)} />}
369 </TimelineItem>
370 );
371 })}
372 </>
373 );
374}
375
376/** Where a signed-in person writes a comment, or a review. */
377export function CommentForm({
378 author,
379 resetKey,
380 review,
381}: {
382 /** The viewer's username, or null if they are signed out. */
383 author: string | null;
384 /** Changes when a comment is added, which clears the box. */
385 resetKey: number;
386 review?: Review;
387}) {
388 if (!author) {
389 return (
390 <p className="text-sm text-muted">
391 <Link to="/login" className="text-fg underline underline-offset-4">
392 Sign in
393 </Link>{" "}
394 to comment.
395 </p>
396 );
397 }
398 return (
399 <div className="flex gap-3">
400 <span className="mt-1 hidden shrink-0 sm:block">
401 <Avatar name={author} size={32} />
402 </span>
403 <Form method="post" className="min-w-0 grow space-y-2" key={resetKey}>
404 <input type="hidden" name="action" value="comment" />
405 <MentionTextarea
406 name="body"
407 rows={3}
408 placeholder={
409 review?.canJudge
410 ? "Leave a comment, or a review. Markdown works."
411 : "Leave a comment. Markdown works."
412 }
413 />
414 <div className="flex flex-wrap gap-2">
415 <Button type="submit">Comment</Button>
416 {review?.canJudge && (
417 <>
418 <Button variant="quiet" type="submit" name="verdict" value="approve">
419 <CircleCheck size={14} className="text-accent" />
420 Approve
421 </Button>
422 <Button variant="quiet" type="submit" name="verdict" value="request_changes">
423 <CircleSlash size={14} className="text-danger" />
424 Request changes
425 </Button>
426 </>
427 )}
428 </div>
429 </Form>
430 </div>
431 );
432}
433
434/**
435 * Checkboxes for choosing people: the workspace's members, anyone already
436 * chosen, and a box for other usernames. Posts `name` for each ticked and
437 * `others` for the rest.
438 */
439export function PeoplePicker({
440 name,
441 members,
442 chosen,
443}: {
444 name: string;
445 members: string[];
446 chosen: string[];
447}) {
448 const people = [...new Set([...members, ...chosen])];
449 return (
450 <>
451 {people.length > 0 && (
452 <div className="space-y-1.5">
453 {people.map((person) => (
454 <CheckboxOption
455 key={person}
456 name={name}
457 value={person}
458 defaultChecked={chosen.includes(person)}
459 className="items-center"
460 labelClassName="flex items-center gap-2"
461 label={
462 <>
463 <Avatar name={person} size={18} />
464 <span className="font-mono text-xs">{person}</span>
465 </>
466 }
467 />
468 ))}
469 </div>
470 )}
471 <input
472 name="others"
473 placeholder="Other usernames, comma separated"
474 autoComplete="off"
475 data-1p-ignore
476 className="w-full rounded-md border border-line bg-bg px-3 py-2 text-sm outline-none placeholder:text-faint hover:border-line-strong focus:border-accent-dim"
477 />
478 </>
479 );
480}
481
482/** Where each reviewer stands: their most recent verdict. */
483export function verdicts(comments: Comment[]): { reviewer: string; verdict: NonNullable<Comment["verdict"]> }[] {
484 const latest = new Map<string, NonNullable<Comment["verdict"]>>();
485 for (const comment of comments) {
486 if (comment.verdict) latest.set(comment.author.username, comment.verdict);
487 }
488 return [...latest].map(([reviewer, verdict]) => ({ reviewer, verdict }));
489}
490
491/** `2 files +12 −3`: the size of a pull request's change. */
492export function ChangeSize({ files }: { files: Pull["files"] }) {
493 if (files.length === 0) return null;
494 const additions = files.reduce((sum, file) => sum + file.additions, 0);
495 const deletions = files.reduce((sum, file) => sum + file.deletions, 0);
496 return (
497 <span className="flex shrink-0 items-center gap-1.5 font-mono text-xs text-faint">
498 <span>
499 {files.length} {files.length === 1 ? "file" : "files"}
500 </span>
501 <span className="text-accent">+{additions}</span>
502 <span className="text-danger">−{deletions}</span>
503 </span>
504 );
505}
506
507/** The words of a Markdown text, without its markup, for a one-line preview. */
508export function plainText(markdown: string): string {
509 return markdown
510 .replace(/```[\s\S]*?```/g, " ")
511 .replace(/^\s{0,3}(#{1,6}|[-*+]|\d+\.)\s+/gm, "")
512 .replace(/!?\[([^\]]*)\]\([^)]*\)/g, "$1")
513 .replace(/[*_`~]/g, "")
514 .replace(/\s+/g, " ")
515 .trim();
516}