Skip to content

Commit 7f3ffce

Browse files
saphidclaude
andcommitted
fix: hand message-link scrolling back to the user cleanly
Web cancellation reads the user's position when Legend runs it. Mobile waits for staged history to land, finds the row again at scroll time, and ends the link when the user drags. Co-Authored-By: Claude Opus 5.5 <[email protected]>
1 parent f3f1be9 commit 7f3ffce

4 files changed

Lines changed: 53 additions & 20 deletions

File tree

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

Lines changed: 39 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -2155,13 +2155,34 @@ export const ThreadFeed = memo(function ThreadFeed(props: ThreadFeedProps) {
21552155
userScrollSettleTimerRef.current = null;
21562156
}
21572157
}, []);
2158+
// A message link opens the feed at that message, loading earlier history through it and
2159+
// expanding the turn that hides it. That is a manual navigation, so live follow stops.
2160+
const linkedMessageId = props.linkedMessageId ?? null;
2161+
const onLinkedMessageHandled = props.onLinkedMessageHandled;
2162+
const handledLinkedMessageIdRef = useRef<string | null>(null);
2163+
// The feed a load through the linked message started from. The message is missing once a
2164+
// finished load leaves the feed unchanged.
2165+
const [linkedMessageLoad, setLinkedMessageLoad] = useState<{
2166+
readonly feed: ReadonlyArray<ThreadFeedEntry>;
2167+
readonly inFlight: boolean;
2168+
} | null>(null);
2169+
const finishLinkedMessage = useCallback(() => {
2170+
if (linkedMessageId === null) return;
2171+
handledLinkedMessageIdRef.current = linkedMessageId;
2172+
setLinkedMessageLoad(null);
2173+
onLinkedMessageHandled?.();
2174+
}, [linkedMessageId, onLinkedMessageHandled]);
21582175
const handleScrollBeginDrag = useCallback(() => {
2176+
// The user takes over from a message link that is still loading or unfolding.
2177+
if (linkedMessageId !== null && handledLinkedMessageIdRef.current !== linkedMessageId) {
2178+
finishLinkedMessage();
2179+
}
21592180
clearUserScrollSettle();
21602181
userScrollSessionRef.current = true;
21612182
// Pause before the first scroll event. Otherwise a stream update can run
21622183
// maintainScrollAtEnd between touch-down and the drag leaving its threshold.
21632184
transitionEndFollow({ type: "user-scroll-begin" });
2164-
}, [clearUserScrollSettle, transitionEndFollow]);
2185+
}, [clearUserScrollSettle, finishLinkedMessage, linkedMessageId, transitionEndFollow]);
21652186
const finishUserScroll = useCallback(
21662187
(releaseIsAtEnd?: boolean) => {
21672188
clearUserScrollSettle();
@@ -2264,6 +2285,10 @@ export const ThreadFeed = memo(function ThreadFeed(props: ThreadFeedProps) {
22642285
props.latestRun,
22652286
],
22662287
);
2288+
const presentedFeedRef = useRef(presentedFeed);
2289+
useLayoutEffect(() => {
2290+
presentedFeedRef.current = presentedFeed;
2291+
}, [presentedFeed]);
22672292
const setupAnchorIndex = presentedFeed.findIndex(
22682293
(entry) => entry.type === "message" && entry.message.role === "user",
22692294
);
@@ -2292,17 +2317,6 @@ export const ThreadFeed = memo(function ThreadFeed(props: ThreadFeedProps) {
22922317
),
22932318
[presentedFeed, props.anchorMessageId, anchorTopInset],
22942319
);
2295-
// A message link opens the feed at that message, loading earlier history through it and
2296-
// expanding the turn that hides it. That is a manual navigation, so live follow stops.
2297-
const linkedMessageId = props.linkedMessageId ?? null;
2298-
const onLinkedMessageHandled = props.onLinkedMessageHandled;
2299-
const handledLinkedMessageIdRef = useRef<string | null>(null);
2300-
// The feed a load through the linked message started from. The message is missing once a
2301-
// finished load leaves the feed unchanged.
2302-
const [linkedMessageLoad, setLinkedMessageLoad] = useState<{
2303-
readonly feed: ReadonlyArray<ThreadFeedEntry>;
2304-
readonly inFlight: boolean;
2305-
} | null>(null);
23062320
useEffect(() => {
23072321
if (linkedMessageId === null) {
23082322
handledLinkedMessageIdRef.current = null;
@@ -2355,24 +2369,32 @@ export const ThreadFeed = memo(function ThreadFeed(props: ThreadFeedProps) {
23552369
}));
23562370
return;
23572371
}
2358-
handledLinkedMessageIdRef.current = linkedMessageId;
2359-
setLinkedMessageLoad(null);
2360-
onLinkedMessageHandled?.();
2372+
finishLinkedMessage();
23612373
if (step === "missing") {
23622374
Alert.alert("Message not found", "It may have been deleted from this thread.");
23632375
return;
23642376
}
23652377
const listRef = props.listRef;
23662378
const viewOffset = anchorTopInset + CHAT_LIST_ANCHOR_OFFSET;
23672379
requestAnimationFrame(() => {
2368-
void listRef.current?.scrollToIndex({ index, viewPosition: 0, viewOffset, animated: false });
2380+
// Rows can change before the frame, so find the message again.
2381+
const latestIndex = presentedFeedRef.current.findIndex(
2382+
(entry) => entry.type === "message" && entry.id === linkedMessageId,
2383+
);
2384+
if (latestIndex < 0) return;
2385+
void listRef.current?.scrollToIndex({
2386+
index: latestIndex,
2387+
viewPosition: 0,
2388+
viewOffset,
2389+
animated: false,
2390+
});
23692391
});
23702392
}, [
23712393
anchorTopInset,
23722394
expandedTurnIds,
23732395
linkedMessageId,
23742396
linkedMessageLoad,
2375-
onLinkedMessageHandled,
2397+
finishLinkedMessage,
23762398
presentedFeed,
23772399
props.activeWorkStartedAt,
23782400
props.feed,

‎apps/mobile/src/features/threads/thread-feed-message-link.test.ts‎

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,9 @@ describe("resolveLinkedMessageStep", () => {
2929
it("waits for the sync and any history load before deciding the message is older", () => {
3030
expect(resolveLinkedMessageStep({ ...absent, synced: false })).toBe("wait");
3131
expect(resolveLinkedMessageStep({ ...absent, historyLoading: true })).toBe("wait");
32+
expect(resolveLinkedMessageStep({ ...absent, presented: true, historyLoading: true })).toBe(
33+
"wait",
34+
);
3235
});
3336

3437
it("loads earlier history until it runs out, then reports the message missing", () => {

‎apps/mobile/src/features/threads/thread-feed-message-link.ts‎

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,10 +15,12 @@ export function resolveLinkedMessageStep(input: {
1515
/** Earlier history remains, and the last load through the message changed the feed. */
1616
readonly canLoadEarlier: boolean;
1717
}): "scroll" | "expand" | "wait" | "load" | "missing" {
18+
// History lands in stages, so a message shown mid-load can still move.
19+
if (input.historyLoading) return "wait";
1820
if (input.presented) return "scroll";
1921
if (input.foldedRunId !== null) return "expand";
2022
// Loaded but hidden by something other than a fold: more history cannot reveal it.
2123
if (input.inFeed) return "missing";
22-
if (!input.synced || input.historyLoading) return "wait";
24+
if (!input.synced) return "wait";
2325
return input.canLoadEarlier ? "load" : "missing";
2426
}

‎apps/web/src/components/chat/useLinkedMessageTarget.ts‎

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -111,9 +111,15 @@ export function useLinkedMessageTarget({
111111
const cancel = () => {
112112
setFinishedKey(key);
113113
const scrollNode = listRef.current?.getScrollableNode();
114-
// Supersede a queued list scroll before the user's gesture runs.
114+
// Supersede a queued list scroll before the user's gesture runs. Legend may defer this
115+
// call too, so it reads the user's position when it executes.
115116
if (scrollNode instanceof HTMLElement)
116-
void listRef.current?.scrollToOffset({ offset: scrollNode.scrollTop, animated: false });
117+
void listRef.current?.scrollToOffset({
118+
get offset() {
119+
return scrollNode.scrollTop;
120+
},
121+
animated: false,
122+
});
117123
};
118124
const onKeyDown = (event: KeyboardEvent) => {
119125
if (["ArrowUp", "ArrowDown", "PageUp", "PageDown", "Home", "End", " "].includes(event.key))

0 commit comments

Comments
 (0)