Skip to content

Commit b433a52

Browse files
committed
fix(web): a stacked context row keeps one annotation, and each collapse refocuses the marker
1 parent 1c2d4e1 commit b433a52

3 files changed

Lines changed: 39 additions & 27 deletions

File tree

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

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -452,8 +452,14 @@ it("folds a resolved conversation into avatars on its line until it is opened",
452452

453453
// Stacked view draws a context row once, so both sides' conversations share its marker.
454454
await screen.rerender(withThread(true, true));
455-
await expect
456-
.element(screen.getByRole("button", { name: "Show 2 resolved conversations" }))
457-
.toBeVisible();
455+
const both = screen.getByRole("button", { name: "Show 2 resolved conversations" });
456+
await expect.element(both).toBeVisible();
458457
await expect.element(marker()).not.toBeInTheDocument();
458+
459+
// The second collapse finds the marker already there and still hands it focus.
460+
await both.click();
461+
await card("Resolved").first().click();
462+
await expect.element(marker()).toHaveFocus();
463+
await card("Resolved").click();
464+
await expect.element(both).toHaveFocus();
459465
});

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

Lines changed: 21 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -641,7 +641,25 @@ function PullRequestCodeTab({
641641
const fileKey = buildFileDiffRenderKey(fileDiff);
642642
const path = resolveFileDiffPath(fileDiff);
643643
const groups = new Map<string, MutableAnnotationGroup>();
644-
const groupAt = (side: PullRequestDiffSide, line: number) => {
644+
// Stacked view draws a context row once, so its old side shares the new side's annotation.
645+
const contextNewLines = new Map<number, number>();
646+
const stackedContextLine = (line: number) => {
647+
if (contextNewLines.size === 0) {
648+
for (const row of fileDiffRows(fileDiff)) {
649+
if (row.oldLine !== null && row.newLine !== null) {
650+
contextNewLines.set(row.oldLine, row.newLine);
651+
}
652+
}
653+
}
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;
661+
const side = contextLine === undefined ? anchorSide : "right";
662+
const line = contextLine ?? anchorLine;
645663
const key = `${side}:${line}`;
646664
const existing = groups.get(key);
647665
if (existing) return existing;
@@ -656,26 +674,9 @@ function PullRequestCodeTab({
656674
return created;
657675
};
658676

659-
const fileThreads = threadsByPath.get(path) ?? [];
660-
// Stacked view draws a context row once, so its old side's conversations join the new side's.
661-
const contextNewLines =
662-
diffLayout !== "split" && fileThreads.some((thread) => thread.side === "left")
663-
? new Map(
664-
fileDiffRows(fileDiff).flatMap((row) =>
665-
row.oldLine !== null && row.newLine !== null
666-
? [[row.oldLine, row.newLine] as const]
667-
: [],
668-
),
669-
)
670-
: null;
671-
for (const thread of fileThreads) {
677+
for (const thread of threadsByPath.get(path) ?? []) {
672678
if (thread.line === null || !placedThreadIds.has(thread.id)) continue;
673-
const contextLine = thread.side === "left" ? contextNewLines?.get(thread.line) : undefined;
674-
const group =
675-
contextLine === undefined
676-
? groupAt(thread.side, thread.line)
677-
: groupAt("right", contextLine);
678-
group.threads.push(thread);
679+
groupAt(thread.side, thread.line).threads.push(thread);
679680
}
680681
if (commit === null) {
681682
for (const comment of pendingComments) {

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

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -184,7 +184,11 @@ export function InlineReviewThreads({
184184
}) {
185185
const [choices, setChoices] = useState<ReadonlyMap<string, boolean>>(() => new Map());
186186
// Folding swaps the focused control for another, so focus is handed to its replacement.
187-
const [focus, setFocus] = useState<"marker" | { readonly threadId: string } | null>(null);
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;
188192
// A conversation seen open starts expanded, so resolving it does not fold it under the reader.
189193
const seenOpen = threads.filter((thread) => !thread.isResolved && !choices.has(thread.id));
190194
if (seenOpen.length > 0) {
@@ -197,8 +201,9 @@ export function InlineReviewThreads({
197201
<>
198202
{folded.length > 0 ? (
199203
<ResolvedThreadsMarker
204+
key={markerFocus}
200205
threads={folded}
201-
autoFocus={focus === "marker"}
206+
autoFocus={markerFocus > 0}
202207
onOpen={() => {
203208
setChoices(
204209
new Map([...choices, ...folded.map((thread) => [thread.id, true] as const)]),
@@ -211,11 +216,11 @@ export function InlineReviewThreads({
211216
renderThread(thread, {
212217
expanded: isExpanded(thread),
213218
folded: folded.includes(thread),
214-
focusHeader: focus !== null && focus !== "marker" && focus.threadId === thread.id,
219+
focusHeader: focus !== null && "threadId" in focus && focus.threadId === thread.id,
215220
onToggle: () => {
216221
const expanded = !isExpanded(thread);
217222
setChoices(new Map(choices).set(thread.id, expanded));
218-
if (!expanded && thread.isResolved) setFocus("marker");
223+
if (!expanded && thread.isResolved) setFocus({ marker: markerFocus + 1 });
219224
},
220225
}),
221226
)}

0 commit comments

Comments
 (0)