Skip to content

Commit fa0926d

Browse files
author
Julius Marminge
committed
fix(mobile): preserve position during first disclosure expansion
1 parent 479f910 commit fa0926d

5 files changed

Lines changed: 181 additions & 86 deletions

File tree

‎apps/mobile/src/features/threads/ThreadFeed.tsx‎

Lines changed: 14 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1838,12 +1838,12 @@ export const ThreadFeed = memo(function ThreadFeed(props: ThreadFeedProps) {
18381838
// Live-follow latch. LegendList's maintainScrollAtEnd alone re-pins the feed
18391839
// whenever the viewport drifts back inside its geometric threshold, which
18401840
// yanked users off history they were reading every time a stream chunk grew
1841-
// a row. Follow breaks when the user scrolls up and away, and re-arms only
1842-
// when the list actually returns to the end (or on send / thread switch).
1841+
// a row. Scrolling away or expanding a disclosure above the end breaks
1842+
// follow; reaching the end (or sending / switching threads) re-arms it.
18431843
const [endFollowEnabled, setEndFollowEnabled] = useState(true);
18441844
const endFollowEnabledRef = useRef(true);
18451845
// A "user scroll session" spans from drag start through the end of its
1846-
// momentum; only motion inside a session can break follow, so MVCP
1846+
// momentum; scroll events only break follow inside that session, so MVCP
18471847
// compensations and programmatic scrolls never strand a follower.
18481848
const userScrollSessionRef = useRef(false);
18491849
const setEndFollow = useCallback(
@@ -2271,13 +2271,23 @@ export const ThreadFeed = memo(function ThreadFeed(props: ThreadFeedProps) {
22712271
}
22722272
disclosureSettleFrameRef.current = requestAnimationFrame(() => {
22732273
disclosureSettleSecondFrameRef.current = requestAnimationFrame(() => {
2274+
// A disclosure can leave the reader above the end without a drag.
2275+
// Reconcile follow before a later layout or resume can re-pin it.
2276+
const listState = props.listRef.current?.getState();
2277+
if (listState) {
2278+
transitionEndFollow({
2279+
type: "disclosure-settled",
2280+
isAtEnd: listState.isAtEnd,
2281+
userScrollSessionActive: userScrollSessionRef.current,
2282+
});
2283+
}
22742284
disclosureAnchorKeyRef.current = null;
22752285
setDisclosureToggleSettling(false);
22762286
disclosureSettleFrameRef.current = null;
22772287
disclosureSettleSecondFrameRef.current = null;
22782288
});
22792289
});
2280-
}, []);
2290+
}, [props.listRef, transitionEndFollow]);
22812291

22822292
const suspendEndScrollMaintenanceForDisclosure = useCallback((anchorKey: string | null) => {
22832293
disclosureAnchorKeyRef.current = anchorKey;

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

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -102,6 +102,17 @@ describe("resolveThreadFeedLiveFollow", () => {
102102
).toBe(false);
103103
});
104104

105+
it.each([
106+
{ isAtEnd: false, userScrollSessionActive: false, expected: false },
107+
{ isAtEnd: true, userScrollSessionActive: false, expected: true },
108+
{ isAtEnd: false, userScrollSessionActive: true, expected: false },
109+
{ isAtEnd: true, userScrollSessionActive: true, expected: false },
110+
])("reconciles follow after a disclosure settles: %j", ({ expected, ...state }) => {
111+
expect(resolveThreadFeedLiveFollow(!expected, { type: "disclosure-settled", ...state })).toBe(
112+
expected,
113+
);
114+
});
115+
105116
it("re-arms at the actual end only after the user scroll session ends", () => {
106117
expect(
107118
resolveThreadFeedLiveFollow(false, {

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

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ export type ThreadFeedLiveFollowEvent =
77
readonly userScrollSessionActive: boolean;
88
}
99
| {
10-
readonly type: "scroll";
10+
readonly type: "scroll" | "disclosure-settled";
1111
readonly isAtEnd: boolean;
1212
readonly userScrollSessionActive: boolean;
1313
};
@@ -41,6 +41,8 @@ export function resolveThreadFeedLiveFollow(
4141
return false;
4242
case "user-scroll-end":
4343
return event.userScrollSessionActive ? event.isAtEnd : current;
44+
case "disclosure-settled":
45+
return !event.userScrollSessionActive && event.isAtEnd;
4446
case "scroll":
4547
if (event.userScrollSessionActive) {
4648
return false;

0 commit comments

Comments
 (0)