Skip to content

Commit c176734

Browse files
matt2eclaude
andcommitted
fix(desktop): end an own send's freeze hold when its row lands
Problem: Review of 3c47951dc ("fix(desktop): keep an own send out of the frozen timeline tail") raised two findings in MessageTimeline.tsx. - Warning: the hold on freezing the tail lasted as long as the send's bottom pin, not as long as the send. The pin fires only when the tail id changes (`newLatestArrived` in useAnchoredScroll), and only firing or a channel change cleared it. The window cache sorts rows by created_at, so the sent row doesn't become the tail when the last row has a later created_at (a sender whose clock runs ahead) or a same-second row sorts after it. The pin then stayed armed after the sent row rendered, and a reader who scrolled up never froze the tail. The next arrival to become the tail fired the pin and pulled them to the bottom instead of showing the pill. Before 3c47951dc the freeze buffered that arrival, so a stale pin could not fire under a reader. - Nit: `prepareForOwnMessage` set `semanticAtBottomRef.current = true`, which the hold already made redundant, and cancelling the queued freeze left behind the `suppressNextSemanticBottomRef` it had set. Fix: `useAnchoredScroll` keeps a pending-send flag next to the pin. `scrollToBottomOnNextUpdate` sets both. The restoration effect clears the flag on the first append after arming, whether or not it moves the tail (`newLatestArrived` or an "append" delta), and the channel reset clears it. `isBottomPinArmed` becomes `isOwnSendPending`, which reads the flag, and MessageTimeline holds off freezing only while it is set. The pin itself is unchanged and still waits for a new tail. Once the hold ends, a reader who scrolls up freezes the tail as before, so later arrivals wait behind the pill and a stale pin can't fire under them. This keeps arming and disarming in the hook. Disarming from ChannelPane after `onSendMessage` settles would need a second call site (the welcome add-agent flow also arms the pin), and the send's promise isn't tied to when the sent row reaches the deferred snapshot. `prepareForOwnMessage` now clears `suppressNextSemanticBottomRef` instead of setting the semantic ref, so dropping the queued freeze also drops the suppression it armed. No test covers this: a leftover suppression only swallowed a bottom report while the send's release was already making the timeline semantically at the bottom. Validation: - New case in `MessageTimeline.ownSend.jsdom-test.mjs`: the tail's createdAt is an hour ahead, the send lands above it, the reader scrolls up 200 px, and a row from the same fast clock arrives and becomes the tail. It expects "1 new message" and the reader's offset unchanged. On 3c47951dc it fails: no pill, and the pin moved the reader from 756 to the floor at 1052 and rendered the arrival. With the fix the reader stays at 756 and the arrival waits behind the pill. - Mutations: clearing the flag only on a new tail, or never on an append, fails only the new case. Removing the hold check fails the original "gap after the send" case, as before. - Messages UI jsdom tests (10) and `src/features/messages` unit tests (1530) pass. Full desktop `pnpm test`: 6933 unit and 179 jsdom tests pass. - `tsc --noEmit`, `biome check` on the changed files, `check:file-sizes` and `git diff --check` are clean. No browser E2E was run for this follow-up. Co-Authored-By: Claude Opus 5.5 <[email protected]> Signed-off-by: mtoohey <[email protected]> (cherry picked from commit a2a9767de1b4f01879c652b51e9c10e508678aa6)
1 parent f2c57af commit c176734

3 files changed

Lines changed: 61 additions & 16 deletions

File tree

‎desktop/src/features/messages/ui/MessageTimeline.ownSend.jsdom-test.mjs‎

Lines changed: 34 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -140,7 +140,7 @@ afterEach(() => {
140140
container?.remove();
141141
});
142142

143-
async function mountAtFloor() {
143+
async function mountAtFloor(initial = history) {
144144
const router = createRouter({
145145
routeTree: createRootRoute(),
146146
history: createMemoryHistory({ initialEntries: ["/"] }),
@@ -171,7 +171,7 @@ async function mountAtFloor() {
171171
container = document.createElement("div");
172172
document.body.appendChild(container);
173173
root = createRoot(container);
174-
await render(history);
174+
await render(initial);
175175
const scroller = container.querySelector('[data-testid="message-timeline"]');
176176
act(() => {
177177
for (const observer of resizeObservers) {
@@ -231,3 +231,35 @@ for (const { name, order } of [
231231
assert.equal(scroller.scrollTop, scroller.scrollHeight - VIEWPORT);
232232
});
233233
}
234+
235+
// Rows arrive sorted by created_at, so a tail from a sender whose clock runs
236+
// ahead stays the tail when the send lands above it, and the pin stays armed.
237+
// The send's row still ends its hold on freezing: a reader who then scrolls up
238+
// gets the next arrival behind the pill, even one that becomes the tail.
239+
test("a reader who scrolls up after a send that lands above the tail keeps their place", async () => {
240+
const earlier = history.slice(0, -1);
241+
const aheadTail = { ...history.at(-1), createdAt: sent.createdAt + 3600 };
242+
const aheadNext = { ...message(22), createdAt: aheadTail.createdAt + 1 };
243+
const { render, scroller, timeline } = await mountAtFloor([
244+
...earlier,
245+
aheadTail,
246+
]);
247+
248+
act(() => timeline.current.scrollToBottomOnNextUpdate());
249+
await render([...earlier, sent, aheadTail]);
250+
flushFrames();
251+
assert.ok(container.querySelector(`[data-message-id="${sent.id}"]`));
252+
253+
readerScrollsTo(scroller, scroller.scrollTop - 200);
254+
const readingOffset = scroller.scrollTop;
255+
flushFrames();
256+
await render([...earlier, sent, aheadTail, aheadNext]);
257+
flushFrames();
258+
259+
assert.equal(
260+
container.querySelector('[data-testid="message-scroll-to-latest"]')
261+
?.textContent,
262+
"1 new message",
263+
);
264+
assert.equal(scroller.scrollTop, readingOffset);
265+
});

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

Lines changed: 10 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -358,7 +358,7 @@ const MessageTimelineBase = React.forwardRef<
358358
onScroll,
359359
scrollToBottom,
360360
scrollToBottomOnNextUpdate,
361-
isBottomPinArmed,
361+
isOwnSendPending,
362362
scrollToMessage,
363363
onVirtualizerAtBottomStateChange,
364364
} = useAnchoredScroll({
@@ -425,17 +425,17 @@ const MessageTimelineBase = React.forwardRef<
425425
}
426426
} else if (hasConfirmedVirtualizerBottomRef.current) {
427427
onVirtualizerAtBottomStateChange(false);
428-
// An armed own-send pin owns the next append. Freezing the tail first
429-
// would buffer the sent row, and the pin would never see it arrive.
430-
// Virtua reports a stale gap here when a discarded render already
431-
// counted the sent row in its store.
432-
if (semanticAtBottomRef.current && !isBottomPinArmed()) {
428+
// Until the sent row lands, the own-send pin owns the next append.
429+
// Freezing the tail first would buffer the sent row, and the pin would
430+
// never see it arrive. Virtua reports a stale gap here when a
431+
// discarded render already counted the sent row in its store.
432+
if (semanticAtBottomRef.current && !isOwnSendPending()) {
433433
suppressNextSemanticBottomRef.current = true;
434434
queueSemanticBottom(false);
435435
}
436436
}
437437
},
438-
[isBottomPinArmed, onVirtualizerAtBottomStateChange, queueSemanticBottom],
438+
[isOwnSendPending, onVirtualizerAtBottomStateChange, queueSemanticBottom],
439439
);
440440
const hasPendingLiveRows = bufferedTimeline.pendingCount > 0;
441441
React.useEffect(() => {
@@ -486,12 +486,13 @@ const MessageTimelineBase = React.forwardRef<
486486
// The user's own send is the deliberate Zulip exception: release buffered
487487
// output before arming the next-append bottom pin so the sent row can enter
488488
// Virtua's model and become the new physical floor. Drop a freeze queued
489-
// before the send, or it would land after this release.
489+
// before the send, or it would land after this release, and the bottom
490+
// report suppression that freeze armed.
490491
if (semanticBottomRafRef.current !== null) {
491492
window.cancelAnimationFrame(semanticBottomRafRef.current);
492493
semanticBottomRafRef.current = null;
493494
}
494-
semanticAtBottomRef.current = true;
495+
suppressNextSemanticBottomRef.current = false;
495496
setIsSemanticallyAtBottom(true);
496497
scrollToBottomOnNextUpdate();
497498
}, [scrollToBottomOnNextUpdate]);

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

Lines changed: 17 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -88,8 +88,9 @@ type UseAnchoredScrollResult = {
8888
/** Arm a one-shot scroll-to-bottom that fires on the next appended message
8989
* (used by the composer's send flow). */
9090
scrollToBottomOnNextUpdate: () => void;
91-
/** True while that one-shot is armed and its append has not landed. */
92-
isBottomPinArmed: () => boolean;
91+
/** True from that arming until the first append lands, whether or not the
92+
* append moves the tail and fires the one-shot. */
93+
isOwnSendPending: () => boolean;
9394
/** Imperative: scroll a specific message into view; optionally pulse it.
9495
* Returns true if the row was found and scrolled, false otherwise. */
9596
scrollToMessage: (
@@ -252,6 +253,11 @@ export function useAnchoredScroll({
252253
// appends, we snap to bottom even if they had scrolled up to read history.
253254
// Consumed (and cleared) by the next append in the restoration effect.
254255
const forceBottomOnNextAppendRef = React.useRef(false);
256+
// Set with the one-shot, but any append clears it, including one that sorts
257+
// above the current tail (a sender whose clock runs ahead, a same-second
258+
// row) and so leaves the one-shot armed for a later tail. MessageTimeline
259+
// holds its tail open only while this is set.
260+
const ownSendPendingRef = React.useRef(false);
255261
// True from a programmatic bottom pin until the list's row measurement settles
256262
// and the view reaches a true physical bottom. During this window `onScroll`
257263
// ignores transient gaps and keeps chasing the floor. A `ref`, not state — the
@@ -288,6 +294,7 @@ export function useAnchoredScroll({
288294
prevMessagesRef.current = [];
289295
handledTargetIdRef.current = null;
290296
forceBottomOnNextAppendRef.current = false;
297+
ownSendPendingRef.current = false;
291298
settlingRef.current = false;
292299
programmaticScrollTopRef.current = null;
293300
isWritingScrollRef.current = false;
@@ -446,9 +453,10 @@ export function useAnchoredScroll({
446453
// outbound message pulls the view down even if they'd scrolled up.
447454
const scrollToBottomOnNextUpdate = React.useCallback(() => {
448455
forceBottomOnNextAppendRef.current = true;
456+
ownSendPendingRef.current = true;
449457
}, []);
450-
const isBottomPinArmed = React.useCallback(
451-
() => forceBottomOnNextAppendRef.current,
458+
const isOwnSendPending = React.useCallback(
459+
() => ownSendPendingRef.current,
452460
[],
453461
);
454462

@@ -739,6 +747,10 @@ export function useAnchoredScroll({
739747
previous: prevMessages,
740748
});
741749
const isPrepend = messageDelta === "prepend";
750+
// The first append ends a pending send, wherever its row sorts.
751+
if (newLatestArrived || messageDelta === "append") {
752+
ownSendPendingRef.current = false;
753+
}
742754

743755
// One-shot: an outbound send armed `scrollToBottomOnNextUpdate`. When the
744756
// resulting append lands, snap to bottom regardless of the current anchor,
@@ -1065,7 +1077,7 @@ export function useAnchoredScroll({
10651077
scrollToBottom: scrollToBottomImperative,
10661078
settleAtBottomAfterLayout,
10671079
scrollToBottomOnNextUpdate,
1068-
isBottomPinArmed,
1080+
isOwnSendPending,
10691081
scrollToMessage: scrollToMessageImperative,
10701082
onVirtualizerAtBottomStateChange,
10711083
};

0 commit comments

Comments
 (0)