121 lines · 6.0 KBRaw
| 1 | import { Ago, Badge, Banner, Button, InputArea, LayerCard } from "@refs/design-system"; |
| 2 | import { ArrowCounterClockwiseIcon, CheckCircleIcon } from "@refs/design-system/icons"; |
| 3 | import type { Attribution, LineRange, MentionState, OnlineAgent, ReviewThread, TimelineItem } from "@refs/backend/contract"; |
| 4 | import { useEffect, useState } from "react"; |
| 5 | import { Link, useFetcher } from "react-router"; |
| 6 | import { MentionStates, MentionTools } from "./mentions"; |
| 7 | import { useOnSuccess } from "./use-on-success"; |
| 8 | import { actorName } from "./attribution"; |
| 9 | import { ActorMark, ActorName } from "./timeline"; |
| 10 | |
| 11 | export type ThreadComment = Extract<TimelineItem, { kind: "pull.commented" }>; |
| 12 | |
| 13 | export function rangeLabel(range: LineRange): string { |
| 14 | const label = (side: LineRange["side"], line: number) => `${side === "deletions" ? "L" : "R"}${line}`; |
| 15 | return range.start === range.end && range.side === range.endSide ? label(range.side, range.start) : `${label(range.side, range.start)}–${label(range.endSide, range.end)}`; |
| 16 | } |
| 17 | |
| 18 | function StatusBadge({ thread, now }: { thread: ReviewThread; now: number }) { |
| 19 | if (thread.status === "resolved") return <Badge variant="success">Resolved{thread.resolved && <> by {actorName(thread.resolved.by)} <Ago at={thread.resolved.at} now={now} /></>}</Badge>; |
| 20 | if (thread.status === "addressed") |
| 21 | return ( |
| 22 | <Badge variant="primary"> |
| 23 | {thread.addressed ? `${actorName(thread.addressed.by)} says ${thread.addressed.commit.slice(0, 7)} addresses this` : "Addressed"} · awaiting confirmation |
| 24 | </Badge> |
| 25 | ); |
| 26 | return <Badge variant="secondary">Open</Badge>; |
| 27 | } |
| 28 | |
| 29 | /** |
| 30 | * A review thread: the lines or decision it is about, its comments, a reply box, and, for people with write access, |
| 31 | * Resolve or Reopen. Posts `thread-reply`, `thread-resolve` and `thread-reopen` to the current route. |
| 32 | */ |
| 33 | export function ReviewThreadCard({ |
| 34 | thread, |
| 35 | comments, |
| 36 | pullBase, |
| 37 | agents, |
| 38 | canResolve, |
| 39 | mentionsOf, |
| 40 | now, |
| 41 | }: { |
| 42 | thread: ReviewThread; |
| 43 | comments: readonly ThreadComment[]; |
| 44 | pullBase: string; |
| 45 | agents: readonly OnlineAgent[]; |
| 46 | canResolve: boolean; |
| 47 | mentionsOf: (comment: string) => readonly MentionState[]; |
| 48 | now: number; |
| 49 | }) { |
| 50 | const fetcher = useFetcher<{ intent: string; error: string | null }>(); |
| 51 | const [body, setBody] = useState(""); |
| 52 | const [round, setRound] = useState(0); |
| 53 | const [open, setOpen] = useState(thread.status !== "resolved"); |
| 54 | const busy = fetcher.state !== "idle"; |
| 55 | useOnSuccess(fetcher, (answer) => { |
| 56 | if (answer.intent !== "thread-reply") return; |
| 57 | setBody(""); |
| 58 | setRound((value) => value + 1); |
| 59 | }); |
| 60 | useEffect(() => setOpen(thread.status !== "resolved"), [thread.status]); |
| 61 | |
| 62 | return ( |
| 63 | <LayerCard className="min-w-0 flex-1"> |
| 64 | <LayerCard.Secondary className="flex flex-wrap items-center justify-between gap-2 py-2 font-normal"> |
| 65 | <Link to={thread.range ? `${pullBase}/files` : `${pullBase}/reasoning#${thread.decision}`} className="min-w-0 truncate font-mono text-xs text-accent-text hover:underline"> |
| 66 | {thread.range ? `${thread.path}:${rangeLabel(thread.range)}` : `Decision ${thread.decision}`} |
| 67 | </Link> |
| 68 | <span className="flex items-center gap-2"> |
| 69 | {thread.outdated && <Badge variant="outline">Outdated</Badge>} |
| 70 | <StatusBadge thread={thread} now={now} /> |
| 71 | {thread.status === "resolved" && ( |
| 72 | <Button type="button" variant="ghost" size="sm" onClick={() => setOpen(!open)} aria-expanded={open}> |
| 73 | {open ? "Hide" : "Show"} |
| 74 | </Button> |
| 75 | )} |
| 76 | </span> |
| 77 | </LayerCard.Secondary> |
| 78 | {open && ( |
| 79 | <LayerCard.Primary className="gap-0 p-0"> |
| 80 | {thread.range ? ( |
| 81 | <pre className="m-0 overflow-x-auto bg-sunken px-4 py-2.5 font-mono text-xs shadow-[inset_0_-1px_0_var(--hairline)]">{thread.code}</pre> |
| 82 | ) : ( |
| 83 | <p className="m-0 bg-sunken px-4 py-2.5 text-compact font-medium shadow-[inset_0_-1px_0_var(--hairline)]">{thread.code}</p> |
| 84 | )} |
| 85 | {comments.map((comment) => ( |
| 86 | <div key={comment.id} className="px-4 py-3 shadow-[inset_0_-1px_0_var(--hairline)]"> |
| 87 | <p className="m-0 mb-2 flex flex-wrap items-center gap-x-2 gap-y-1 text-compact text-muted"> |
| 88 | <ActorMark by={comment.by} /> |
| 89 | <ActorName by={comment.by} /> |
| 90 | <Ago at={comment.at} now={now} /> |
| 91 | </p> |
| 92 | <article className="markdown" dangerouslySetInnerHTML={{ __html: comment.data.html }} /> |
| 93 | <MentionStates mentions={mentionsOf(comment.id)} /> |
| 94 | </div> |
| 95 | ))} |
| 96 | <fetcher.Form key={round} method="post" className="flex flex-col gap-3 p-4"> |
| 97 | <input type="hidden" name="thread" value={thread.id} /> |
| 98 | <InputArea aria-label="Reply" name="body" value={body} onValueChange={setBody} rows={2} placeholder="Reply…" /> |
| 99 | {body && <MentionTools agents={agents} body={body} onBody={setBody} canAssign />} |
| 100 | {fetcher.data?.error && <Banner variant="error" description={fetcher.data.error} />} |
| 101 | <div className="flex flex-wrap justify-end gap-2"> |
| 102 | {canResolve && thread.status !== "resolved" && ( |
| 103 | <Button type="submit" name="intent" value="thread-resolve" variant="secondary" icon={<CheckCircleIcon size={16} />} loading={busy && fetcher.formData?.get("intent") === "thread-resolve"}> |
| 104 | {thread.status === "addressed" ? "Confirm and resolve" : "Resolve"} |
| 105 | </Button> |
| 106 | )} |
| 107 | {canResolve && thread.status !== "open" && ( |
| 108 | <Button type="submit" name="intent" value="thread-reopen" variant="secondary" icon={<ArrowCounterClockwiseIcon size={16} />} loading={busy && fetcher.formData?.get("intent") === "thread-reopen"}> |
| 109 | Reopen |
| 110 | </Button> |
| 111 | )} |
| 112 | <Button type="submit" name="intent" value="thread-reply" variant="primary" disabled={!body.trim()} loading={busy && fetcher.formData?.get("intent") === "thread-reply"}> |
| 113 | Reply |
| 114 | </Button> |
| 115 | </div> |
| 116 | </fetcher.Form> |
| 117 | </LayerCard.Primary> |
| 118 | )} |
| 119 | </LayerCard> |
| 120 | ); |
| 121 | } |