Skip to content

Commit 9810d85

Browse files
wesbillmanCarl
andauthored
fix(desktop): preserve thread anchor through layout reflow (#3212)
## Summary - keep a thread presentation-switch anchor pinned while focus/split width reflow settles - retire the temporary anchor only after resize correction and a following paint confirm the row is visible - preserve the existing external-target resolution behavior and viewport E2E contract ## Root cause The focus and split wrappers intentionally retain the same thread surface, but switching wrappers also changes the message column width. `useAnchoredScroll` centered the captured message once and immediately cleared the one-shot layout target. A later text reflow could then move that message outside the viewport with no remaining target to correct it. ## Verification - `pnpm check` - `pnpm typecheck` - `pnpm test` — 3,699 passed - `pnpm build:e2e` - `pnpm exec playwright test tests/e2e/thread-focus-mode.spec.ts --project=smoke --repeat-each=10` — 20 passed - push hook: branch-skew, Desktop check, and Desktop full unit suite passed --------- Signed-off-by: Wes <[email protected]> Co-authored-by: Carl <c7ebe626f000404285d3686e1dc74cc07cc60a9754a150041ba132e14bd3e2ec@buzz.block.builderlab.xyz>
1 parent cb2a265 commit 9810d85

6 files changed

Lines changed: 258 additions & 23 deletions

File tree

‎desktop/src/features/channels/ui/ChannelPane.tsx‎

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -514,8 +514,6 @@ export const ChannelPane = React.memo(function ChannelPane({
514514
const isOverlay = useIsThreadPanelOverlay();
515515
const useSplitAuxiliaryPane = !isSinglePanelView && !isOverlay;
516516
const threadViewMode = useThreadViewMode();
517-
// Focus mode only replaces the wide split thread pane; narrow threads and
518-
// other auxiliary panels keep their existing presentation.
519517
const useFocusThreadDrawer =
520518
threadViewMode === "focus" &&
521519
useSplitAuxiliaryPane &&
@@ -526,6 +524,7 @@ export const ChannelPane = React.memo(function ChannelPane({
526524
);
527525
const { changeThreadViewMode, layoutScrollTargetId, resolveScrollTarget } =
528526
useThreadViewModeSwitch({
527+
activeThreadHeadId: threadHeadMessage?.id ?? null,
529528
externalScrollTargetId: threadScrollTargetId,
530529
onExternalTargetResolved: onThreadScrollTargetResolved,
531530
onModeChange: markExitComplete,
@@ -881,7 +880,8 @@ export const ChannelPane = React.memo(function ChannelPane({
881880
onExpandReplies={onExpandThreadReplies}
882881
onSelectReplyTarget={onSelectThreadReplyTarget}
883882
onSend={onSendThreadReply}
884-
onScrollTargetResolved={resolveScrollTarget}
883+
onScrollTargetResolved={() => resolveScrollTarget()}
884+
onScrollTargetSettled={resolveScrollTarget}
885885
onToggleReaction={onToggleReaction}
886886
onUnfollowThread={onUnfollowThread}
887887
profiles={profiles}

‎desktop/src/features/channels/ui/useThreadViewModeSwitch.test.mjs‎

Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ import test from "node:test";
44
import {
55
findTopVisibleThreadMessageId,
66
getResolvedThreadTargets,
7+
getScopedLayoutScrollTargetId,
78
} from "./useThreadViewModeSwitch.ts";
89

910
function row(id, top, bottom) {
@@ -44,6 +45,55 @@ test("resolves both sources when a layout anchor matches the external target", (
4445
);
4546
});
4647

48+
test("does not resolve a layout target that was never captured", () => {
49+
assert.deepEqual(
50+
getResolvedThreadTargets({
51+
externalTargetId: "reply-b",
52+
layoutTargetId: null,
53+
}),
54+
{ resolveExternal: true, resolveLayout: false },
55+
);
56+
assert.deepEqual(
57+
getResolvedThreadTargets({
58+
externalTargetId: null,
59+
layoutTargetId: null,
60+
}),
61+
{ resolveExternal: true, resolveLayout: false },
62+
);
63+
});
64+
65+
test("drops a captured layout target when the active thread closes or changes", () => {
66+
const captured = { messageId: "reply-a", threadHeadId: "thread-a" };
67+
68+
assert.equal(
69+
getScopedLayoutScrollTargetId({
70+
activeThreadHeadId: "thread-a",
71+
layoutTarget: captured,
72+
}),
73+
"reply-a",
74+
);
75+
assert.equal(
76+
getScopedLayoutScrollTargetId({
77+
activeThreadHeadId: null,
78+
layoutTarget: captured,
79+
}),
80+
null,
81+
);
82+
const replacementLayoutTargetId = getScopedLayoutScrollTargetId({
83+
activeThreadHeadId: "thread-b",
84+
layoutTarget: captured,
85+
});
86+
assert.equal(replacementLayoutTargetId, null);
87+
assert.deepEqual(
88+
getResolvedThreadTargets({
89+
externalTargetId: "reply-b",
90+
layoutTargetId: replacementLayoutTargetId,
91+
}),
92+
{ resolveExternal: true, resolveLayout: false },
93+
"the stale anchor does not mask the replacement thread target",
94+
);
95+
});
96+
4797
test("returns null without a mounted thread body or visible message", () => {
4898
assert.equal(findTopVisibleThreadMessageId(null), null);
4999
assert.equal(

‎desktop/src/features/channels/ui/useThreadViewModeSwitch.ts‎

Lines changed: 60 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -31,21 +31,49 @@ export function getResolvedThreadTargets({
3131
};
3232
}
3333

34+
type LayoutScrollTarget = {
35+
messageId: string;
36+
threadHeadId: string;
37+
};
38+
39+
export function getScopedLayoutScrollTargetId({
40+
activeThreadHeadId,
41+
layoutTarget,
42+
}: {
43+
activeThreadHeadId: string | null;
44+
layoutTarget: LayoutScrollTarget | null;
45+
}): string | null {
46+
return layoutTarget?.threadHeadId === activeThreadHeadId
47+
? layoutTarget.messageId
48+
: null;
49+
}
50+
3451
type ThreadViewModeSwitchOptions = {
52+
activeThreadHeadId: string | null;
3553
externalScrollTargetId: string | null;
3654
onExternalTargetResolved: () => void;
3755
onModeChange?: (mode: ThreadViewMode) => void;
3856
};
3957

4058
/** Preserves the reply being read while the thread changes presentation. */
4159
export function useThreadViewModeSwitch({
60+
activeThreadHeadId,
4261
externalScrollTargetId,
4362
onExternalTargetResolved,
4463
onModeChange,
4564
}: ThreadViewModeSwitchOptions) {
46-
const [layoutScrollTargetId, setLayoutScrollTargetId] = React.useState<
47-
string | null
48-
>(null);
65+
const [layoutScrollTarget, setLayoutScrollTarget] =
66+
React.useState<LayoutScrollTarget | null>(null);
67+
const layoutScrollTargetId = getScopedLayoutScrollTargetId({
68+
activeThreadHeadId,
69+
layoutTarget: layoutScrollTarget,
70+
});
71+
72+
React.useEffect(() => {
73+
setLayoutScrollTarget((current) =>
74+
current?.threadHeadId === activeThreadHeadId ? current : null,
75+
);
76+
}, [activeThreadHeadId]);
4977

5078
const changeThreadViewMode = React.useCallback(
5179
(mode: ThreadViewMode, restoreFocus: boolean) => {
@@ -54,7 +82,11 @@ export function useThreadViewModeSwitch({
5482
);
5583
const anchorId = findTopVisibleThreadMessageId(body);
5684

57-
setLayoutScrollTargetId(anchorId);
85+
setLayoutScrollTarget(
86+
anchorId && activeThreadHeadId
87+
? { messageId: anchorId, threadHeadId: activeThreadHeadId }
88+
: null,
89+
);
5890
onModeChange?.(mode);
5991
setThreadViewMode(mode);
6092
requestAnimationFrame(() => {
@@ -69,17 +101,32 @@ export function useThreadViewModeSwitch({
69101
});
70102
});
71103
},
72-
[onModeChange],
104+
[activeThreadHeadId, onModeChange],
73105
);
74106

75-
const resolveScrollTarget = React.useCallback(() => {
76-
const resolution = getResolvedThreadTargets({
77-
externalTargetId: externalScrollTargetId,
78-
layoutTargetId: layoutScrollTargetId,
79-
});
80-
if (resolution.resolveLayout) setLayoutScrollTargetId(null);
81-
if (resolution.resolveExternal) onExternalTargetResolved();
82-
}, [externalScrollTargetId, layoutScrollTargetId, onExternalTargetResolved]);
107+
const resolveScrollTarget = React.useCallback(
108+
(settledMessageId?: string) => {
109+
const resolution = getResolvedThreadTargets({
110+
externalTargetId: externalScrollTargetId,
111+
layoutTargetId: layoutScrollTargetId,
112+
});
113+
if (resolution.resolveExternal) onExternalTargetResolved();
114+
if (settledMessageId) {
115+
setLayoutScrollTarget((current) =>
116+
current?.threadHeadId === activeThreadHeadId &&
117+
current.messageId === settledMessageId
118+
? null
119+
: current,
120+
);
121+
}
122+
},
123+
[
124+
activeThreadHeadId,
125+
externalScrollTargetId,
126+
layoutScrollTargetId,
127+
onExternalTargetResolved,
128+
],
129+
);
83130

84131
return {
85132
changeThreadViewMode,

‎desktop/src/features/messages/ui/MessageThreadPanel.tsx‎

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -79,6 +79,7 @@ type MessageThreadPanelProps = ThreadPanelLayoutProps & {
7979
onMarkRead?: (message: TimelineMessage) => void;
8080
onExpandReplies: (message: TimelineMessage) => void;
8181
onScrollTargetResolved: () => void;
82+
onScrollTargetSettled?: (messageId: string) => void;
8283
scrollTargetHighlights?: boolean;
8384
onSelectReplyTarget: (message: TimelineMessage) => void;
8485
onSend: (
@@ -207,6 +208,7 @@ export function MessageThreadPanel({
207208
onMarkRead,
208209
onExpandReplies,
209210
onScrollTargetResolved,
211+
onScrollTargetSettled,
210212
onSelectReplyTarget,
211213
onSend,
212214
onToggleReaction,
@@ -488,6 +490,8 @@ export function MessageThreadPanel({
488490
messages: threadMessages,
489491
highlightTargetMessage: scrollTargetHighlights,
490492
onTargetReached: onScrollTargetResolved,
493+
onTargetSettled: onScrollTargetSettled,
494+
pinTargetCentered: !scrollTargetHighlights,
491495
scrollContainerRef: threadBodyRef,
492496
targetMessageId: scrollTargetId,
493497
});

‎desktop/src/features/messages/ui/useAnchoredScroll.lifecycle.test.mjs‎

Lines changed: 65 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -173,7 +173,7 @@ function makePinnedCenterNodes() {
173173
listener(event);
174174
},
175175
getBoundingClientRect() {
176-
return { top: 0 };
176+
return { bottom: this.clientHeight, top: 0 };
177177
},
178178
querySelector() {
179179
return row;
@@ -232,12 +232,13 @@ function makePinnedCenterNodes() {
232232
};
233233
}
234234

235-
function Harness({ channelId, refs }) {
235+
function Harness({ channelId, onTargetSettled, refs }) {
236236
useAnchoredScroll({
237237
channelId,
238238
contentRef: refs.content,
239239
isLoading: false,
240240
messages: [{ id: "selected" }],
241+
onTargetSettled,
241242
pinTargetCentered: true,
242243
scrollContainerRef: refs.container,
243244
targetMessageId: "selected",
@@ -312,6 +313,68 @@ test("channel change attaches pinned-center observers after refs mount", async (
312313
});
313314
});
314315

316+
test("pinned target settles only after resize correction and a paint frame", async () => {
317+
const refs = {
318+
container: { current: null },
319+
content: { current: null },
320+
};
321+
const root = createRoot(document.createElement("div"));
322+
const settled = [];
323+
const nodes = makePinnedCenterNodes();
324+
refs.container.current = nodes.container;
325+
refs.content.current = nodes.content;
326+
327+
await act(async () => {
328+
root.render(
329+
React.createElement(Harness, {
330+
channelId: "conversation",
331+
onTargetSettled: (messageId) => settled.push(messageId),
332+
refs,
333+
}),
334+
);
335+
});
336+
337+
assert.deepEqual(settled, [], "initial target reach is not settled");
338+
nodes.moveSelectedRowBy(96);
339+
nodes.resizeObservers[0].callback();
340+
assert.deepEqual(settled, [], "resize callback waits for the paint frame");
341+
await act(async () => new Promise((resolve) => setTimeout(resolve, 0)));
342+
343+
assert.deepEqual(settled, ["selected"]);
344+
assert.deepEqual(nodes.container.scrollWrites, [96]);
345+
await act(async () => root.unmount());
346+
});
347+
348+
test("user interaction releases and retires a pending pinned target", async () => {
349+
const refs = {
350+
container: { current: null },
351+
content: { current: null },
352+
};
353+
const root = createRoot(document.createElement("div"));
354+
const settled = [];
355+
const nodes = makePinnedCenterNodes();
356+
refs.container.current = nodes.container;
357+
refs.content.current = nodes.content;
358+
359+
await act(async () => {
360+
root.render(
361+
React.createElement(Harness, {
362+
channelId: "conversation",
363+
onTargetSettled: (messageId) => settled.push(messageId),
364+
refs,
365+
}),
366+
);
367+
});
368+
await act(async () => nodes.container.dispatchEvent({ type: "wheel" }));
369+
nodes.moveSelectedRowBy(96);
370+
nodes.resizeObservers[0].callback();
371+
await act(async () => new Promise((resolve) => setTimeout(resolve, 0)));
372+
373+
assert.deepEqual(settled, ["selected"]);
374+
assert.deepEqual(nodes.container.scrollWrites, []);
375+
await act(async () => root.unmount());
376+
});
377+
315378
test("mounted virtual target retires bottom intent before direct centering", async () => {
316379
const resizeObservers = [];
317380
globalThis.ResizeObserver = class {

0 commit comments

Comments
 (0)