Skip to content

Commit aed0216

Browse files
committed
refactor(web): resolved conversation avatars stay on the line as a show and hide toggle
The avatars no longer swap places with the card, so the focus handoff, per-card fold state and their expiry rules are gone.
1 parent 7cca228 commit aed0216

3 files changed

Lines changed: 72 additions & 169 deletions

File tree

‎apps/web/src/components/pullRequest/PullRequestCodeTab.browser.test.tsx‎

Lines changed: 16 additions & 46 deletions
Original file line numberDiff line numberDiff line change
@@ -356,7 +356,7 @@ it("answers a commit's diff link only once that commit's diff is shown", async (
356356
await vi.waitFor(() => expect(settled).toHaveBeenCalledWith(url));
357357
});
358358

359-
it("folds a resolved conversation into avatars on its line until it is opened", async () => {
359+
it("hides a resolved conversation behind avatars on its line until they are clicked", async () => {
360360
setup({ paged: false });
361361
__setClientSettingsForTests({ ...DEFAULT_CLIENT_SETTINGS, diffFilesCollapsed: false });
362362
using _preferences = {
@@ -415,65 +415,35 @@ it("folds a resolved conversation into avatars on its line until it is opened",
415415
/>
416416
);
417417
await using screen = await render(withThread(true), { container });
418-
const marker = () => screen.getByRole("button", { name: "Show resolved conversation" });
418+
const marker = screen.getByRole("button", { name: "Resolved conversation", exact: true });
419419
const card = (state: string) => screen.getByRole("button", { name: `${state} · 1 comment` });
420+
const reply = () => screen.getByRole("textbox", { name: "Reply to this conversation" });
420421

421-
await expect.element(marker()).toBeVisible();
422+
await expect.element(marker).toHaveAttribute("aria-expanded", "false");
422423
await expect.element(card("Resolved")).not.toBeInTheDocument();
423424

424-
await marker().click();
425+
await marker.click();
426+
await expect.element(marker).toHaveAttribute("aria-expanded", "true");
425427
await expect.element(card("Resolved")).toHaveAttribute("aria-expanded", "true");
426-
await expect.element(card("Resolved")).toHaveFocus();
427-
await expect.element(marker()).not.toBeInTheDocument();
428-
const reply = () => screen.getByRole("textbox", { name: "Reply to this conversation" });
429428
await screen.getByRole("button", { name: "Reply", exact: true }).click();
430429
await reply().fill("Unsent");
431430

432-
// Reopened and resolved again under the reader, it stays open until they collapse it.
433-
await screen.rerender(withThread(false));
434-
await expect.element(card("Open")).toHaveAttribute("aria-expanded", "true");
435-
await screen.rerender(withThread(true));
436-
await expect.element(card("Resolved")).toHaveAttribute("aria-expanded", "true");
437-
438-
await card("Resolved").click();
439-
await expect.element(marker()).toHaveFocus();
431+
// Hidden conversations stay mounted, so the unsent reply is still there.
432+
await marker.click();
440433
await expect.element(card("Resolved")).not.toBeInTheDocument();
441-
442-
// Reopened and resolved again by someone else, the returning marker leaves focus alone.
443-
await screen.rerender(withThread(false));
444-
await expect.element(card("Open")).toBeVisible();
445-
await screen.rerender(withThread(true));
446-
await expect.element(marker()).toBeVisible();
447-
await expect.element(marker()).not.toHaveFocus();
448-
449-
await marker().click();
434+
await marker.click();
450435
await expect.element(reply()).toHaveValue("Unsent");
436+
await marker.click();
451437

452-
// Collapsed by the reader while open, it folds once it is resolved.
453-
await screen.rerender(withThread(false));
454-
await card("Open").click();
455-
await expect.element(card("Open")).toHaveAttribute("aria-expanded", "false");
456-
await screen.rerender(withThread(true));
457-
await expect.element(marker()).toBeVisible();
458-
await expect.element(card("Resolved")).not.toBeInTheDocument();
459-
460-
// Reopened by someone else, the returning card leaves focus alone too.
438+
// Seen open and then resolved, it stays until the avatars are clicked.
461439
await screen.rerender(withThread(false));
462440
await expect.element(card("Open")).toBeVisible();
463-
await expect.element(card("Open")).not.toHaveFocus();
464441
await screen.rerender(withThread(true));
465-
await expect.element(marker()).toBeVisible();
442+
await expect.element(card("Resolved")).toBeVisible();
466443

467-
// Stacked view draws a context row once, so both sides' conversations share its marker.
444+
// Stacked view draws a context row once, so both sides' conversations share the avatars.
468445
await screen.rerender(withThread(true, true));
469-
const both = screen.getByRole("button", { name: "Show 2 resolved conversations" });
470-
await expect.element(both).toBeVisible();
471-
await expect.element(marker()).not.toBeInTheDocument();
472-
473-
// The second collapse finds the marker already there and still hands it focus.
474-
await both.click();
475-
await card("Resolved").first().click();
476-
await expect.element(marker()).toHaveFocus();
477-
await card("Resolved").click();
478-
await expect.element(both).toHaveFocus();
446+
await expect
447+
.element(screen.getByRole("button", { name: "2 resolved conversations" }))
448+
.toBeVisible();
479449
});

‎apps/web/src/components/pullRequest/PullRequestCodeTab.tsx‎

Lines changed: 5 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -118,7 +118,6 @@ import {
118118
InlineReviewThreads,
119119
PendingReviewCommentCard,
120120
ReviewThreadCard,
121-
type ReviewThreadFold,
122121
toViewerSide,
123122
} from "./PullRequestReviewAnnotation";
124123
import { pullRequestCompletionScope } from "./PullRequestMarkdownTextarea";
@@ -643,21 +642,15 @@ function PullRequestCodeTab({
643642
const groups = new Map<string, MutableAnnotationGroup>();
644643
// Stacked view draws a context row once, so its old side shares the new side's annotation.
645644
const contextNewLines = new Map<number, number>();
646-
const stackedContextLine = (line: number) => {
647-
if (contextNewLines.size === 0) {
645+
const groupAt = (anchorSide: PullRequestDiffSide, anchorLine: number) => {
646+
if (anchorSide === "left" && diffLayout !== "split" && contextNewLines.size === 0) {
648647
for (const row of fileDiffRows(fileDiff)) {
649648
if (row.oldLine !== null && row.newLine !== null) {
650649
contextNewLines.set(row.oldLine, row.newLine);
651650
}
652651
}
653652
}
654-
return contextNewLines.get(line);
655-
};
656-
const groupAt = (anchorSide: PullRequestDiffSide, anchorLine: number) => {
657-
const contextLine =
658-
anchorSide === "left" && diffLayout !== "split"
659-
? stackedContextLine(anchorLine)
660-
: undefined;
653+
const contextLine = anchorSide === "left" ? contextNewLines.get(anchorLine) : undefined;
661654
const side = contextLine === undefined ? anchorSide : "right";
662655
const line = contextLine ?? anchorLine;
663656
const key = `${side}:${line}`;
@@ -1016,7 +1009,7 @@ function PullRequestCodeTab({
10161009
return true;
10171010
};
10181011

1019-
const renderThreadCard = (thread: PullRequestReviewThread, fold?: ReviewThreadFold) => (
1012+
const renderThreadCard = (thread: PullRequestReviewThread, hidden?: boolean) => (
10201013
<ReviewThreadCard
10211014
// Host thread ids can collide across pull requests.
10221015
key={`${reference.projectId}#${reference.number}:${thread.id}`}
@@ -1081,7 +1074,7 @@ function PullRequestCodeTab({
10811074
)
10821075
}
10831076
onReacted={onRefresh}
1084-
{...(fold ? { fold } : {})}
1077+
{...(hidden === undefined ? {} : { hidden })}
10851078
/>
10861079
);
10871080

‎apps/web/src/components/pullRequest/PullRequestReviewAnnotation.tsx‎

Lines changed: 51 additions & 111 deletions
Original file line numberDiff line numberDiff line change
@@ -116,121 +116,62 @@ export function PendingReviewCommentCard({
116116
);
117117
}
118118

119-
const MARKER_AVATAR_LIMIT = 3;
120-
121-
function ResolvedThreadsMarker({
119+
/** The conversations on one diff line. Resolved ones hide behind their authors' avatars. */
120+
export function InlineReviewThreads({
122121
threads,
123-
autoFocus,
124-
onOpen,
122+
renderThread,
125123
}: {
126124
threads: ReadonlyArray<PullRequestReviewThread>;
127-
autoFocus: boolean;
128-
onOpen: () => void;
125+
renderThread: (thread: PullRequestReviewThread, hidden: boolean) => ReactNode;
129126
}) {
127+
const [shown, setShown] = useState<ReadonlySet<string>>(() => new Set());
128+
// A conversation seen open stays shown once resolved, so it does not vanish under the reader.
129+
const open = threads.filter((thread) => !thread.isResolved);
130+
if (open.some((thread) => !shown.has(thread.id))) {
131+
setShown(new Set([...shown, ...open.map((thread) => thread.id)]));
132+
}
133+
const resolved = threads.filter((thread) => thread.isResolved);
134+
const expanded = resolved.every((thread) => shown.has(thread.id));
130135
const authors = new Map<string, PullRequestActor | null>();
131-
for (const thread of threads) {
132-
for (const comment of thread.comments) {
133-
authors.set(comment.author?.login ?? "ghost", comment.author);
134-
}
136+
for (const comment of resolved.flatMap((thread) => thread.comments)) {
137+
authors.set(comment.author?.login ?? "ghost", comment.author);
135138
}
136139
if (authors.size === 0) authors.set("ghost", null);
137140
const label =
138-
threads.length === 1
139-
? "Show resolved conversation"
140-
: `Show ${threads.length} resolved conversations`;
141-
return (
142-
<Tooltip>
143-
<TooltipTrigger
144-
render={
145-
<button
146-
type="button"
147-
data-resolved-threads
148-
aria-label={label}
149-
aria-expanded={false}
150-
autoFocus={autoFocus}
151-
// Sits on the commented line, which is the row above this zero-height annotation.
152-
className="absolute right-3 bottom-full flex h-[var(--diffs-line-height,20px)] cursor-pointer items-center -space-x-1 rounded-full outline-none focus-visible:ring-2 focus-visible:ring-ring focus-visible:ring-inset"
153-
contentEditable={false}
154-
onPointerDown={(event) => event.stopPropagation()}
155-
onClick={onOpen}
156-
/>
157-
}
158-
>
159-
{[...authors].slice(0, MARKER_AVATAR_LIMIT).map(([login, author]) => (
160-
<PullRequestActorAvatar key={login} actor={author} className="ring-1 ring-background" />
161-
))}
162-
</TooltipTrigger>
163-
<TooltipPopup side="left">{label}</TooltipPopup>
164-
</Tooltip>
165-
);
166-
}
167-
168-
/** A conversation on a diff line: its line owns whether it is expanded or folded to the marker. */
169-
export interface ReviewThreadFold {
170-
readonly expanded: boolean;
171-
/** Folded cards stay mounted, so an unsent reply survives the fold. */
172-
readonly folded: boolean;
173-
readonly focusHeader: boolean;
174-
readonly onToggle: () => void;
175-
}
176-
177-
/** The conversations on one diff line. Resolved ones show as avatars until opened. */
178-
export function InlineReviewThreads({
179-
threads,
180-
renderThread,
181-
}: {
182-
threads: ReadonlyArray<PullRequestReviewThread>;
183-
renderThread: (thread: PullRequestReviewThread, fold: ReviewThreadFold) => ReactNode;
184-
}) {
185-
const [choices, setChoices] = useState<ReadonlyMap<string, boolean>>(() => new Map());
186-
// Folding swaps the focused control for another, so focus is handed to its replacement.
187-
// Each collapse remounts the marker, so one already on the line takes focus too.
188-
const [focus, setFocus] = useState<
189-
{ readonly marker: number } | { readonly threadId: string } | null
190-
>(null);
191-
const markerFocus = focus !== null && "marker" in focus ? focus.marker : 0;
192-
// A conversation seen open starts expanded, so resolving it does not fold it under the reader.
193-
const seenOpen = threads.filter((thread) => !thread.isResolved && !choices.has(thread.id));
194-
if (seenOpen.length > 0) {
195-
setChoices(new Map([...choices, ...seenOpen.map((thread) => [thread.id, true] as const)]));
196-
}
197-
const isExpanded = (thread: PullRequestReviewThread) =>
198-
choices.get(thread.id) ?? !thread.isResolved;
199-
const folded = threads.filter((thread) => thread.isResolved && !isExpanded(thread));
200-
// A focus request lives only while its target is on the line, so a later remount cannot steal focus.
201-
const focusTargetGone =
202-
focus !== null &&
203-
("marker" in focus
204-
? folded.length === 0
205-
: !threads.some((thread) => thread.id === focus.threadId && !folded.includes(thread)));
206-
if (focusTargetGone) setFocus(null);
141+
resolved.length === 1 ? "Resolved conversation" : `${resolved.length} resolved conversations`;
207142
return (
208143
<>
209-
{folded.length > 0 ? (
210-
<ResolvedThreadsMarker
211-
key={markerFocus}
212-
threads={folded}
213-
autoFocus={markerFocus > 0}
214-
onOpen={() => {
215-
setChoices(
216-
new Map([...choices, ...folded.map((thread) => [thread.id, true] as const)]),
217-
);
218-
setFocus(folded[0] ? { threadId: folded[0].id } : null);
219-
}}
220-
/>
144+
{resolved.length > 0 ? (
145+
<Tooltip>
146+
<TooltipTrigger
147+
render={
148+
<button
149+
type="button"
150+
data-resolved-threads
151+
aria-label={label}
152+
aria-expanded={expanded}
153+
// Sits on the commented line, which is the row above this annotation.
154+
className="absolute right-3 bottom-full flex h-[var(--diffs-line-height,20px)] cursor-pointer items-center -space-x-1 rounded-full outline-none focus-visible:ring-2 focus-visible:ring-ring focus-visible:ring-inset"
155+
contentEditable={false}
156+
onPointerDown={(event) => event.stopPropagation()}
157+
onClick={() =>
158+
setShown(new Set((expanded ? open : threads).map((thread) => thread.id)))
159+
}
160+
/>
161+
}
162+
>
163+
{[...authors].slice(0, 3).map(([login, author]) => (
164+
<PullRequestActorAvatar
165+
key={login}
166+
actor={author}
167+
className="ring-1 ring-background"
168+
/>
169+
))}
170+
</TooltipTrigger>
171+
<TooltipPopup side="left">{label}</TooltipPopup>
172+
</Tooltip>
221173
) : null}
222-
{threads.map((thread) =>
223-
renderThread(thread, {
224-
expanded: isExpanded(thread),
225-
folded: folded.includes(thread),
226-
focusHeader: focus !== null && "threadId" in focus && focus.threadId === thread.id,
227-
onToggle: () => {
228-
const expanded = !isExpanded(thread);
229-
setChoices(new Map(choices).set(thread.id, expanded));
230-
if (!expanded && thread.isResolved) setFocus({ marker: markerFocus + 1 });
231-
},
232-
}),
233-
)}
174+
{threads.map((thread) => renderThread(thread, thread.isResolved && !shown.has(thread.id)))}
234175
</>
235176
);
236177
}
@@ -255,7 +196,7 @@ export function ReviewThreadCard({
255196
onEditComment,
256197
onToggleResolved,
257198
onReacted,
258-
fold,
199+
hidden,
259200
}: {
260201
thread: PullRequestReviewThread;
261202
workspaceRoot: string;
@@ -280,10 +221,10 @@ export function ReviewThreadCard({
280221
onEditComment: (commentId: string, body: string) => Promise<boolean>;
281222
onToggleResolved: () => void;
282223
onReacted: () => void;
283-
fold?: ReviewThreadFold;
224+
/** Set on a diff line. Hidden draws nothing but stays mounted, so an unsent reply survives. */
225+
hidden?: boolean;
284226
}) {
285-
const [ownExpanded, setOwnExpanded] = useState(!thread.isResolved);
286-
const expanded = fold?.expanded ?? ownExpanded;
227+
const [expanded, setExpanded] = useState(hidden !== undefined || !thread.isResolved);
287228
const [replying, setReplying] = useState(false);
288229
const [reply, setReply] = useState("");
289230
const [editingId, setEditingId] = useState<string | null>(null);
@@ -356,7 +297,7 @@ export function ReviewThreadCard({
356297
}));
357298
};
358299

359-
if (fold?.folded) return null;
300+
if (hidden) return null;
360301

361302
return (
362303
<div
@@ -374,8 +315,7 @@ export function ReviewThreadCard({
374315
type="button"
375316
className="hover:text-foreground"
376317
aria-expanded={expanded}
377-
autoFocus={fold?.focusHeader === true}
378-
onClick={() => (fold ? fold.onToggle() : setOwnExpanded((current) => !current))}
318+
onClick={() => setExpanded((current) => !current)}
379319
>
380320
{thread.isResolved ? "Resolved" : "Open"} · {commentCount}{" "}
381321
{commentCount === 1 ? "comment" : "comments"}

0 commit comments

Comments
 (0)