open-swe/ui/src/components/agents/ReviewCommentsMenu.tsx
Johannes du Plessis 9370a8c7f4
feat: inline PR comments in the reviews UI (#1600)
* feat: inline PR comments in the reviews UI

Click the diff gutter "+" on a line to open an inline comment composer
(rendered like the finding card via a Pierre annotation); submitting
posts a real inline PR review comment as the signed-in user through a
new POST /reviews/{owner}/{repo}/{number}/comments. The "+" press-drag →
"Add to Chat" selection path is unchanged.

* feat: GitHub-parity comment box, PR comments dropdown, collapse nav

- Comment composer now mirrors GitHub's box: Write/Preview tabs (markdown
  rendered via the existing Markdown component) and a markdown toolbar
  (heading, bold, italic, quote, code, link, bulleted/numbered/task list).
- Surface other people's inline PR comments in a Devin-style dropdown in the
  review header (search + link to the thread on GitHub). New
  GET /reviews/{owner}/{repo}/{number}/comments lists them and flags the
  reviewer's own (marker-bearing) comments so they're filtered out.
- Collapse the global nav by default on a review detail page, restoring the
  prior preference on leave.

* feat: bigger comment-toolbar icons; open dropdown comments inline

- Enlarge the markdown toolbar glyphs (Phosphor) in the comment composer —
  they were rendering at 10px.
- Clicking a comment in the PR comments dropdown now opens it inline in the
  diff as a read-only finding-style card (InlineComment), scrolling its line
  into view, instead of navigating to GitHub. Falls back to GitHub when the
  comment's file/line isn't in the current diff.

* fix: drive "Add to Chat" from native text selection

The gutter "+" is now comment-only; wiring its click to the composer
conflicted with its old double-duty as the drag-to-select handle, which
broke selection → "Add to Chat". Switch to Devin's model: disable Pierre's
interactive line selection and instead map a native text highlight in the
diff to a line range (via the data-line / data-line-type attributes Pierre
stamps on each line, read from the diff's open shadow root) to show the
"Add to Chat" popup. ⌘L and the existing attachment/popup path are unchanged.

* feat: gutter "+" drag selects a range for multi-line comments

Re-enable Pierre's gutter line selection so dragging the "+" down the
gutter comments across a range (click still comments on a single line);
onLineSelectionEnd routes the range to the composer. Native code-text
selection still drives "Add to Chat" — Pierre only line-selects from the
gutter, and onLineSelectionEnd bails when a native text selection is
present, so a code highlight never opens the composer.

* fix: keep the range highlighted while its comment composer is open

Previously opening the composer cleared the selection, so the lines being
commented on lost their highlight. Drive the controlled selection from the
open comment draft's range so the rows stay highlighted until the composer
is closed.

* fix: address PR review — paginate comments, fall back for outdated ones

- list_review_comments now pages through all PR review comments (bounded by
  _MAX_REVIEW_COMMENT_PAGES) instead of returning only the first 100, so older
  comments still show in the dropdown.
- Surface GitHub's outdated flag (position == null) as is_outdated; opening such
  a comment (or one whose line isn't in the diff) now opens it on GitHub instead
  of silently rendering nothing, plus a timeout fallback if the annotation never
  mounts (e.g. collapsed context).
2026-06-23 16:01:46 -07:00

167 lines
6 KiB
TypeScript

import { useEffect, useMemo, useRef, useState } from "react"
import { useQuery } from "@tanstack/react-query"
import { ChatCircleIcon, MagnifyingGlassIcon } from "@phosphor-icons/react"
import type { PrReviewComment } from "@/lib/api"
import { api } from "@/lib/api"
import { cn } from "@/lib/utils"
function basename(path: string): string {
const idx = path.lastIndexOf("/")
return idx === -1 ? path : path.slice(idx + 1)
}
// Devin-style dropdown surfacing inline PR comments left by people (the
// reviewer's own findings already render inline + in the side panel, so they're
// filtered out). Each entry links to the comment thread on GitHub.
export function ReviewCommentsMenu({
owner,
repo,
number,
onSelect,
}: {
owner: string
repo: string
number: number
onSelect: (comment: PrReviewComment) => void
}) {
const [open, setOpen] = useState(false)
const [query, setQuery] = useState("")
const wrapperRef = useRef<HTMLDivElement | null>(null)
const comments = useQuery({
queryKey: ["reviewComments", owner, repo, number],
queryFn: () => api.listReviewComments(owner, repo, number),
enabled: Number.isFinite(number),
staleTime: 30_000,
})
const otherComments = useMemo(
() => (comments.data?.comments ?? []).filter((c) => !c.is_open_swe),
[comments.data]
)
const filtered = useMemo(() => {
const q = query.trim().toLowerCase()
if (!q) return otherComments
return otherComments.filter((c) =>
`${c.author} ${c.path} ${c.body}`.toLowerCase().includes(q)
)
}, [otherComments, query])
useEffect(() => {
if (!open) return
const onPointerDown = (event: PointerEvent) => {
if (
event.target instanceof Node &&
!wrapperRef.current?.contains(event.target)
) {
setOpen(false)
}
}
const onKeyDown = (event: KeyboardEvent) => {
if (event.key === "Escape") setOpen(false)
}
window.addEventListener("pointerdown", onPointerDown)
window.addEventListener("keydown", onKeyDown)
return () => {
window.removeEventListener("pointerdown", onPointerDown)
window.removeEventListener("keydown", onKeyDown)
}
}, [open])
const count = otherComments.length
return (
<div ref={wrapperRef} className="relative">
<button
type="button"
onClick={() => setOpen((value) => !value)}
aria-label="PR comments"
aria-expanded={open}
className={cn(
"inline-flex items-center gap-1.5 rounded-md border border-border px-2 py-1 text-xs text-muted-foreground hover:text-foreground",
open && "text-foreground"
)}
>
<ChatCircleIcon className="size-3.5" />
<span>Comments</span>
{count > 0 && (
<span className="rounded bg-muted px-1 text-[10px] font-medium text-foreground">
{count}
</span>
)}
</button>
{open && (
<div className="absolute top-full right-0 z-50 mt-1 w-96 overflow-hidden rounded-md border border-border bg-popover text-popover-foreground shadow-md">
<div className="flex items-center gap-1.5 border-b border-border px-2 py-1.5">
<MagnifyingGlassIcon className="size-3.5 shrink-0 text-muted-foreground" />
<input
value={query}
onChange={(event) => setQuery(event.target.value)}
placeholder="Search comments"
className="w-full bg-transparent text-xs outline-none placeholder:text-muted-foreground"
/>
</div>
<div className="max-h-96 overflow-y-auto">
{comments.isLoading ? (
<p className="px-3 py-4 text-center text-xs text-muted-foreground">
Loading…
</p>
) : comments.isError ? (
<p className="px-3 py-4 text-center text-xs text-destructive">
Failed to load comments
</p>
) : filtered.length === 0 ? (
<p className="px-3 py-4 text-center text-xs text-muted-foreground">
{otherComments.length === 0
? "No comments yet"
: "No matching comments"}
</p>
) : (
<ul className="divide-y divide-border">
{filtered.map((comment) => (
<li key={comment.id}>
<button
type="button"
onClick={() => {
onSelect(comment)
setOpen(false)
}}
className="flex w-full gap-2 px-3 py-2 text-left hover:bg-muted/50"
>
{comment.author_avatar_url ? (
<img
src={comment.author_avatar_url}
alt=""
className="mt-0.5 size-4 shrink-0 rounded-full"
/>
) : (
<span className="mt-0.5 size-4 shrink-0 rounded-full bg-muted" />
)}
<div className="min-w-0 flex-1">
<div className="flex items-center gap-1.5 text-[11px]">
<span className="font-medium text-foreground">
{comment.author}
</span>
{comment.path && (
<span className="truncate font-mono text-muted-foreground">
{basename(comment.path)}
{comment.line !== null ? `:${comment.line}` : ""}
</span>
)}
</div>
<p className="mt-0.5 line-clamp-2 text-xs text-muted-foreground">
{comment.body}
</p>
</div>
</button>
</li>
))}
</ul>
)}
</div>
</div>
)}
</div>
)
}