Skip to content

Commit 884f705

Browse files
authored
fix(desktop): clear stale thread new-message pill (#3411)
## Summary - reconcile anchored-scroll state when passive layout changes put a thread at its physical floor - route thread composer-padding growth and shrink through the same hook-owned settlement path - preserve pinned thread targets while clearing stale new-message state ## Root cause Thread bottom state was updated primarily by native `scroll` events. Deferred replies, viewport changes, and composer-overlay padding can finish changing geometry after the user's last scroll—or after the initial open pin—without another scroll event. The thread could visibly reach the floor while `isAtBottom` and `newMessageCount` remained stale, leaving the “N new messages” pill visible. ## Verification - `pnpm check` - `pnpm typecheck` - `pnpm test` — 3,768 passed - push hook: branch-skew, Desktop check, and Desktop full unit suite passed Signed-off-by: Wes <[email protected]>
1 parent eb588c6 commit 884f705

3 files changed

Lines changed: 122 additions & 27 deletions

File tree

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

Lines changed: 25 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -251,11 +251,6 @@ export function MessageThreadPanel({
251251
// conditional activity accessory (agent working and/or someone typing).
252252
const hasComposerBottomActivity =
253253
activityAccessoryVisible || threadTypingPubkeys.length > 0;
254-
useComposerHeightPadding(
255-
threadBodyRef,
256-
threadComposerWrapperRef,
257-
isSinglePanelView,
258-
);
259254

260255
// Live ref so onCaptureSendContext can read reply state at submit time
261256
// (before any async mention-flow awaits change navigation state).
@@ -490,19 +485,31 @@ export function MessageThreadPanel({
490485
threadHead,
491486
]);
492487

493-
const { isAtBottom, newMessageCount, onScroll, scrollToBottom } =
494-
useAnchoredScroll({
495-
channelId: threadHeadId,
496-
contentRef: threadContentRef,
497-
isLoading: threadRepliesPending || repliesRenderState === "pending",
498-
messages: threadMessages,
499-
highlightTargetMessage: scrollTargetHighlights,
500-
onTargetReached: onScrollTargetResolved,
501-
onTargetSettled: onScrollTargetSettled,
502-
pinTargetCentered: !scrollTargetHighlights,
503-
scrollContainerRef: threadBodyRef,
504-
targetMessageId: scrollTargetId,
505-
});
488+
const {
489+
isAtBottom,
490+
newMessageCount,
491+
onScroll,
492+
scrollToBottom,
493+
settleAtBottomAfterLayout,
494+
} = useAnchoredScroll({
495+
channelId: threadHeadId,
496+
contentRef: threadContentRef,
497+
isLoading: threadRepliesPending || repliesRenderState === "pending",
498+
messages: threadMessages,
499+
highlightTargetMessage: scrollTargetHighlights,
500+
onTargetReached: onScrollTargetResolved,
501+
onTargetSettled: onScrollTargetSettled,
502+
pinTargetCentered: !scrollTargetHighlights,
503+
scrollContainerRef: threadBodyRef,
504+
targetMessageId: scrollTargetId,
505+
});
506+
useComposerHeightPadding(
507+
threadBodyRef,
508+
threadComposerWrapperRef,
509+
isSinglePanelView,
510+
"padding",
511+
settleAtBottomAfterLayout,
512+
);
506513

507514
const knownAgentPubkeys = useKnownAgentPubkeys();
508515
const initialAgentPubkeys = React.useMemo(() => {

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

Lines changed: 59 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -218,7 +218,8 @@ function makePinnedCenterNodes() {
218218
disconnect() {}
219219

220220
observe(target) {
221-
this.target = target;
221+
this.targets ??= [];
222+
this.targets.push(target);
222223
}
223224
};
224225

@@ -246,6 +247,18 @@ function Harness({ channelId, onTargetSettled, refs }) {
246247
return null;
247248
}
248249

250+
function BottomStateHarness({ messages, onState, refs }) {
251+
const anchored = useAnchoredScroll({
252+
channelId: "conversation",
253+
contentRef: refs.content,
254+
isLoading: false,
255+
messages,
256+
scrollContainerRef: refs.container,
257+
});
258+
onState(anchored);
259+
return null;
260+
}
261+
249262
function VirtualTargetHarness({ refs }) {
250263
const didRun = React.useRef(false);
251264
const bottomApi = useVirtualizedBottomSettle(
@@ -294,7 +307,10 @@ test("channel change attaches pinned-center observers after refs mount", async (
294307
});
295308

296309
assert.equal(nodes.resizeObservers.length, 1);
297-
assert.equal(nodes.resizeObservers[0].target, nodes.content);
310+
assert.deepEqual(nodes.resizeObservers[0].targets, [
311+
nodes.content,
312+
nodes.container,
313+
]);
298314
assert.equal(nodes.container.listeners.get("wheel")?.length, 1);
299315

300316
await act(async () => {
@@ -313,7 +329,47 @@ test("channel change attaches pinned-center observers after refs mount", async (
313329
});
314330
});
315331

316-
test("pinned target settles only after resize correction and a paint frame", async () => {
332+
test("container resize clears a stale new-message state at the physical floor", async () => {
333+
const refs = {
334+
container: { current: null },
335+
content: { current: null },
336+
};
337+
const root = createRoot(document.createElement("div"));
338+
const nodes = makePinnedCenterNodes();
339+
refs.container.current = nodes.container;
340+
refs.content.current = nodes.content;
341+
let state = null;
342+
const render = (messages) =>
343+
root.render(
344+
React.createElement(BottomStateHarness, {
345+
messages,
346+
onState: (nextState) => {
347+
state = nextState;
348+
},
349+
refs,
350+
}),
351+
);
352+
353+
await act(async () => render([{ id: "first" }]));
354+
await act(async () => new Promise((resolve) => setTimeout(resolve, 0)));
355+
nodes.container.scrollTop = 100;
356+
await act(async () => state.onScroll());
357+
nodes.container.scrollTop = 100;
358+
await act(async () => state.onScroll());
359+
await act(async () => render([{ id: "first" }, { id: "second" }]));
360+
assert.equal(state.isAtBottom, false);
361+
assert.equal(state.newMessageCount, 1);
362+
363+
// A taller viewport reaches the floor without producing a native scroll.
364+
nodes.container.clientHeight = 900;
365+
await act(async () => nodes.resizeObservers[0].callback());
366+
367+
assert.equal(state.isAtBottom, true);
368+
assert.equal(state.newMessageCount, 0);
369+
await act(async () => root.unmount());
370+
});
371+
372+
test("pinned target resize reconciles bottom state before retiring", async () => {
317373
const refs = {
318374
container: { current: null },
319375
content: { current: null },

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

Lines changed: 38 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -73,6 +73,9 @@ type UseAnchoredScrollResult = {
7373
highlightedMessageId: string | null;
7474
/** Imperative: scroll to bottom. */
7575
scrollToBottom: (behavior?: ScrollBehavior) => void;
76+
/** Re-pins after a layout owner changes trailing geometry. Returns true when
77+
* the hook handled the settlement, including a preserved pinned target. */
78+
settleAtBottomAfterLayout: () => boolean;
7679
/** Arm a one-shot scroll-to-bottom that fires on the next appended message
7780
* (used by the composer's send flow). */
7881
scrollToBottomOnNextUpdate: () => void;
@@ -383,6 +386,35 @@ export function useAnchoredScroll({
383386
forceBottomOnNextAppendRef.current = true;
384387
}, []);
385388

389+
const settleAtBottomAfterLayout = React.useCallback(() => {
390+
const container = scrollContainerRef.current;
391+
if (!container) return false;
392+
if (anchorRef.current.kind === "pinned-center") {
393+
repinPinnedCenter();
394+
const atBottom = isAtBottomNow(container);
395+
setIsAtBottom((previous) =>
396+
previous === atBottom ? previous : atBottom,
397+
);
398+
if (atBottom) setNewMessageCount(0);
399+
schedulePinnedTargetSettle(anchorRef.current.messageId);
400+
return true;
401+
}
402+
if (!isAtBottomNow(container)) return false;
403+
404+
anchorRef.current = { kind: "at-bottom" };
405+
setIsAtBottom(true);
406+
setNewMessageCount(0);
407+
if (!virtualizerOwnsPrependAnchoring) {
408+
container.scrollTo({ top: container.scrollHeight, behavior: "auto" });
409+
}
410+
return true;
411+
}, [
412+
repinPinnedCenter,
413+
schedulePinnedTargetSettle,
414+
scrollContainerRef,
415+
virtualizerOwnsPrependAnchoring,
416+
]);
417+
386418
const highlightMessage = React.useCallback((messageId: string) => {
387419
if (highlightTimeoutRef.current !== null) {
388420
window.clearTimeout(highlightTimeoutRef.current);
@@ -743,17 +775,17 @@ export function useAnchoredScroll({
743775
const observer = new ResizeObserver(() => {
744776
const container = scrollContainerRef.current;
745777
if (!container) return;
746-
if (anchorRef.current.kind === "pinned-center") {
747-
repinPinnedCenter();
748-
schedulePinnedTargetSettle(anchorRef.current.messageId);
749-
} else if (
778+
if (settleAtBottomAfterLayout()) return;
779+
if (
750780
anchorRef.current.kind === "at-bottom" &&
751781
!virtualizerOwnsPrependAnchoring
752782
) {
753783
container.scrollTo({ top: container.scrollHeight, behavior: "auto" });
754784
}
755785
});
756786
observer.observe(content);
787+
const container = scrollContainerRef.current;
788+
if (container && container !== content) observer.observe(container);
757789
return () => {
758790
observer.disconnect();
759791
if (targetSettleRafRef.current !== null) {
@@ -764,9 +796,8 @@ export function useAnchoredScroll({
764796
}, [
765797
channelId,
766798
contentRef,
767-
repinPinnedCenter,
768-
schedulePinnedTargetSettle,
769799
scrollContainerRef,
800+
settleAtBottomAfterLayout,
770801
virtualizerOwnsPrependAnchoring,
771802
]);
772803

@@ -919,6 +950,7 @@ export function useAnchoredScroll({
919950
newMessageCount,
920951
highlightedMessageId,
921952
scrollToBottom: scrollToBottomImperative,
953+
settleAtBottomAfterLayout,
922954
scrollToBottomOnNextUpdate,
923955
scrollToMessage: scrollToMessageImperative,
924956
onVirtualizerAtBottomStateChange,

0 commit comments

Comments
 (0)