Skip to content

Free, Open Source, Self-Hosted WhatsApp API Gateway

fix(dashboard): hold position through several same-frame media decodes

Yudhi Armyndharis5b781f59b06d
diff --git a/dashboard/src/hooks/useChatScrollPosition.test.ts b/dashboard/src/hooks/useChatScrollPosition.test.ts
index 31e7df5a4..e45b9ed6d 100644
--- a/dashboard/src/hooks/useChatScrollPosition.test.ts
+++ b/dashboard/src/hooks/useChatScrollPosition.test.ts
@@ -80,10 +80,12 @@ test('media straddling the top edge is left uncorrected rather than over-correct
// that decodes above it pushes everything below down, so scrollTop has to grow by the same amount to
// keep the same content on screen.
-test('an element that decoded above the reader is corrected by exactly its growth', () => {
+// correctionForMediaGrowth returns the DELTA to add to a live scrollTop (or null), not an absolute
+// target, so a second same-frame decode stacks on the first rather than overwriting it.
+test('an element that decoded above the reader yields exactly its growth as the delta', () => {
// Seeded 0px at mount, now 300px, and its post-decode bottom sits at 90: before it grew, its
// bottom was at -210, well above the reading position.
- assert.equal(correctionForMediaGrowth(1000, 0, 300, 90, CONTAINER_TOP), 1300);
+ assert.equal(correctionForMediaGrowth(0, 300, 90, CONTAINER_TOP), 300);
});
/**
@@ -91,14 +93,14 @@ test('an element that decoded above the reader is corrected by exactly its growt
* already in layout, so seeded === current, the growth reads as zero, and nothing is corrected
* while the reader has in fact been displaced.
*/
-test('a baseline equal to the current height yields no correction, which is why it is seeded at mount', () => {
- assert.equal(correctionForMediaGrowth(1000, 300, 300, 90, CONTAINER_TOP), null);
+test('a baseline equal to the current height yields no delta, which is why it is seeded at mount', () => {
+ assert.equal(correctionForMediaGrowth(300, 300, 90, CONTAINER_TOP), null);
});
-test('an element that decoded below the reader is left alone', () => {
+test('an element that decoded below the reader yields no delta', () => {
// Grew 300, and even before growing its bottom was at 500: far below the top edge, so nothing
// the reader can see moved. Correcting here would drag them toward the newest messages.
- assert.equal(correctionForMediaGrowth(1000, 0, 300, 800, CONTAINER_TOP), null);
+ assert.equal(correctionForMediaGrowth(0, 300, 800, CONTAINER_TOP), null);
});
/**
@@ -107,10 +109,26 @@ test('an element that decoded below the reader is left alone', () => {
* was at 50, above the reader, so it did displace them.
*/
test('the above/below question is asked against the pre-decode layout', () => {
- assert.equal(correctionForMediaGrowth(1000, 0, 100, 150, CONTAINER_TOP), 1100);
+ assert.equal(correctionForMediaGrowth(0, 100, 150, CONTAINER_TOP), 100);
});
-test('a shrink or an unchanged box corrects nothing', () => {
- assert.equal(correctionForMediaGrowth(1000, 300, 300, 50, CONTAINER_TOP), null);
- assert.equal(correctionForMediaGrowth(1000, 300, 120, 50, CONTAINER_TOP), null);
+test('a shrink or an unchanged box yields no delta', () => {
+ assert.equal(correctionForMediaGrowth(300, 300, 50, CONTAINER_TOP), null);
+ assert.equal(correctionForMediaGrowth(300, 120, 50, CONTAINER_TOP), null);
+});
+
+/**
+ * Two above-the-fold elements decoding in one frame. Each returns its own growth as a delta, and
+ * the caller adds each to the live scrollTop, so the reader is held through the SUM. Modelled here
+ * as the caller does it: start at 1000, apply the first delta, then the second against the result.
+ */
+test('two same-frame deltas accumulate rather than overwrite', () => {
+ const d1 = correctionForMediaGrowth(0, 300, 90, CONTAINER_TOP);
+ const d2 = correctionForMediaGrowth(0, 300, 80, CONTAINER_TOP);
+ assert.equal(d1, 300);
+ assert.equal(d2, 300);
+ let scrollTop = 1000;
+ scrollTop += d1 as number;
+ scrollTop += d2 as number;
+ assert.equal(scrollTop, 1600); // 600 total, not 300
});
diff --git a/dashboard/src/hooks/useChatScrollPosition.ts b/dashboard/src/hooks/useChatScrollPosition.ts
index 25a5dd662..2704f135c 100644
--- a/dashboard/src/hooks/useChatScrollPosition.ts
+++ b/dashboard/src/hooks/useChatScrollPosition.ts
@@ -93,8 +93,11 @@ export function grewAboveReadingPosition(mediaBottom: number, containerTop: numb
}
/**
- * The scrollTop that holds the reader still after a media element finished decoding, or null when
- * nothing on screen moved.
+ * How much `scrollTop` must gain to hold the reader still after a media element finished decoding,
+ * or null when nothing on screen moved. This is a DELTA, not an absolute target, so the caller adds
+ * it to the LIVE scrollTop at apply time: two elements decoding in one frame each contribute their
+ * own growth and the reader is held through the sum, where a captured absolute would have the second
+ * write overwrite the first with the same value and under-correct.
*
* `seededHeight` is the element's box height recorded when it MOUNTED. That matters: by the time a
* `load` handler runs, the decoded size is already in layout, and every geometry read there flushes
@@ -106,7 +109,6 @@ export function grewAboveReadingPosition(mediaBottom: number, containerTop: numb
* the container's top edge by exactly the amount it grew.
*/
export function correctionForMediaGrowth(
- scrollTop: number,
seededHeight: number,
currentHeight: number,
mediaBottom: number,
@@ -114,7 +116,7 @@ export function correctionForMediaGrowth(
): number | null {
const grew = currentHeight - seededHeight;
if (grew <= 0) return null;
- return grewAboveReadingPosition(mediaBottom - grew, containerTop) ? scrollTop + grew : null;
+ return grewAboveReadingPosition(mediaBottom - grew, containerTop) ? grew : null;
}
export function useChatScrollPosition(
@@ -327,14 +329,8 @@ export function useChatScrollPosition(
// Re-seed before the frame: the element has finished growing, so its current box is the
// baseline for any later change (a re-decode on a src swap, a lazy dimension arriving).
mediaHeights.current.set(media, rect.height);
- const corrected = correctionForMediaGrowth(
- el.scrollTop,
- seeded,
- rect.height,
- rect.bottom,
- el.getBoundingClientRect().top,
- );
- if (corrected === null) return;
+ const grew = correctionForMediaGrowth(seeded, rect.height, rect.bottom, el.getBoundingClientRect().top);
+ if (grew === null) return;
requestAnimationFrame(() => {
const cur = containerRef.current;
if (!cur) return;
@@ -342,8 +338,12 @@ export function useChatScrollPosition(
// apply the correction to a different thread and save it under the outgoing chat's id.
// prevChatIdRef holds whichever chat is actually on screen, set by the restore effect above.
if (prevChatIdRef.current !== activeChatId) return;
- writeScrollTop(cur, corrected);
- if (activeChatId !== null) scrollMap.current.set(activeChatId, corrected);
+ // Add to the LIVE scrollTop, not a value captured at event time: a sibling element decoding
+ // in the same frame has its own rAF that already moved scrollTop, and this one must stack on
+ // top of it. A captured absolute would re-assert the pre-sibling position and lose the sum.
+ const next = cur.scrollTop + grew;
+ writeScrollTop(cur, next);
+ if (activeChatId !== null) scrollMap.current.set(activeChatId, next);
});
},
[activeChatId, pinToBottom, writeScrollTop],