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