Skip to content

Commit 9de3953

Browse files
fix(web): settle a comment-link reveal only for its own request
A host read started for one comment link could clear a newer link once it settled, non-comment fragments forced a host refetch, a pending link survived reload and reopen, and a failed activity refetch left it pending for good. - settlePullRequestReveal takes the link it settles and is a no-op once the surface holds another; the panel drops its pending read on unmount. - Only a fragment shaped like a loaded comment's anchor (`#issuecomment-`, `#discussion_r`, `#note_`) asks the host again; others settle at once. - revealUrl is stripped from persisted state and from closed-tab snapshots. - A failed activity refetch after the host read settles the link. Co-Authored-By: Claude Opus 5.5 <[email protected]>
1 parent e4601a1 commit 9de3953

7 files changed

Lines changed: 116 additions & 31 deletions

File tree

‎apps/web/src/components/chat/RightPanelSurfaceContent.tsx‎

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -154,8 +154,8 @@ export function RightPanelSurfaceContent(props: {
154154
useRightPanelStore.getState().setPullRequestDetailTab(threadRef, surface.id, tab);
155155
}}
156156
revealUrl={surface.revealUrl}
157-
onRevealSettled={() => {
158-
useRightPanelStore.getState().settlePullRequestReveal(threadRef, surface.id);
157+
onRevealSettled={(revealUrl) => {
158+
useRightPanelStore.getState().settlePullRequestReveal(threadRef, surface.id, revealUrl);
159159
}}
160160
context={pullRequestPanelContext(
161161
{

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

Lines changed: 33 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -126,6 +126,7 @@ import {
126126
isStackedPullRequestBase,
127127
pullRequestActionNeedsHostRefresh,
128128
pullRequestCheckoutCommand,
129+
pullRequestCommentLinkMayLoad,
129130
pullRequestCommentLinkTarget,
130131
pullRequestConversationItems,
131132
pullRequestFindingKey,
@@ -305,7 +306,8 @@ export function PullRequestDetailPanel({
305306
onTabChange?: ((tab: PullRequestDetailTab) => void) | undefined;
306307
/** A link that may name one of this pull request's comments, waiting to be shown. */
307308
revealUrl?: string | undefined;
308-
onRevealSettled?: (() => void) | undefined;
309+
/** Reports the link it was given shown or missing, so a newer link is left pending. */
310+
onRevealSettled?: ((revealUrl: string) => void) | undefined;
309311
}) {
310312
const environmentConfigs = useServerConfigs();
311313
const projects = useProjects();
@@ -363,13 +365,14 @@ export function PullRequestDetailPanel({
363365
detail,
364366
} = usePullRequestDetail(environmentId, reference, matchingListEntry);
365367
const [selectedTab, setTab] = useState<PullRequestDetailTab>(initialTab);
366-
const revealCommentId =
368+
const revealItems =
367369
revealUrl === undefined || detail === null
368370
? null
369-
: (pullRequestCommentLinkTarget(
370-
pullRequestConversationItems(detail.comments, detail.reviewThreads),
371-
revealUrl,
372-
)?.commentId ?? null);
371+
: pullRequestConversationItems(detail.comments, detail.reviewThreads);
372+
const revealCommentId =
373+
revealItems === null || revealUrl === undefined
374+
? null
375+
: (pullRequestCommentLinkTarget(revealItems, revealUrl)?.commentId ?? null);
373376
// A comment link shows the conversation holding it.
374377
if (revealCommentId !== null && !tabOnPage && selectedTab !== "summary") setTab("summary");
375378
const reviewGuidesEnabled = useEnvironmentSettings(
@@ -585,6 +588,7 @@ export function PullRequestDetailPanel({
585588
!activityQuery.isPending
586589
? activityQuery.dataUpdatedAt
587590
: null;
591+
const revealReadFailed = revealMissedAt !== null && activityQuery.error !== null;
588592
const refreshDetail = () => {
589593
detailQuery.refresh();
590594
activityQuery.refresh();
@@ -651,27 +655,37 @@ export function PullRequestDetailPanel({
651655
const revealHostRead = useRef<{ readonly url: string; readonly after: number | null } | null>(
652656
null,
653657
);
654-
const settleRevealMiss = () => {
658+
// oxlint-disable-next-line t3code/no-unreasoned-state-sync -- a host read outliving the panel must not settle a later link
659+
useEffect(
660+
() => () => {
661+
revealHostRead.current = null;
662+
},
663+
[],
664+
);
665+
const settleReveal = (url: string) => {
655666
revealHostRead.current = null;
656-
onRevealSettled?.();
667+
onRevealSettled?.(url);
657668
};
658669
const showRevealedComment = useEffectEvent(() => {
659670
if (revealCommentId === null) {
660-
if (revealUrl === undefined) return;
671+
if (revealUrl === undefined || revealItems === null) return;
661672
const read = revealHostRead.current;
662673
if (read?.url !== revealUrl) {
674+
if (!pullRequestCommentLinkMayLoad(revealItems, revealUrl)) return settleReveal(revealUrl);
663675
const request = { url: revealUrl, after: null };
664676
revealHostRead.current = request;
665677
void refetchFromHost().then((invalidated) => {
666678
if (revealHostRead.current !== request) return;
667679
// Without an invalidation the refetch could only repeat the cached read.
668-
if (!invalidated) return settleRevealMiss();
680+
if (!invalidated) return settleReveal(request.url);
669681
revealHostRead.current = { url: request.url, after: Date.now() };
670682
});
671683
return;
672684
}
673685
// A read that finished before the invalidation cannot show the comment is missing.
674-
if (read.after !== null && (revealMissedAt ?? 0) > read.after) settleRevealMiss();
686+
if (read.after !== null && (revealReadFailed || (revealMissedAt ?? 0) > read.after)) {
687+
settleReveal(revealUrl);
688+
}
675689
return;
676690
}
677691
// A later click on the same link gets its own host read.
@@ -682,8 +696,11 @@ export function PullRequestDetailPanel({
682696
});
683697
// oxlint-disable-next-line t3code/no-unreasoned-state-sync -- the tab and the pending link live in stores outside the panel
684698
useEffect(() => {
685-
if (revealCommentId !== null || revealMissedAt !== null) showRevealedComment();
686-
}, [revealCommentId, revealMissedAt]);
699+
if (revealCommentId !== null || revealMissedAt !== null || revealReadFailed) {
700+
// oxlint-disable-next-line react/set-state-in-effect -- a missed comment starts a host read, which marks itself refreshing before it awaits
701+
showRevealedComment();
702+
}
703+
}, [revealCommentId, revealMissedAt, revealReadFailed]);
687704
const runAction = useAtomCommand(pullRequestEnvironment.runAction, {
688705
reportFailure: false,
689706
});
@@ -1819,7 +1836,9 @@ export function PullRequestDetailPanel({
18191836
detail={detail}
18201837
activityPending={activityPending}
18211838
revealCommentId={tab === "summary" ? revealCommentId : null}
1822-
{...(onRevealSettled ? { onCommentRevealed: onRevealSettled } : {})}
1839+
{...(onRevealSettled && revealUrl !== undefined
1840+
? { onCommentRevealed: () => onRevealSettled(revealUrl) }
1841+
: {})}
18231842
checksStale={checksStale}
18241843
activityError={activityError}
18251844
pendingFinding={handoff}

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

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1552,11 +1552,11 @@ export function PullRequestsPage() {
15521552
refreshToken={detailRefreshToken}
15531553
tabOnPage
15541554
revealUrl={renderedPullRequestSurface.revealUrl}
1555-
onRevealSettled={() => {
1555+
onRevealSettled={(revealUrl) => {
15561556
if (rightPanelRef === null) return;
15571557
useRightPanelStore
15581558
.getState()
1559-
.settlePullRequestReveal(rightPanelRef, renderedPullRequestSurface.id);
1559+
.settlePullRequestReveal(rightPanelRef, renderedPullRequestSurface.id, revealUrl);
15601560
}}
15611561
onActed={(action, phase = "done", reply) => {
15621562
// From every row held, not the ones on screen: a pull request closed from an open

‎apps/web/src/components/pullRequest/pullRequestDetail.logic.test.ts‎

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@ import {
2828
groupPullRequestTimelineConversations,
2929
filterPullRequestConversationItems,
3030
pagePullRequestConversationItems,
31+
pullRequestCommentLinkMayLoad,
3132
pullRequestCommentLinkTarget,
3233
pullRequestConversationItemComments,
3334
pullRequestConversationItems,
@@ -582,6 +583,16 @@ describe("review thread conversations", () => {
582583
expect(
583584
pullRequestCommentLinkTarget(githubItems, "https://github.com/o/r/pull/1/files#r42"),
584585
).toEqual({ item: githubItems[0], commentId: "reply" });
586+
const issueComment = { ...reply, url: "https://github.com/o/r/pull/1#issuecomment-1" };
587+
const issueItems = pullRequestConversationItems([issueComment], []);
588+
const pull = "https://github.com/o/r/pull/1";
589+
expect(pullRequestCommentLinkMayLoad(issueItems, `${pull}#issuecomment-999`)).toBe(true);
590+
expect(pullRequestCommentLinkMayLoad(githubItems, `${pull}/files#r999`)).toBe(true);
591+
for (const fragment of ["#diff-abc", "#issue-1", "#event-1", "#partial-pull-merging", ""]) {
592+
expect(pullRequestCommentLinkMayLoad(issueItems, `${pull}/files${fragment}`), fragment).toBe(
593+
false,
594+
);
595+
}
585596
for (const url of ["https://example.test/pull/1", "https://example.test/pull/1#gone", "#"]) {
586597
expect(pullRequestCommentLinkTarget(items, url), url).toBeNull();
587598
}

‎apps/web/src/components/pullRequest/pullRequestDetail.logic.ts‎

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -651,6 +651,31 @@ export function pullRequestCommentLinkTarget(
651651
return null;
652652
}
653653

654+
/** `#issuecomment-1` -> `#issuecomment-`; linear, unlike a backtracking `/\d+$/`. */
655+
function anchorKind(anchor: string) {
656+
let end = anchor.length;
657+
while (end > 0 && anchor.charCodeAt(end - 1) >= 48 && anchor.charCodeAt(end - 1) <= 57) end--;
658+
return anchor.slice(0, end);
659+
}
660+
661+
/**
662+
* Whether a link no loaded comment matches could name a newer one: its fragment is shaped like a
663+
* loaded comment's, so `#diff-…` or `#event-…` never asks the host again.
664+
*/
665+
export function pullRequestCommentLinkMayLoad(
666+
items: ReadonlyArray<PullRequestConversationItem>,
667+
url: string,
668+
): boolean {
669+
const anchor = commentAnchor(url);
670+
if (!anchor) return false;
671+
const kind = anchorKind(anchor);
672+
return items.some((item) =>
673+
pullRequestConversationItemComments(item).some(
674+
(comment) => comment.url !== null && anchorKind(commentAnchor(comment.url) ?? "") === kind,
675+
),
676+
);
677+
}
678+
654679
export function filterPullRequestConversationItems(
655680
items: ReadonlyArray<PullRequestConversationItem>,
656681
filter: { readonly bots: boolean; readonly resolved: boolean },

‎apps/web/src/rightPanelStore.test.ts‎

Lines changed: 19 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1012,7 +1012,7 @@ describe("rightPanelStore", () => {
10121012
expect(state.surfaces[0]).not.toHaveProperty("revealUrl");
10131013
});
10141014

1015-
it("holds a comment link until the panel settles it, and a later link replaces it", () => {
1015+
it("holds a comment link until the panel settles it, and a later link replaces it", async () => {
10161016
const pr = { projectId: "project-a", repository: "pingdotgg/t3code", number: 4909 };
10171017
const id = pullRequestSurfaceId(pr);
10181018
const url = "https://github.com/pingdotgg/t3code/pull/4909";
@@ -1025,12 +1025,29 @@ describe("rightPanelStore", () => {
10251025
store.openPullRequest(refA, { ...pr, url });
10261026
expect(surface()).not.toHaveProperty("revealUrl");
10271027
store.openPullRequest(refA, { ...pr, url: comment(2), revealUrl: comment(2) });
1028-
store.settlePullRequestReveal(refA, id);
1028+
// A host read started for an earlier link settles only that link.
1029+
store.settlePullRequestReveal(refA, id, comment(1));
1030+
expect(surface()).toMatchObject({ revealUrl: comment(2) });
1031+
store.settlePullRequestReveal(refA, id, comment(2));
10291032
expect(surface()).toMatchObject({ url: comment(2) });
10301033
expect(surface()).not.toHaveProperty("revealUrl");
10311034
// Reopening a closed tab hands back its surface, whose url still names the comment.
10321035
store.openPullRequest(refA, { ...pr, url: comment(2) });
10331036
expect(surface()).not.toHaveProperty("revealUrl");
1037+
store.openPullRequest(refA, { ...pr, url: comment(3), revealUrl: comment(3) });
1038+
await reloadRightPanelStore();
1039+
expect(surface()).toMatchObject({ url: comment(3) });
1040+
expect(surface()).not.toHaveProperty("revealUrl");
1041+
store.openPullRequest(refA, { ...pr, url: comment(4), revealUrl: comment(4) });
1042+
store.closeSurface(refA, id);
1043+
await reopenClosedView(useClosedViewStore.getState().entries[0]!, {
1044+
workspaceAvailable: true,
1045+
openPreview: () => {
1046+
throw new Error("A PR tab must not create a browser session");
1047+
},
1048+
});
1049+
expect(surface()).toMatchObject({ url: comment(4) });
1050+
expect(surface()).not.toHaveProperty("revealUrl");
10341051
});
10351052

10361053
it("restores each environment, thread and PR surface's detail tab after reload and URL reopen", async () => {

‎apps/web/src/rightPanelStore.ts‎

Lines changed: 24 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -160,7 +160,8 @@ interface RightPanelStoreState {
160160
surfaceId: string,
161161
detailTab: PullRequestDetailTab,
162162
) => void;
163-
settlePullRequestReveal: (ref: ScopedThreadRef, surfaceId: string) => void;
163+
/** Clears the surface's pending link only while it is still `revealUrl`. */
164+
settlePullRequestReveal: (ref: ScopedThreadRef, surfaceId: string, revealUrl: string) => void;
164165
openTerminal: (ref: ScopedThreadRef, terminalId: string) => void;
165166
splitTerminal: (
166167
ref: ScopedThreadRef,
@@ -298,6 +299,13 @@ export function pullRequestSurface(target: {
298299
};
299300
}
300301

302+
/** A pending comment link belongs to the click that made it, not to a reload or a reopen. */
303+
const withoutReveal = (surface: RightPanelSurface): RightPanelSurface => {
304+
if (surface.kind !== "pull-request" || surface.revealUrl === undefined) return surface;
305+
const { revealUrl: _pending, ...rest } = surface;
306+
return rest;
307+
};
308+
301309
const upsertSurface = (
302310
current: ThreadRightPanelState,
303311
surface: RightPanelSurface,
@@ -787,20 +795,18 @@ export const useRightPanelStore = create<RightPanelStoreState>()(
787795
),
788796
}));
789797
}),
790-
settlePullRequestReveal: (ref, surfaceId) =>
798+
settlePullRequestReveal: (ref, surfaceId, revealUrl) =>
791799
set((state) => {
792800
const threadKey = scopedThreadKey(ref);
793801
const existing = state.byThreadKey[threadKey]?.surfaces.find(
794802
(surface) => surface.id === surfaceId,
795803
);
796-
if (existing?.kind !== "pull-request" || existing.revealUrl === undefined) return state;
804+
if (existing?.kind !== "pull-request" || existing.revealUrl !== revealUrl) return state;
797805
return automaticUpdate(state, threadKey, (current) => ({
798806
...current,
799-
surfaces: current.surfaces.map((surface) => {
800-
if (surface.id !== surfaceId || surface.kind !== "pull-request") return surface;
801-
const { revealUrl: _settled, ...rest } = surface;
802-
return rest;
803-
}),
807+
surfaces: current.surfaces.map((surface) =>
808+
surface.id === surfaceId ? withoutReveal(surface) : surface,
809+
),
804810
}));
805811
}),
806812
openFile: (ref, requestedPath, line) =>
@@ -1186,8 +1192,15 @@ export const useRightPanelStore = create<RightPanelStoreState>()(
11861192
),
11871193
partialize: (state) => ({
11881194
byThreadKey: Object.fromEntries(
1189-
Object.entries(state.byThreadKey).filter(
1190-
([threadKey]) => !isPullRequestsPanelKey(threadKey),
1195+
Object.entries(state.byThreadKey).flatMap(([threadKey, threadState]) =>
1196+
isPullRequestsPanelKey(threadKey)
1197+
? []
1198+
: [
1199+
[
1200+
threadKey,
1201+
{ ...threadState, surfaces: threadState.surfaces.map(withoutReveal) },
1202+
],
1203+
],
11911204
),
11921205
),
11931206
threadPanelVisibilityByThreadKey: Object.fromEntries(
@@ -1217,7 +1230,7 @@ useRightPanelStore.subscribe((next, previous) => {
12171230
...removed.filter((surface) => surface.id !== before.activeSurfaceId),
12181231
...removed.filter((surface) => surface.id === before.activeSurfaceId),
12191232
];
1220-
for (const surface of closedInOrder) {
1233+
for (const surface of closedInOrder.map(withoutReveal)) {
12211234
if (
12221235
surface.kind === "terminal" ||
12231236
(surface.kind === "preview" && surface.resourceId !== null)

0 commit comments

Comments
 (0)