Skip to content

Commit bf94273

Browse files
fix(mobile): wait for native tool-group scrolling
1 parent ee442ac commit bf94273

3 files changed

Lines changed: 175 additions & 23 deletions

File tree

‎apps/mobile/src/features/threads/thread-feed-live-follow.test.ts‎

Lines changed: 75 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
import { describe, expect, it } from "vite-plus/test";
22

33
import {
4+
canCompleteThreadWorkGroupAppend,
45
resolveThreadFeedLiveFollow,
56
resolveThreadFeedSubmissionAnchor,
67
resolveThreadWorkGroupInitialScroll,
@@ -46,6 +47,7 @@ describe("tool-group append following", () => {
4647
scrollOffset: 33,
4748
detailsChanged: false,
4849
userScrolling: false,
50+
followPending: false,
4951
};
5052

5153
it("follows a new call when the reader was at the end", () => {
@@ -66,6 +68,41 @@ describe("tool-group append following", () => {
6668
).toBe(true);
6769
});
6870

71+
it("keeps following consecutive appends while the native scroll offset is still catching up", () => {
72+
const firstAppend = {
73+
...atEnd,
74+
previousContentHeight: 259,
75+
contentHeight: 287,
76+
scrollOffset: 3.2,
77+
};
78+
const followPending = shouldFollowThreadWorkGroupAppend(firstAppend);
79+
expect(followPending).toBe(true);
80+
81+
const nextAppend = {
82+
...firstAppend,
83+
previousRows: appendedRows,
84+
rows: [...appendedRows, { id: "next-call" }],
85+
previousContentHeight: 287,
86+
contentHeight: 316,
87+
followPending,
88+
};
89+
expect(shouldFollowThreadWorkGroupAppend({ ...nextAppend, followPending: false })).toBe(false);
90+
expect(shouldFollowThreadWorkGroupAppend(nextAppend)).toBe(true);
91+
});
92+
93+
it("cancels pending following for manual scrolling, detail changes, or changes to existing calls", () => {
94+
const pendingAppend = { ...atEnd, followPending: true };
95+
const cancellations = [
96+
{ userScrolling: true },
97+
{ detailsChanged: true },
98+
{ rows: [{ id: "replacement" }, ...appendedRows.slice(1)] },
99+
{ rows: previousRows.slice(0, -1) },
100+
];
101+
for (const changes of cancellations) {
102+
expect(shouldFollowThreadWorkGroupAppend({ ...pendingAppend, ...changes })).toBe(false);
103+
}
104+
});
105+
69106
it.each([
70107
{ name: "reading earlier calls", changes: { scrollOffset: 20 } },
71108
{ name: "dragging before leaving the edge", changes: { userScrolling: true } },
@@ -82,6 +119,44 @@ describe("tool-group append following", () => {
82119
});
83120
});
84121

122+
describe("tool-group pending append readiness", () => {
123+
const firstAppend = {
124+
targetContentHeight: 259,
125+
nativeContentHeight: 259.2,
126+
targetViewportHeight: 256,
127+
measuredViewportHeight: 231.47,
128+
userScrolling: false,
129+
};
130+
const secondAppend = {
131+
...firstAppend,
132+
targetContentHeight: 287,
133+
measuredViewportHeight: 256,
134+
};
135+
136+
it("waits for both the viewport and native content before following consecutive appends", () => {
137+
expect(canCompleteThreadWorkGroupAppend(firstAppend)).toBe(false);
138+
expect(canCompleteThreadWorkGroupAppend({ ...firstAppend, measuredViewportHeight: 256 })).toBe(
139+
true,
140+
);
141+
142+
// At the height cap, matching viewport geometry does not mean native content has grown.
143+
expect(canCompleteThreadWorkGroupAppend(secondAppend)).toBe(false);
144+
expect(canCompleteThreadWorkGroupAppend({ ...secondAppend, nativeContentHeight: 287.47 })).toBe(
145+
true,
146+
);
147+
});
148+
149+
it("does not complete an append while the reader scrolls, even after native geometry catches up", () => {
150+
expect(
151+
canCompleteThreadWorkGroupAppend({
152+
...secondAppend,
153+
nativeContentHeight: 287.47,
154+
userScrolling: true,
155+
}),
156+
).toBe(false);
157+
});
158+
});
159+
85160
describe("resolveThreadFeedSubmissionAnchor", () => {
86161
it("anchors the first user message in a thread", () => {
87162
expect(

‎apps/mobile/src/features/threads/thread-feed-live-follow.ts‎

Lines changed: 23 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -36,14 +36,36 @@ export function shouldFollowThreadWorkGroupAppend(input: {
3636
readonly scrollOffset: number;
3737
readonly detailsChanged: boolean;
3838
readonly userScrolling: boolean;
39+
readonly followPending: boolean;
3940
}) {
4041
return (
4142
!input.detailsChanged &&
4243
!input.userScrolling &&
4344
input.contentHeight > input.previousContentHeight &&
4445
input.rows.length > input.previousRows.length &&
4546
input.previousRows.every((row, index) => row.id === input.rows[index]?.id) &&
46-
input.previousContentHeight - input.viewportHeight - input.scrollOffset <= 1
47+
(input.followPending ||
48+
input.previousContentHeight - input.viewportHeight - input.scrollOffset <= 1)
49+
);
50+
}
51+
52+
export function canCompleteThreadWorkGroupAppend(input: {
53+
readonly targetContentHeight: number;
54+
readonly nativeContentHeight: number;
55+
readonly targetViewportHeight: number;
56+
readonly measuredViewportHeight: number;
57+
readonly userScrolling: boolean;
58+
}) {
59+
return (
60+
!input.userScrolling &&
61+
[
62+
input.targetContentHeight,
63+
input.nativeContentHeight,
64+
input.targetViewportHeight,
65+
input.measuredViewportHeight,
66+
].every((height) => Number.isFinite(height) && height > 0) &&
67+
Math.abs(input.nativeContentHeight - input.targetContentHeight) <= 1 &&
68+
Math.abs(input.measuredViewportHeight - input.targetViewportHeight) <= 1
4769
);
4870
}
4971

‎apps/mobile/src/features/threads/thread-work-log.tsx‎

Lines changed: 77 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -73,6 +73,7 @@ import {
7373
} from "@t3tools/client-runtime/work-log/item-detail";
7474
import { useTurnItemDetail } from "../../state/queries";
7575
import {
76+
canCompleteThreadWorkGroupAppend,
7677
resolveThreadWorkGroupInitialScroll,
7778
shouldFollowThreadWorkGroupAppend,
7879
type ThreadWorkGroupScrollPosition,
@@ -573,7 +574,13 @@ function ThreadWorkGroupList(props: {
573574
const listRef = useRef<LegendListRef>(null);
574575
const loadedRef = useRef(false);
575576
const userScrollingRef = useRef(false);
576-
const pendingAppendHeightRef = useRef<number | null>(null);
577+
const pendingAppendRef = useRef<{
578+
contentHeight: number;
579+
viewportHeight: number;
580+
requested: boolean;
581+
} | null>(null);
582+
const nativeContentHeightRef = useRef(0);
583+
const measuredViewportHeightRef = useRef(0);
577584
const previousContent = useRef({
578585
rows: props.activities,
579586
height: Math.max(estimatedRowsHeight, initialPosition?.contentHeight ?? 0),
@@ -612,22 +619,30 @@ function ThreadWorkGroupList(props: {
612619
contentHeight: state.contentLength,
613620
});
614621
};
615-
const finishPendingAppend = () => {
616-
const targetHeight = pendingAppendHeightRef.current;
617-
const state = listRef.current?.getState();
622+
const requestPendingAppend = () => {
623+
const pending = pendingAppendRef.current;
624+
const list = listRef.current;
625+
// JS sizing can advance before native content and viewport geometry catch up.
618626
if (
619-
targetHeight !== null &&
620-
state &&
621-
!userScrollingRef.current &&
622-
Math.abs(state.scrollLength - targetHeight) <= 1
627+
pending !== null &&
628+
!pending.requested &&
629+
list !== null &&
630+
canCompleteThreadWorkGroupAppend({
631+
targetContentHeight: pending.contentHeight,
632+
nativeContentHeight: nativeContentHeightRef.current,
633+
targetViewportHeight: pending.viewportHeight,
634+
measuredViewportHeight: measuredViewportHeightRef.current,
635+
userScrolling: userScrollingRef.current,
636+
})
623637
) {
624-
pendingAppendHeightRef.current = null;
625-
void listRef.current?.scrollToEnd({ animated: false });
638+
// The public request queues work; native arrival settles the intent.
639+
pending.requested = true;
640+
void list.scrollToEnd({ animated: false });
626641
}
627642
};
628643
// LegendList notifies from child layout effects. Effect Events see the
629644
// committed rows before those effects run, unlike a parent layout-effect ref.
630-
const onContentSizeChange = useEffectEvent((nextHeight: number) => {
645+
const onComputedContentSizeChange = useEffectEvent((nextHeight: number) => {
631646
const previous = previousContent.current;
632647
const detailsChanged = previous.expandedRows !== props.expandedRows;
633648
const followAppend =
@@ -641,6 +656,7 @@ function ThreadWorkGroupList(props: {
641656
scrollOffset: scrollOffset.value,
642657
detailsChanged,
643658
userScrolling: userScrollingRef.current,
659+
followPending: pendingAppendRef.current !== null,
644660
});
645661
previousContent.current = {
646662
rows: props.activities,
@@ -653,13 +669,25 @@ function ThreadWorkGroupList(props: {
653669
: { height: nextHeight, rowCount: props.activities.length },
654670
);
655671
if (followAppend) {
656-
pendingAppendHeightRef.current = Math.min(nextHeight, WORK_GROUP_MAX_HEIGHT);
672+
pendingAppendRef.current = {
673+
contentHeight: nextHeight,
674+
viewportHeight: Math.min(nextHeight, WORK_GROUP_MAX_HEIGHT),
675+
requested: false,
676+
};
657677
} else if (detailsChanged || userScrollingRef.current || previous.rows !== props.activities) {
658-
pendingAppendHeightRef.current = null;
659-
} else if (pendingAppendHeightRef.current !== null) {
660-
pendingAppendHeightRef.current = Math.min(nextHeight, WORK_GROUP_MAX_HEIGHT);
678+
pendingAppendRef.current = null;
679+
} else if (
680+
pendingAppendRef.current !== null &&
681+
(pendingAppendRef.current.contentHeight !== nextHeight ||
682+
pendingAppendRef.current.viewportHeight !== Math.min(nextHeight, WORK_GROUP_MAX_HEIGHT))
683+
) {
684+
pendingAppendRef.current = {
685+
contentHeight: nextHeight,
686+
viewportHeight: Math.min(nextHeight, WORK_GROUP_MAX_HEIGHT),
687+
requested: false,
688+
};
661689
}
662-
finishPendingAppend();
690+
requestPendingAppend();
663691
rememberPosition();
664692
});
665693
// The native ScrollView reports its content size a frame or more after
@@ -671,9 +699,9 @@ function ThreadWorkGroupList(props: {
671699
const list = listRef.current;
672700
if (!list) return;
673701
const unsubscribe = list.getState().listen("totalSize", () => {
674-
onContentSizeChange(list.getState().contentLength);
702+
onComputedContentSizeChange(list.getState().contentLength);
675703
});
676-
onContentSizeChange(list.getState().contentLength);
704+
onComputedContentSizeChange(list.getState().contentLength);
677705
return unsubscribe;
678706
}, []);
679707
const getFixedItemSize = (row: ThreadFeedActivity, index: number) =>
@@ -704,23 +732,50 @@ function ThreadWorkGroupList(props: {
704732
extraData={props.renderRow}
705733
renderItem={renderItem}
706734
sharedValues={sharedValues}
707-
onLayout={finishPendingAppend}
735+
onLayout={(event) => {
736+
measuredViewportHeightRef.current = event.nativeEvent.layout.height;
737+
requestPendingAppend();
738+
}}
739+
onContentSizeChange={(_width, height) => {
740+
nativeContentHeightRef.current = height;
741+
requestPendingAppend();
742+
}}
708743
onLoad={() => {
709744
loadedRef.current = true;
710745
setRestoringPosition(false);
711746
rememberPosition();
712747
}}
713-
onScroll={rememberPosition}
748+
onScroll={(event) => {
749+
const pending = pendingAppendRef.current;
750+
const { contentOffset, contentSize, layoutMeasurement } = event.nativeEvent;
751+
if (
752+
pending !== null &&
753+
canCompleteThreadWorkGroupAppend({
754+
targetContentHeight: pending.contentHeight,
755+
nativeContentHeight: contentSize.height,
756+
targetViewportHeight: pending.viewportHeight,
757+
measuredViewportHeight: layoutMeasurement.height,
758+
userScrolling: userScrollingRef.current,
759+
}) &&
760+
Number.isFinite(contentOffset.y) &&
761+
Math.abs(
762+
contentOffset.y - Math.max(0, contentSize.height - layoutMeasurement.height),
763+
) <= 1
764+
) {
765+
pendingAppendRef.current = null;
766+
}
767+
rememberPosition();
768+
}}
714769
onScrollBeginDrag={() => {
715770
userScrollingRef.current = true;
716-
pendingAppendHeightRef.current = null;
771+
pendingAppendRef.current = null;
717772
}}
718773
onScrollEndDrag={() => {
719774
userScrollingRef.current = false;
720775
}}
721776
onMomentumScrollBegin={() => {
722777
userScrollingRef.current = true;
723-
pendingAppendHeightRef.current = null;
778+
pendingAppendRef.current = null;
724779
}}
725780
onMomentumScrollEnd={() => {
726781
userScrollingRef.current = false;

0 commit comments

Comments
 (0)