Skip to content

Commit b42a8d4

Browse files
wesbillmanCarl
andauthored
fix(desktop): reconcile thread arrivals at bottom (#3585)
## Summary - reconcile stale native-scroll anchors when a reply arrives at the physical floor - clear the thread new-message affordance instead of incrementing it from stale cached state - preserve the existing mid-history path and add direct lifecycle regression coverage ## Why PR #3411 fixed geometry-driven reconciliation, but the reply-arrival branch still trusted a cached `message` anchor without checking the rendered position. Native anchoring could return a short thread to the floor without another scroll/resize callback, then the next reply incremented the pill anyway. ## Verification - Desktop checks passed - Desktop typecheck passed - focused lifecycle test passed (6/6) - push hook full Desktop unit suite passed (3,770/3,770) - `git diff --check` passed Signed-off-by: Wes <[email protected]> Co-authored-by: Carl <c7ebe626f000404285d3686e1dc74cc07cc60a9754a150041ba132e14bd3e2ec@buzz.block.builderlab.xyz>
1 parent 047533c commit b42a8d4

2 files changed

Lines changed: 108 additions & 1 deletion

File tree

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

Lines changed: 92 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -247,13 +247,20 @@ function Harness({ channelId, onTargetSettled, refs }) {
247247
return null;
248248
}
249249

250-
function BottomStateHarness({ messages, onState, refs }) {
250+
function BottomStateHarness({
251+
messages,
252+
onState,
253+
refs,
254+
targetMessageId = null,
255+
}) {
251256
const anchored = useAnchoredScroll({
252257
channelId: "conversation",
253258
contentRef: refs.content,
254259
isLoading: false,
255260
messages,
261+
pinTargetCentered: targetMessageId !== null,
256262
scrollContainerRef: refs.container,
263+
targetMessageId,
257264
});
258265
onState(anchored);
259266
return null;
@@ -329,6 +336,90 @@ test("channel change attaches pinned-center observers after refs mount", async (
329336
});
330337
});
331338

339+
test("arrival at the physical floor does not preserve a stale unread state", async () => {
340+
const refs = {
341+
container: { current: null },
342+
content: { current: null },
343+
};
344+
const root = createRoot(document.createElement("div"));
345+
const nodes = makePinnedCenterNodes();
346+
refs.container.current = nodes.container;
347+
refs.content.current = nodes.content;
348+
let state = null;
349+
const render = (messages) =>
350+
root.render(
351+
React.createElement(BottomStateHarness, {
352+
messages,
353+
onState: (nextState) => {
354+
state = nextState;
355+
},
356+
refs,
357+
}),
358+
);
359+
360+
await act(async () => render([{ id: "first" }]));
361+
await act(async () => new Promise((resolve) => setTimeout(resolve, 0)));
362+
nodes.container.scrollTop = 100;
363+
await act(async () => state.onScroll());
364+
nodes.container.scrollTop = 100;
365+
await act(async () => state.onScroll());
366+
assert.equal(state.isAtBottom, false);
367+
368+
// Native anchoring can return the viewport to the floor without a scroll or
369+
// resize callback, leaving only the hook's cached message anchor stale.
370+
nodes.container.scrollTop =
371+
nodes.container.scrollHeight - nodes.container.clientHeight;
372+
await act(async () => render([{ id: "first" }, { id: "second" }]));
373+
374+
assert.equal(state.isAtBottom, true);
375+
assert.equal(state.newMessageCount, 0);
376+
await act(async () => root.unmount());
377+
});
378+
379+
test("arrival does not steal an active layout target during floor-like reflow", async () => {
380+
const refs = {
381+
container: { current: null },
382+
content: { current: null },
383+
};
384+
const root = createRoot(document.createElement("div"));
385+
const nodes = makePinnedCenterNodes();
386+
refs.container.current = nodes.container;
387+
refs.content.current = nodes.content;
388+
let state = null;
389+
const render = (messages, targetMessageId = null) =>
390+
root.render(
391+
React.createElement(BottomStateHarness, {
392+
messages,
393+
onState: (nextState) => {
394+
state = nextState;
395+
},
396+
refs,
397+
targetMessageId,
398+
}),
399+
);
400+
401+
await act(async () => render([{ id: "selected" }]));
402+
await act(async () => new Promise((resolve) => setTimeout(resolve, 0)));
403+
nodes.container.scrollTop = 100;
404+
await act(async () => state.onScroll());
405+
nodes.container.scrollTop = 100;
406+
await act(async () => state.onScroll());
407+
assert.equal(state.isAtBottom, false);
408+
409+
// A focus/split presentation switch can commit fresh replies while the old
410+
// container geometry momentarily reads as the physical floor. The explicit
411+
// layout target must win so the reading row is restored after reflow.
412+
nodes.container.scrollTop =
413+
nodes.container.scrollHeight - nodes.container.clientHeight;
414+
await act(async () =>
415+
render([{ id: "selected" }, { id: "second" }], "selected"),
416+
);
417+
418+
assert.equal(state.isAtBottom, false);
419+
assert.equal(state.newMessageCount, 1);
420+
await act(async () => root.unmount());
421+
});
422+
332423
test("container resize clears a stale new-message state at the physical floor", async () => {
333424
const refs = {
334425
container: { current: null },

‎desktop/src/features/messages/ui/useAnchoredScroll.ts‎

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -714,6 +714,22 @@ export function useAnchoredScroll({
714714
container.scrollTo({ top: container.scrollHeight, behavior: "auto" });
715715
}
716716
if (newLatestArrived) setNewMessageCount(0);
717+
} else if (
718+
messagesArrived > 0 &&
719+
!targetMessageId &&
720+
!virtualizerOwnsPrependAnchoring &&
721+
isAtBottomNow(container)
722+
) {
723+
// A native scroll/layout callback may not have reconciled a stale
724+
// message anchor before this append commits. If the rendered result is
725+
// still physically at the floor (common in short threads), do not turn
726+
// that stale anchor into a visible unread affordance. Active navigation
727+
// targets own the viewport and must be preserved across presentation
728+
// reflow even when the old geometry momentarily reads as the floor.
729+
anchorRef.current = { kind: "at-bottom" };
730+
container.scrollTo({ top: container.scrollHeight, behavior: "auto" });
731+
setIsAtBottom(true);
732+
setNewMessageCount(0);
717733
} else if (messagesArrived > 0 && !virtualizerOwnsPrependAnchoring) {
718734
// Anchored mid-history. An older-history prepend grows the content above
719735
// the reading row; the browser's native scroll anchoring does NOT correct

0 commit comments

Comments
 (0)