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

519 lines16,153 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/** How g1t's own agents are named; they have no profile to link to. */
171const AGENTS = new Set(["g1t-agent", "g1t agent", "g1t"]);
172
173/**
174 * A person's name (or `children`) linking to their profile at `/u/<name>`.
175 * g1t's agents, and names that are not usernames, stay plain text.
176 */
177export function PersonLink({
178 name,
179 className,
180 label,
181 children,
182}: {
183 name: string;
184 className?: string;
185 label?: string;
186 children?: ReactNode;
187}) {
188 // g1t itself has no profile; its name carries a small label instead.
189 if (name === "g1t" && !children) {
190 return (
191 <span className="inline-flex items-baseline gap-1">
192 <span className={className}>g1t</span>
193 <span className="rounded border border-line px-1 text-[0.625rem] leading-[1.35] font-medium text-muted">bot</span>
194 </span>
195 );
196 }
197 if (AGENTS.has(name) || !/^[a-z0-9-]{1,39}$/i.test(name)) {
198 return <span className={className}>{children ?? name}</span>;
199 }
200 return (
201 <Link to={`/u/${name.toLowerCase()}`} className={className} aria-label={label}>
202 {children ?? name}
203 </Link>
204 );
205}
206
207/**
208 * Comments in order, then the box to add one. On a pull request, `review`
209 * says where its changes are shown and whether the viewer may give a
210 * verdict.
211 */
212/**
213 * One entry in a conversation: who, what they did and when, then what they
214 * wrote. The author's avatar sits beside it, as on any forge.
215 */
216export function TimelineItem({
217 author,
218 action,
219 at,
220 aside,
221 children,
222}: {
223 author: string;
224 /** What they did, after their name: "commented", "opened this". */
225 action: ReactNode;
226 at?: string;
227 /** Shown at the right of the header. */
228 aside?: ReactNode;
229 children?: ReactNode;
230}) {
231 return (
232 <div className="flex gap-3">
233 <span className="mt-1 hidden shrink-0 sm:block">
234 <PersonLink name={author} label={`${author}'s profile`}>
235 <Avatar name={author} size={32} />
236 </PersonLink>
237 </span>
238 <article className="min-w-0 grow overflow-hidden rounded-xl border border-line bg-surface">
239 <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">
240 <span className="sm:hidden">
241 <Avatar name={author} size={18} />
242 </span>
243 <PersonLink name={author} className="font-medium text-fg hover:underline" />
244 {action}
245 {at && <TimeAgo at={at} />}
246 {aside && <span className="ml-auto min-w-0">{aside}</span>}
247 </header>
248 {children && <div className="px-4 py-3">{children}</div>}
249 </article>
250 </div>
251 );
252}
253
254type Review = { changesUrl: string; canJudge: boolean };
255
256/**
257 * Text with each `#12` linked to the issue or pull request of that number,
258 * and each file named in backticks shown as code, linked to its diff when
259 * `filesUrl` says where the changes are.
260 */
261function WithReferences({ text, base, filesUrl }: { text: string; base?: string; filesUrl?: string }) {
262 if (!base) return <>{text}</>;
263 return (
264 <>
265 {text.split(/(#\d+|`[^`\n]+`)/).map((part, index) =>
266 /^`[^`]+`$/.test(part) ? (
267 filesUrl ? (
268 <Link
269 key={index}
270 to={`${filesUrl}#file-${part.slice(1, -1)}`}
271 className="font-mono text-xs text-fg hover:underline"
272 >
273 {part.slice(1, -1)}
274 </Link>
275 ) : (
276 <code key={index} className="font-mono text-xs text-fg">
277 {part.slice(1, -1)}
278 </code>
279 )
280 ) : /^#\d+$/.test(part) ? (
281 // Issues and pull requests share numbers; the issue page forwards.
282 <Link
283 key={index}
284 to={`${base}/issues/${part.slice(1)}`}
285 className="font-medium text-fg hover:underline"
286 >
287 {part}
288 </Link>
289 ) : (
290 part
291 ),
292 )}
293 </>
294 );
295}
296
297/** Something that happened, as one line on the conversation's rail. */
298function TimelineEvent({ comment, base, filesUrl }: { comment: Comment; base?: string; filesUrl?: string }) {
299 return (
300 <div className="flex items-center gap-3 text-sm text-muted">
301 <span className="hidden w-8 shrink-0 justify-center sm:flex">
302 <span className="size-2 rounded-full border border-line-strong bg-bg" />
303 </span>
304 <span className="flex min-w-0 flex-wrap items-center gap-x-1.5 gap-y-0.5">
305 <Avatar name={comment.author.username} size={16} />
306 <PersonLink name={comment.author.username} className="font-medium text-fg hover:underline" />
307 <span>
308 <WithReferences text={comment.body} base={base} filesUrl={filesUrl} />
309 </span>
310 <span className="text-faint">
311 · <TimeAgo at={comment.createdAt} />
312 </span>
313 </span>
314 </div>
315 );
316}
317
318/**
319 * The conversation of an issue or a pull request, oldest first: what
320 * people and agents wrote, and between those, what happened.
321 */
322export function CommentList({
323 comments,
324 review,
325 base,
326}: {
327 comments: Comment[];
328 review?: Review;
329 /** The repository's path, so that `#12` in an event can be linked. */
330 base?: string;
331}) {
332 return (
333 <>
334 {comments.map((comment) => {
335 if (comment.kind === "event") {
336 return <TimelineEvent key={comment.id} comment={comment} base={base} filesUrl={review?.changesUrl} />;
337 }
338 const verdict = comment.verdict && VERDICTS[comment.verdict];
339 return (
340 <TimelineItem
341 key={comment.id}
342 author={comment.author.username}
343 at={comment.createdAt}
344 action={
345 verdict ? (
346 <span className={`flex items-center gap-1 font-medium ${verdict.style}`}>
347 {comment.verdict === "approve" ? (
348 <CircleCheck size={14} />
349 ) : (
350 <CircleSlash size={14} />
351 )}
352 {verdict.label}
353 </span>
354 ) : (
355 <span>commented</span>
356 )
357 }
358 aside={
359 comment.path &&
360 review && (
361 <Link
362 to={`${review.changesUrl}#file-${comment.path}`}
363 className="block truncate font-mono text-xs text-faint hover:text-fg"
364 >
365 {comment.path}
366 {comment.line != null && `:${comment.line}`}
367 </Link>
368 )
369 }
370 >
371 {comment.body && <Markdown source={comment.body} repo={repoAt(base)} />}
372 </TimelineItem>
373 );
374 })}
375 </>
376 );
377}
378
379/** Where a signed-in person writes a comment, or a review. */
380export function CommentForm({
381 author,
382 resetKey,
383 review,
384}: {
385 /** The viewer's username, or null if they are signed out. */
386 author: string | null;
387 /** Changes when a comment is added, which clears the box. */
388 resetKey: number;
389 review?: Review;
390}) {
391 if (!author) {
392 return (
393 <p className="text-sm text-muted">
394 <Link to="/login" className="text-fg underline underline-offset-4">
395 Sign in
396 </Link>{" "}
397 to comment.
398 </p>
399 );
400 }
401 return (
402 <div className="flex gap-3">
403 <span className="mt-1 hidden shrink-0 sm:block">
404 <Avatar name={author} size={32} />
405 </span>
406 <Form method="post" className="min-w-0 grow space-y-2" key={resetKey}>
407 <input type="hidden" name="action" value="comment" />
408 <MentionTextarea
409 name="body"
410 rows={3}
411 placeholder={
412 review?.canJudge
413 ? "Leave a comment, or a review. Markdown works."
414 : "Leave a comment. Markdown works."
415 }
416 />
417 <div className="flex flex-wrap gap-2">
418 <Button type="submit">Comment</Button>
419 {review?.canJudge && (
420 <>
421 <Button variant="quiet" type="submit" name="verdict" value="approve">
422 <CircleCheck size={14} className="text-accent" />
423 Approve
424 </Button>
425 <Button variant="quiet" type="submit" name="verdict" value="request_changes">
426 <CircleSlash size={14} className="text-danger" />
427 Request changes
428 </Button>
429 </>
430 )}
431 </div>
432 </Form>
433 </div>
434 );
435}
436
437/**
438 * Checkboxes for choosing people: the workspace's members, anyone already
439 * chosen, and a box for other usernames. Posts `name` for each ticked and
440 * `others` for the rest.
441 */
442export function PeoplePicker({
443 name,
444 members,
445 chosen,
446}: {
447 name: string;
448 members: string[];
449 chosen: string[];
450}) {
451 const people = [...new Set([...members, ...chosen])];
452 return (
453 <>
454 {people.length > 0 && (
455 <div className="space-y-1.5">
456 {people.map((person) => (
457 <CheckboxOption
458 key={person}
459 name={name}
460 value={person}
461 defaultChecked={chosen.includes(person)}
462 className="items-center"
463 labelClassName="flex items-center gap-2"
464 label={
465 <>
466 <Avatar name={person} size={18} />
467 <span className="font-mono text-xs">{person}</span>
468 </>
469 }
470 />
471 ))}
472 </div>
473 )}
474 <input
475 name="others"
476 placeholder="Other usernames, comma separated"
477 autoComplete="off"
478 data-1p-ignore
479 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"
480 />
481 </>
482 );
483}
484
485/** Where each reviewer stands: their most recent verdict. */
486export function verdicts(comments: Comment[]): { reviewer: string; verdict: NonNullable<Comment["verdict"]> }[] {
487 const latest = new Map<string, NonNullable<Comment["verdict"]>>();
488 for (const comment of comments) {
489 if (comment.verdict) latest.set(comment.author.username, comment.verdict);
490 }
491 return [...latest].map(([reviewer, verdict]) => ({ reviewer, verdict }));
492}
493
494/** `2 files +12 −3`: the size of a pull request's change. */
495export function ChangeSize({ files }: { files: Pull["files"] }) {
496 if (files.length === 0) return null;
497 const additions = files.reduce((sum, file) => sum + file.additions, 0);
498 const deletions = files.reduce((sum, file) => sum + file.deletions, 0);
499 return (
500 <span className="flex shrink-0 items-center gap-1.5 font-mono text-xs text-faint">
501 <span>
502 {files.length} {files.length === 1 ? "file" : "files"}
503 </span>
504 <span className="text-accent">+{additions}</span>
505 <span className="text-danger">−{deletions}</span>
506 </span>
507 );
508}
509
510/** The words of a Markdown text, without its markup, for a one-line preview. */
511export function plainText(markdown: string): string {
512 return markdown
513 .replace(/```[\s\S]*?```/g, " ")
514 .replace(/^\s{0,3}(#{1,6}|[-*+]|\d+\.)\s+/gm, "")
515 .replace(/!?\[([^\]]*)\]\([^)]*\)/g, "$1")
516 .replace(/[*_`~]/g, "")
517 .replace(/\s+/g, " ")
518 .trim();
519}