diff --git a/apps/mobile/src/ui/chat/conversation-screen.tsx b/apps/mobile/src/ui/chat/conversation-screen.tsx index 07100509ed..9098d88a4b 100644 --- a/apps/mobile/src/ui/chat/conversation-screen.tsx +++ b/apps/mobile/src/ui/chat/conversation-screen.tsx @@ -1112,7 +1112,9 @@ function ChatConversation({ 100, ); }} - {...(latest.ready ? { onStartReached: () => void conversation?.loadOlder() } : {})} + {...(latest.ready && latest.browsed + ? { onStartReached: () => void conversation?.loadOlder() } + : {})} onStartReachedThreshold={0.5} {...latest.listProps} onScrollBeginDrag={(event: ScrollEvent) => { diff --git a/apps/mobile/src/ui/chat/follow-latest.ts b/apps/mobile/src/ui/chat/follow-latest.ts index 92b3301018..b88d668368 100644 --- a/apps/mobile/src/ui/chat/follow-latest.ts +++ b/apps/mobile/src/ui/chat/follow-latest.ts @@ -9,7 +9,8 @@ import type { import type { ConversationMessage } from '../../host/conversation'; export const LATEST_SLOP = 40; -export const SETTLE_FRAMES = 12; +export const SETTLE_FRAMES = 45; +export const STABLE_FRAMES = 6; export type ListMetrics = { content: number; layout: number; offset: number }; @@ -69,6 +70,7 @@ export function useFollowLatest( const [away, setAway] = useState(false); const [anchor, setAnchor] = useState(); const [ready, setReady] = useState(false); + const [browsed, setBrowsed] = useState(false); if (!ready && empty) setReady(true); const followingNow = useRef(true); const dragging = useRef(false); @@ -131,14 +133,17 @@ export function useFollowLatest( }, [onFollowingChange]); const confirm = useCallback((version: number) => { - requestAnimationFrame(() => { - if (settled.current) return; - attempts.current += 1; - if ((version === sizes.current && hasRows.current) || attempts.current >= SETTLE_FRAMES) { - settled.current = true; - setReady(true); - } - }); + const check = (stable: number) => + requestAnimationFrame(() => { + if (settled.current) return; + attempts.current += 1; + const still = version === sizes.current && hasRows.current; + if ((still && stable + 1 >= STABLE_FRAMES) || attempts.current >= SETTLE_FRAMES) { + settled.current = true; + setReady(true); + } else if (still) check(stable + 1); + }); + check(0); }, []); const pin = useCallback(() => { @@ -170,6 +175,7 @@ export function useFollowLatest( const onScrollBeginDrag = useCallback(() => { dragging.current = true; + setBrowsed(true); hold(); }, [hold]); @@ -217,6 +223,7 @@ export function useFollowLatest( return { away, ready, + browsed, unseen: following ? 0 : unseenSince(messages, anchor), hold, jumpToLatest, diff --git a/apps/mobile/test/follow-latest-gestures.test.tsx b/apps/mobile/test/follow-latest-gestures.test.tsx index 20a89ec7e8..e271d00766 100644 --- a/apps/mobile/test/follow-latest-gestures.test.tsx +++ b/apps/mobile/test/follow-latest-gestures.test.tsx @@ -10,7 +10,7 @@ import type { import { afterEach, beforeEach, describe, expect, it, vi } from 'vitest'; import type { ConversationMessage } from '../src/host/conversation'; -import { SETTLE_FRAMES, useFollowLatest } from '../src/ui/chat/follow-latest'; +import { SETTLE_FRAMES, STABLE_FRAMES, useFollowLatest } from '../src/ui/chat/follow-latest'; (globalThis as { IS_REACT_ACT_ENVIRONMENT?: boolean }).IS_REACT_ACT_ENVIRONMENT = true; @@ -33,7 +33,7 @@ const layout = (height: number) => ({ nativeEvent: { layout: { height } } }) as let frames: Array<() => void> = []; const flushFrames = () => act(() => { - for (let round = 0; round < 4 && frames.length > 0; round += 1) + for (let round = 0; round < 64 && frames.length > 0; round += 1) for (const callback of frames.splice(0)) callback(); }); @@ -240,6 +240,32 @@ describe('following a growing conversation', () => { view.unmount(); }); + it('stays hidden while rows keep rendering in batches a few frames apart', () => { + const view = mount([message('a', 'bot')]); + act(() => view.current.listProps.onLayout(layout(LAYOUT))); + act(() => view.current.listProps.onContentSizeChange(0, CONTENT)); + for (let frame = 0; frame < STABLE_FRAMES - 1; frame += 1) act(() => frames.shift()!()); + expect(view.current.ready).toBe(false); + act(() => view.current.listProps.onContentSizeChange(0, CONTENT + 400)); + for (let frame = 0; frame < STABLE_FRAMES - 1; frame += 1) act(() => frames.shift()!()); + expect(view.current.ready).toBe(false); + flushFrames(); + expect(view.scrollToOffset).toHaveBeenLastCalledWith({ offset: END + 400, animated: false }); + expect(view.current.ready).toBe(true); + view.unmount(); + }); + + it('loads older history only after the Human has dragged the list', () => { + const view = mount([message('a', 'bot')]); + act(() => view.current.listProps.onLayout(layout(LAYOUT))); + act(() => view.current.listProps.onContentSizeChange(0, CONTENT)); + flushFrames(); + expect(view.current.browsed).toBe(false); + act(() => view.current.listProps.onScrollBeginDrag()); + expect(view.current.browsed).toBe(true); + view.unmount(); + }); + it('gives up waiting for stable content after a bounded number of frames', () => { const view = mount([]); act(() => view.current.listProps.onLayout(layout(LAYOUT))); diff --git a/changes/1845-open-chat-settle.md b/changes/1845-open-chat-settle.md new file mode 100644 index 0000000000..87ceeaa121 --- /dev/null +++ b/changes/1845-open-chat-settle.md @@ -0,0 +1,11 @@ +--- +section: Fixed +--- + +## en + +- Opening a chat on the phone now shows it already at the latest message. It no longer scrolls visibly down to the bottom while the messages finish laying out, and older history only loads once you scroll up yourself ([#1845](https://github.com/BotHarness/DeepSeekBot/issues/1845)). + +## zh + +- 在手机上打开聊天时,直接停在最新消息,不会再在消息排版过程中看到它一路往下滚到底部;更早的历史只在你自己往上滑时才加载([#1845](https://github.com/BotHarness/DeepSeekBot/issues/1845))。