Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 3 additions & 1 deletion apps/mobile/src/ui/chat/conversation-screen.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -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) => {
Expand Down
25 changes: 16 additions & 9 deletions apps/mobile/src/ui/chat/follow-latest.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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 };

Expand Down Expand Up @@ -69,6 +70,7 @@ export function useFollowLatest<Row>(
const [away, setAway] = useState(false);
const [anchor, setAnchor] = useState<string | undefined>();
const [ready, setReady] = useState(false);
const [browsed, setBrowsed] = useState(false);
if (!ready && empty) setReady(true);
const followingNow = useRef(true);
const dragging = useRef(false);
Expand Down Expand Up @@ -131,14 +133,17 @@ export function useFollowLatest<Row>(
}, [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(() => {
Expand Down Expand Up @@ -170,6 +175,7 @@ export function useFollowLatest<Row>(

const onScrollBeginDrag = useCallback(() => {
dragging.current = true;
setBrowsed(true);
hold();
}, [hold]);

Expand Down Expand Up @@ -217,6 +223,7 @@ export function useFollowLatest<Row>(
return {
away,
ready,
browsed,
unseen: following ? 0 : unseenSince(messages, anchor),
hold,
jumpToLatest,
Expand Down
30 changes: 28 additions & 2 deletions apps/mobile/test/follow-latest-gestures.test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand All @@ -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();
});

Expand Down Expand Up @@ -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)));
Expand Down
11 changes: 11 additions & 0 deletions changes/1845-open-chat-settle.md
Original file line number Diff line number Diff line change
@@ -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))。
Loading