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.

805 linesCodeBlameRaw
1import { Bot, CircleCheck, CircleSlash, Pencil, Trash2 } from "lucide-react";
2import { type CSSProperties, Fragment, type ReactNode } from "react";
3import { Form, Link } from "react-router";
4
5import type { AgentRef, Comment, Pull, State, WorkItem } from "@g1t/contracts";
6
7import { mayChangeComment } from "../lib/comments";
8import { messageParts } from "../lib/commit-message";
9import { chipStyle } from "../lib/labels";
10import { repoAt } from "../lib/markdown-plugins";
11import type { RefContext } from "../lib/refs";
12import { CommitChip, SessionChip } from "./chat/ref-chip";
13import { RefLink, repoScope } from "./commit-message";
14import { Markdown } from "./markdown";
15import { Reactions } from "./reactions";
16import { ItemIcon, PullIcon } from "./work-icons";
17import { sessionPath } from "../lib/work-items";
18import { MentionTextarea } from "./mention-textarea";
19import { ErrorText, SubmitButton, TimeAgo } from "./ui";
20import { Badge } from "./ui/badge";
21import {
22 AlertDialog,
23 AlertDialogAction,
24 AlertDialogCancel,
25 AlertDialogContent,
26 AlertDialogDescription,
27 AlertDialogFooter,
28 AlertDialogHeader,
29 AlertDialogTitle,
30 AlertDialogTrigger,
31} from "./ui/alert-dialog";
32import { Avatar } from "./ui/avatar";
33import { Button } from "./ui/button";
34import { Card } from "./ui/card";
35import { CheckboxOption } from "./ui/checkbox";
36import { Hint } from "./ui/hint";
37import { UserCard } from "./user-card";
38
39export { ItemIcon, PullIcon };
40
41/** Hues for the labels every repository starts with. */
42const LABEL_HUES: Record<string, number> = {
43 bug: 25,
44 feature: 150,
45 docs: 240,
46 chore: 80,
47 question: 310,
48};
49
50/** A stable hue for any other label, from its name. */
51function hueFor(name: string): number {
52 let hash = 0;
53 for (const char of name) hash = (hash * 31 + char.charCodeAt(0)) % 360;
54 return hash;
55}
56
57/**
58 * A label's chip: in its own color when the page knows it (the
59 * repository's labels, by name), else in a hue chosen from its name.
60 */
61export function Label({ name, color }: { name: string; color?: string | null }) {
62 const hue = LABEL_HUES[name] ?? hueFor(name);
63 const own = chipStyle(color);
64 const style: CSSProperties =
65 Object.keys(own).length > 0
66 ? own
67 : {
68 // Light text on a dark page, deep text on a light one (lib/theme.ts).
69 color: `light-dark(oklch(0.46 0.13 ${hue}), oklch(0.84 0.11 ${hue}))`,
70 borderColor: `light-dark(oklch(0.55 0.13 ${hue} / 0.35), oklch(0.84 0.11 ${hue} / 0.35))`,
71 backgroundColor: `light-dark(oklch(0.55 0.13 ${hue} / 0.08), oklch(0.84 0.11 ${hue} / 0.1))`,
72 };
73 return (
74 <span style={style} className="inline-flex shrink-0 items-center rounded-full border px-2 py-px text-xs font-medium">
75 {name}
76 </span>
77 );
78}
79
80/** The people a work item is assigned to, as overlapping avatars. */
81export function AssigneeStack({ people }: { people: string[] }) {
82 if (people.length === 0) return null;
83 const shown = people.slice(0, 3);
84 return (
85 <Hint label={`Assigned to ${people.join(", ")}`}>
86 <span className="flex shrink-0 items-center">
87 <span className="sr-only">Assigned to {people.join(", ")}</span>
88 {shown.map((name, index) => (
89 <span key={name} className={`rounded-full ring-2 ring-bg ${index > 0 ? "-ml-1.5" : ""}`} aria-hidden>
90 <Avatar name={name} size={18} />
91 </span>
92 ))}
93 {people.length > shown.length && (
94 <span className="ml-1 text-xs text-faint" aria-hidden>
95 +{people.length - shown.length}
96 </span>
97 )}
98 </span>
99 </Hint>
100 );
101}
102
103/** The agent working on a work item now. */
104export function Assignee({ agent }: { agent: string }) {
105 return (
106 <Hint label={`Assigned to ${agent}`}>
107 <Badge tone="accent" className="text-xs">
108 <Bot size={12} />
109 <span className="sr-only">Assigned to </span>
110 {agent}
111 </Badge>
112 </Hint>
113 );
114}
115
116const PILL = "inline-flex shrink-0 items-center gap-1.5 rounded-full border px-2.5 py-1 text-xs font-medium";
117
118/** The large state badge at the top of a work item. */
119export function ItemState({ item }: { item: Pick<WorkItem, "state" | "reason"> }) {
120 const style =
121 item.state === "open"
122 ? "border-success/30 bg-success/10 text-success"
123 : item.reason === "not_planned"
124 ? "border-line bg-surface text-muted"
125 : "border-merged/30 bg-merged/10 text-merged";
126 return (
127 <span className={`${PILL} ${style}`}>
128 <ItemIcon item={item} size={14} />
129 {item.state === "open" ? "Open" : item.reason === "not_planned" ? "Closed as not planned" : "Closed"}
130 </span>
131 );
132}
133
134const PULL_STYLES: Record<Pull["status"], string> = {
135 draft: "border-line bg-surface text-muted",
136 open: "border-success/30 bg-success/10 text-success",
137 merged: "border-merged/30 bg-merged/10 text-merged",
138 closed: "border-danger/30 bg-danger/10 text-danger",
139};
140
141const PULL_NAMES: Record<Pull["status"], string> = {
142 draft: "Draft",
143 open: "Open",
144 merged: "Merged",
145 closed: "Closed",
146};
147
148export function PullState({ status }: { status: Pull["status"] }) {
149 return (
150 <span className={`${PILL} ${PULL_STYLES[status]}`}>
151 <PullIcon status={status} size={14} />
152 {PULL_NAMES[status]}
153 </span>
154 );
155}
156
157/** The open and closed switch above a list. `to` is the list's own path. */
158export function StateTabs({
159 to,
160 state,
161 query = "",
162 action,
163}: {
164 to: string;
165 state: State;
166 /** Other query parameters to keep, e.g. `label=bug`. */
167 query?: string;
168 action?: ReactNode;
169}) {
170 const link = (value: State) => `${to}?${[value === "open" ? "" : "state=closed", query].filter(Boolean).join("&")}`;
171 const tab = (value: State, label: string) => (
172 <Link
173 to={link(value)}
174 aria-current={state === value ? "page" : undefined}
175 className={
176 "inline-flex h-full items-center rounded-[5px] px-2.5 text-sm transition-colors " +
177 (state === value ? "bg-raised font-medium text-fg" : "text-muted hover:text-fg")
178 }
179 >
180 {label}
181 </Link>
182 );
183 return (
184 <div className="flex flex-wrap items-center justify-between gap-3">
185 {/* A filter, the height of the filters beside it: the section's views above it outweigh it. */}
186 <Card tone="plain" radius="lg" className="flex h-8 gap-0.5 rounded-md p-0.5">
187 {tab("open", "Open")}
188 {tab("closed", "Closed")}
189 </Card>
190 {action}
191 </div>
192 );
193}
194
195const VERDICTS = {
196 approve: { label: "approved these changes", style: "text-success" },
197 request_changes: { label: "requested changes", style: "text-danger" },
198} as const;
199
200/**
201 * A person's name (or `children`) linking to their profile at `/u/<name>`.
202 * g1t, and names that are not usernames, stay plain text.
203 */
204export function PersonLink({ name, className, label, children }: { name: string; className?: string; label?: string; children?: ReactNode }) {
205 // g1t itself has no profile; its name carries a small label instead, and
206 // its card says what it is.
207 if (name === "g1t" && !children) {
208 return (
209 <UserCard username="g1t">
210 <span tabIndex={0} className="inline-flex items-baseline gap-1 rounded outline-none focus-visible:ring-2 focus-visible:ring-accent">
211 <span className={className}>g1t</span>
212 <span className="rounded border border-line px-1 text-[0.625rem] leading-[1.35] font-medium text-muted">bot</span>
213 </span>
214 </UserCard>
215 );
216 }
217 if (name === "g1t") {
218 return (
219 <UserCard username="g1t">
220 <span className={className}>{children}</span>
221 </UserCard>
222 );
223 }
224 // Ghost, a deleted account, has no profile and no card.
225 if (name === "ghost" || !/^[a-z0-9-]{1,39}$/i.test(name)) {
226 return <span className={className}>{children ?? name}</span>;
227 }
228 return (
229 <UserCard username={name}>
230 <Link to={`/u/${name.toLowerCase()}`} className={className} aria-label={label}>
231 {children ?? name}
232 </Link>
233 </UserCard>
234 );
235}
236
237/**
238 * Comments in order, then the box to add one. On a pull request, `review`
239 * says where its changes are shown and whether the viewer may give a
240 * verdict.
241 */
242/**
243 * One entry in a conversation: who, what they did and when, then what they
244 * wrote. The author's avatar sits beside it, as on any forge.
245 */
246export function TimelineItem({
247 id,
248 author,
249 bot = false,
250 agent,
251 actingFor = agent?.actingFor,
252 action,
253 at,
254 edited,
255 aside,
256 children,
257}: {
258 /** Its anchor, so a link can point at it (`#cmt_…`). */
259 id?: string;
260 author: string;
261 /** g1t, or an agent in someone's harness: drawn as an agent, with no profile to link to. */
262 bot?: boolean;
263 /**
264 * Set when one of the workspace's former agents wrote it, as itself:
265 * shown by its name with an Agent badge, and whom it acted for, in place
266 * of `author`.
267 */
268 agent?: { ref: AgentRef; actingFor?: string | null };
269 /** Whom g1t acted for, when a session g1t started for someone wrote it. */
270 actingFor?: string | null;
271 /** What they did, after their name: "commented", "opened this". */
272 action: ReactNode;
273 at?: string;
274 /** When what they wrote was last edited, if it was. */
275 edited?: string | null;
276 /** Shown at the right of the header. */
277 aside?: ReactNode;
278 children?: ReactNode;
279}) {
280 const face = (size: number) => (agent ? <Avatar name={agent.ref.displayName} agent size={size} /> : <Avatar name={author} size={size} agent={bot} />);
281 return (
282 <div id={id} className="flex scroll-mt-20 gap-3">
283 <span className="mt-1 hidden shrink-0 sm:block">
284 {agent || bot ? (
285 face(32)
286 ) : (
287 <PersonLink name={author} label={`${author}'s profile`}>
288 {face(32)}
289 </PersonLink>
290 )}
291 </span>
292 <Card asChild className="min-w-0 grow overflow-hidden">
293 <article>
294 <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">
295 <span className="sm:hidden">{face(18)}</span>
296 {agent ? (
297 <AgentName agent={agent.ref} />
298 ) : bot ? (
299 <span className="font-medium text-fg">{author}</span>
300 ) : (
301 <PersonLink name={author} className="font-medium text-fg hover:underline" />
302 )}
303 {action}
304 {at && <TimeAgo at={at} />}
305 {actingFor && (
306 <span className="text-xs text-faint">
307 on behalf of{" "}
308 <PersonLink name={actingFor} className="hover:text-fg hover:underline">
309 @{actingFor}
310 </PersonLink>
311 </span>
312 )}
313 {edited && (
314 <Hint
315 label={
316 <>
317 Edited <TimeAgo at={edited} />
318 </>
319 }
320 >
321 <span tabIndex={0} className="text-xs text-faint">
322 edited
323 </span>
324 </Hint>
325 )}
326 {aside && <span className="ml-auto min-w-0">{aside}</span>}
327 </header>
328 {children && <div className="px-4 py-3">{children}</div>}
329 </article>
330 </Card>
331 </div>
332 );
333}
334
335/** A former agent's name, with an Agent badge. */
336function AgentName({ agent }: { agent: AgentRef }) {
337 return (
338 <span className="inline-flex items-center gap-1.5">
339 <span className="font-medium text-fg">{agent.displayName}</span>
340 <Badge tone="accent" className="px-1.5 text-[0.625rem] leading-[1.35]">
341 Agent
342 </Badge>
343 </span>
344 );
345}
346
347/** That an agent's review is advisory, and why it counts toward nothing. */
348function AdvisoryChip() {
349 return (
350 <Hint label="Agent reviews don't count toward required approvals">
351 <span tabIndex={0} className="inline-flex rounded-full outline-none focus-visible:ring-2 focus-visible:ring-accent">
352 <Badge tone="neutral">Advisory</Badge>
353 </span>
354 </Hint>
355 );
356}
357
358type Review = { changesUrl: string; canJudge: boolean };
359
360/**
361 * Text with each `#12` linked to the work item or pull request it names
362 * (its card on hover), read in the repository at `base` or else in
363 * `workspace`; each full commit hash linked to its commit, and each
364 * file named in backticks shown as code, linked to its diff when
365 * `filesUrl` says where the changes are. Without a repository, a commit or
366 * a session (both a repository's) is shown by its short id, unlinked.
367 */
368function WithReferences({ text, base, workspace, filesUrl }: { text: string; base?: string; workspace?: string; filesUrl?: string }) {
369 if (!base && !workspace) return <>{text}</>;
370 const here: RefContext = base ? repoScope(base) : { workspace: workspace! };
371 return (
372 <>
373 {text.split(/(`[^`\n]+`)/).map((chunk, index) =>
374 /^`[^`]+`$/.test(chunk) ? (
375 filesUrl ? (
376 <Link key={index} to={`${filesUrl}#file-${chunk.slice(1, -1)}`} className="font-mono text-xs text-fg hover:underline">
377 {chunk.slice(1, -1)}
378 </Link>
379 ) : (
380 <code key={index} className="font-mono text-xs text-fg">
381 {chunk.slice(1, -1)}
382 </code>
383 )
384 ) : (
385 <Fragment key={index}>
386 {messageParts(chunk).map((part, at) =>
387 part.kind === "ref" ? (
388 <RefLink key={at} scope={part.repo ? repoScope(part.repo) : here} number={part.number} className="font-medium text-fg hover:underline">
389 {part.text}
390 </RefLink>
391 ) : part.kind === "commit" ? (
392 // The commit that closed or merged it, by its short hash.
393 base ? (
394 <CommitChip key={at} href={`${base}/commit/${part.sha}`} sha={part.sha} />
395 ) : (
396 <code key={at} className="font-mono text-xs text-fg">
397 {part.sha.slice(0, 7)}
398 </code>
399 )
400 ) : part.kind === "session" ? (
401 // The session it was closed in.
402 base ? (
403 <SessionChip key={at} href={sessionPath(base.split("/").filter(Boolean)[0] ?? "", part.id)} id={part.id} />
404 ) : (
405 <code key={at} className="font-mono text-xs text-fg">
406 {part.text}
407 </code>
408 )
409 ) : (
410 part.text
411 ),
412 )}
413 </Fragment>
414 ),
415 )}
416 </>
417 );
418}
419
420/**
421 * One line on the conversation's rail: a mark on the rail (a dot unless
422 * `mark` says otherwise), who, what and when.
423 */
424function TimelineRow({ comment, mark, children }: { comment: Comment; mark?: ReactNode; children: ReactNode }) {
425 return (
426 <div className="flex items-center gap-3 text-sm text-muted">
427 <span className="hidden w-8 shrink-0 justify-center sm:flex">{mark ?? <span className="size-2 rounded-full border border-line-strong bg-bg" />}</span>
428 <span className="flex min-w-0 flex-wrap items-center gap-x-1.5 gap-y-0.5">
429 {comment.agent ? (
430 <>
431 <Avatar name={comment.agent.displayName} agent size={16} />
432 <AgentName agent={comment.agent} />
433 </>
434 ) : (
435 <>
436 <Avatar name={comment.author.username} size={16} />
437 <PersonLink name={comment.author.username} className="font-medium text-fg hover:underline" />
438 </>
439 )}
440 {children}
441 {comment.actingFor && (
442 <span className="text-xs text-faint">
443 on behalf of{" "}
444 <PersonLink name={comment.actingFor.username} className="hover:text-fg hover:underline">
445 @{comment.actingFor.username}
446 </PersonLink>
447 </span>
448 )}
449 <span className="text-faint">
450 · <TimeAgo at={comment.createdAt} />
451 </span>
452 </span>
453 </div>
454 );
455}
456
457/** Something that happened, as one line on the conversation's rail. */
458function TimelineEvent({ comment, base, workspace, filesUrl }: { comment: Comment; base?: string; workspace?: string; filesUrl?: string }) {
459 return (
460 <TimelineRow comment={comment}>
461 <span>
462 <WithReferences text={comment.body} base={base} workspace={workspace} filesUrl={filesUrl} />
463 </span>
464 </TimelineRow>
465 );
466}
467
468/** A review's verdict, approved or changes requested, in its own colour. */
469function Verdict({ verdict }: { verdict: keyof typeof VERDICTS }) {
470 const { label, style } = VERDICTS[verdict];
471 return (
472 <span className={`flex items-center gap-1 font-medium ${style}`}>
473 {verdict === "approve" ? <CircleCheck size={14} /> : <CircleSlash size={14} />}
474 {label}
475 </span>
476 );
477}
478
479/**
480 * A review that gave a verdict and said nothing else: one line on the rail,
481 * not a comment with an empty body. There is nothing to edit, and a
482 * verdict is not deleted, so it has no actions.
483 */
484function TimelineVerdict({ comment, verdict }: { comment: Comment; verdict: keyof typeof VERDICTS }) {
485 return (
486 <TimelineRow
487 comment={comment}
488 mark={verdict === "approve" ? <CircleCheck size={16} className="text-success" /> : <CircleSlash size={16} className="text-danger" />}
489 >
490 <span className={VERDICTS[verdict].style}>{VERDICTS[verdict].label}</span>
491 {comment.advisory && <AdvisoryChip />}
492 </TimelineRow>
493 );
494}
495
496/** Edit and Delete, under a comment the viewer may change. */
497function CommentActions({ comment, can, error }: { comment: Comment; can: { edit: boolean; delete: boolean }; error?: string | null }) {
498 return (
499 <div className="mt-3 border-t border-line pt-2 text-sm">
500 <div className="flex flex-wrap items-center gap-1">
501 {can.edit && (
502 <details className="open:order-last open:w-full">
503 <summary className="inline-flex cursor-pointer list-none items-center gap-1 rounded-md px-1.5 py-1 text-xs text-faint hover:text-fg">
504 <Pencil size={12} />
505 Edit
506 </summary>
507 <Form method="post" className="mt-2 space-y-2">
508 <input type="hidden" name="action" value="edit-comment" />
509 <input type="hidden" name="comment" value={comment.id} />
510 <MentionTextarea name="body" rows={4} defaultValue={comment.body} />
511 <SubmitButton match={{ action: "edit-comment", comment: comment.id }} pending="Saving…">
512 Save
513 </SubmitButton>
514 </Form>
515 </details>
516 )}
517 {can.delete && (
518 <AlertDialog>
519 <AlertDialogTrigger asChild>
520 <Button type="button" variant="ghost" size="inline" className="gap-1 px-1.5 py-1 text-xs text-faint hover:text-danger font-normal">
521 <Trash2 size={12} />
522 Delete
523 </Button>
524 </AlertDialogTrigger>
525 <AlertDialogContent>
526 <Form method="post" className="grid gap-4">
527 <input type="hidden" name="action" value="delete-comment" />
528 <input type="hidden" name="comment" value={comment.id} />
529 <AlertDialogHeader>
530 <AlertDialogTitle>Delete this comment?</AlertDialogTitle>
531 <AlertDialogDescription>It is removed from the conversation for everyone. This cannot be undone.</AlertDialogDescription>
532 </AlertDialogHeader>
533 <AlertDialogFooter>
534 <AlertDialogCancel>Cancel</AlertDialogCancel>
535 <AlertDialogAction asChild>
536 <button type="submit">
537 <Trash2 size={14} />
538 Delete comment
539 </button>
540 </AlertDialogAction>
541 </AlertDialogFooter>
542 </Form>
543 </AlertDialogContent>
544 </AlertDialog>
545 )}
546 </div>
547 {error && <ErrorText>{error}</ErrorText>}
548 </div>
549 );
550}
551
552/** Something else in a conversation, put among its comments by when it happened: a session, say. */
553export type TimelineExtra = { id: string; at: string; node: ReactNode };
554
555/**
556 * The conversation of a work item or a pull request, oldest first: what
557 * people and agents wrote, and between those, what happened, with
558 * `extra` (its sessions) where each happened.
559 */
560export function CommentList({
561 comments,
562 review,
563 base,
564 workspace,
565 viewerId,
566 viewerName = null,
567 canModerate = false,
568 failed,
569 extra = [],
570}: {
571 comments: Comment[];
572 review?: Review;
573 /** The repository's path, so that `#12` in an event can be linked. */
574 base?: string;
575 /** For a work item's conversation, which no repository holds: the workspace `#12` is read in. */
576 workspace?: string;
577 /** The signed-in viewer, who may edit and delete their own comments. */
578 viewerId?: string | null;
579 /** Maintain and up: may edit and delete anyone's comments. */
580 canModerate?: boolean;
581 /** Why editing or deleting a comment was refused, under that comment. */
582 failed?: { comment: string; error?: string | null } | null;
583 /** The signed-in viewer's username, who may react; none to only show reactions. */
584 viewerName?: string | null;
585 extra?: TimelineExtra[];
586}) {
587 // Each extra goes before the first comment written after it.
588 const later = [...extra].sort((a, b) => Date.parse(a.at) - Date.parse(b.at));
589 const before = (at: string) => {
590 const due: TimelineExtra[] = [];
591 while (later.length > 0 && Date.parse(later[0].at) <= Date.parse(at)) due.push(later.shift()!);
592 return due.map((one) => <Fragment key={one.id}>{one.node}</Fragment>);
593 };
594 return (
595 <>
596 {comments.map((comment) => (
597 <Fragment key={comment.id}>
598 {before(comment.createdAt)}
599 {entry(comment)}
600 </Fragment>
601 ))}
602 {later.map((one) => (
603 <Fragment key={one.id}>{one.node}</Fragment>
604 ))}
605 </>
606 );
607
608 function entry(comment: Comment) {
609 if (comment.kind === "event") {
610 return <TimelineEvent key={comment.id} comment={comment} base={base} workspace={workspace} filesUrl={review?.changesUrl} />;
611 }
612 const verdict = comment.verdict;
613 // A verdict with nothing said and no line it is about is one line, not a card.
614 if (verdict && !comment.body.trim() && !comment.path) {
615 return <TimelineVerdict key={comment.id} comment={comment} verdict={verdict} />;
616 }
617 const can = mayChangeComment(comment, viewerId, canModerate);
618 return (
619 <TimelineItem
620 key={comment.id}
621 id={comment.id}
622 author={comment.author.username}
623 bot={comment.author.kind === "agent"}
624 agent={
625 comment.agent && {
626 ref: comment.agent,
627 actingFor: comment.actingFor?.username,
628 }
629 }
630 actingFor={comment.actingFor?.username}
631 at={comment.createdAt}
632 action={
633 <>
634 {verdict ? <Verdict verdict={verdict} /> : <span>{comment.advisory ? "reviewed" : "commented"}</span>}
635 {comment.advisory && <AdvisoryChip />}
636 </>
637 }
638 edited={comment.editedAt}
639 aside={
640 comment.path &&
641 review && (
642 <Link to={`${review.changesUrl}#file-${comment.path}`} className="block truncate font-mono text-xs text-faint hover:text-fg">
643 {comment.path}
644 {comment.line != null && `:${comment.line}`}
645 </Link>
646 )
647 }
648 >
649 {comment.body && <Markdown source={comment.body} repo={repoAt(base)} workspace={workspace} />}
650 <Reactions reactions={comment.reactions} commentId={comment.id} viewer={viewerName} className="mt-3" />
651 {(can.edit || can.delete) && <CommentActions comment={comment} can={can} error={failed?.comment === comment.id ? failed.error : null} />}
652 </TimelineItem>
653 );
654 }
655}
656
657/** Where a signed-in person writes a comment, or a review. */
658export function CommentForm({
659 author,
660 resetKey,
661 review,
662 handles,
663}: {
664 /** The viewer's username, or null if they are signed out. */
665 author: string | null;
666 /** Changes when a comment is added, which clears the box. */
667 resetKey: number;
668 review?: Review;
669 /** Teams the box offers to mention, as `@workspace/team`. */
670 handles?: readonly string[];
671}) {
672 if (!author) {
673 return (
674 <p className="text-sm text-muted">
675 <Link to="/login" className="text-fg underline underline-offset-4">
676 Sign in
677 </Link>{" "}
678 to comment.
679 </p>
680 );
681 }
682 return (
683 <div className="flex gap-3">
684 <span className="mt-1 hidden shrink-0 sm:block">
685 <Avatar name={author} size={32} />
686 </span>
687 <Form method="post" className="min-w-0 grow space-y-2" key={resetKey}>
688 <input type="hidden" name="action" value="comment" />
689 <MentionTextarea
690 name="body"
691 handles={handles}
692 rows={3}
693 placeholder={review?.canJudge ? "Leave a comment, or a review. Markdown works." : "Leave a comment. Markdown works."}
694 />
695 <div className="flex flex-wrap gap-2">
696 {/* A plain comment's verdict is none the action knows, so each button tells its own post apart. */}
697 <SubmitButton name="verdict" value="comment" match={{ action: "comment" }} pending="Commenting…">
698 Comment
699 </SubmitButton>
700 {review?.canJudge && (
701 <>
702 <SubmitButton variant="outline" name="verdict" value="approve" match={{ action: "comment" }} pending="Approving…">
703 <CircleCheck size={14} className="text-success" />
704 Approve
705 </SubmitButton>
706 <SubmitButton variant="outline" name="verdict" value="request_changes" match={{ action: "comment" }} pending="Requesting changes…">
707 <CircleSlash size={14} className="text-danger" />
708 Request changes
709 </SubmitButton>
710 </>
711 )}
712 </div>
713 </Form>
714 </div>
715 );
716}
717
718/**
719 * Checkboxes for choosing people: the workspace's members, anyone already
720 * chosen, and a box for other usernames. Posts `name` for each ticked and
721 * `others` for the rest.
722 */
723export function PeoplePicker({
724 name,
725 members,
726 chosen,
727 placeholder = "Other usernames, comma separated",
728}: {
729 name: string;
730 members: string[];
731 chosen: string[];
732 placeholder?: string;
733}) {
734 const people = [...new Set([...members, ...chosen])];
735 return (
736 <>
737 {people.length > 0 && (
738 <div className="space-y-1.5">
739 {people.map((person) => (
740 <CheckboxOption
741 key={person}
742 name={name}
743 value={person}
744 defaultChecked={chosen.includes(person)}
745 className="items-center"
746 labelClassName="flex items-center gap-2"
747 label={
748 <>
749 <Avatar name={person} size={18} />
750 <span className="font-mono text-xs">{person}</span>
751 </>
752 }
753 />
754 ))}
755 </div>
756 )}
757 <input
758 name="others"
759 placeholder={placeholder}
760 autoComplete="off"
761 data-1p-ignore
762 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"
763 />
764 </>
765 );
766}
767
768/**
769 * Where each reviewer stands: their most recent verdict. An agent's review
770 * is advisory and is not one of them.
771 */
772export function verdicts(comments: Comment[]): { reviewer: string; verdict: NonNullable<Comment["verdict"]> }[] {
773 const latest = new Map<string, NonNullable<Comment["verdict"]>>();
774 for (const comment of comments) {
775 if (comment.verdict && !comment.agent && !comment.advisory) latest.set(comment.author.username, comment.verdict);
776 }
777 return [...latest].map(([reviewer, verdict]) => ({ reviewer, verdict }));
778}
779
780/** `2 files +12 −3`: the size of a pull request's change. */
781export function ChangeSize({ files }: { files: Pull["files"] }) {
782 if (files.length === 0) return null;
783 const additions = files.reduce((sum, file) => sum + file.additions, 0);
784 const deletions = files.reduce((sum, file) => sum + file.deletions, 0);
785 return (
786 <span className="flex shrink-0 items-center gap-1.5 font-mono text-xs text-faint">
787 <span>
788 {files.length} {files.length === 1 ? "file" : "files"}
789 </span>
790 <span className="text-success">+{additions}</span>
791 <span className="text-danger">−{deletions}</span>
792 </span>
793 );
794}
795
796/** The words of a Markdown text, without its markup, for a one-line preview. */
797export function plainText(markdown: string): string {
798 return markdown
799 .replace(/```[\s\S]*?```/g, " ")
800 .replace(/^\s{0,3}(#{1,6}|[-*+]|\d+\.)\s+/gm, "")
801 .replace(/!?\[([^\]]*)\]\([^)]*\)/g, "$1")
802 .replace(/[*_`~]/g, "")
803 .replace(/\s+/g, " ")
804 .trim();
805}