diff --git a/mobile/src/session/native-chat/input/MobileNativeChatQueuedMessages.test.tsx b/mobile/src/session/native-chat/input/MobileNativeChatQueuedMessages.test.tsx index f0c905dc385..b756a46cef6 100644 --- a/mobile/src/session/native-chat/input/MobileNativeChatQueuedMessages.test.tsx +++ b/mobile/src/session/native-chat/input/MobileNativeChatQueuedMessages.test.tsx @@ -390,6 +390,47 @@ describe('MobileNativeChatQueuedMessages', () => { expect(scrollTo).toHaveBeenCalledWith({ y: 100, animated: false }) }) + it('places a failed row by the current order after a reorder that lays nothing out', async () => { + vi.useFakeTimers() + try { + const scrollTo = vi.fn() + const mounted = create(createElement('View'), { + createNodeMock: (node) => + node.type === 'ScrollView' + ? { scrollTo, scrollToEnd: vi.fn(), flashScrollIndicators: vi.fn() } + : null + }) + renderer = mounted + const ids = (order: string) => [...order].map((messageId) => card({ messageId })) + await act(async () => mounted.update(element({ cards: ids('abcd') }))) + const scroll = mounted.root.find((node) => String(node.type) === 'ScrollView') + await act(async () => { + scroll.props.onLayout({ nativeEvent: { layout: { height: 100 } } }) + scroll.props.onContentSizeChange(0, 240) + rows(mounted).forEach((row) => + row.props.onLayout({ nativeEvent: { layout: { height: 60 } } }) + ) + }) + // Equal-height rows reordered by transform: no row lays out again. + await act(async () => mounted.update(element({ cards: ids('bcda') }))) + scrollTo.mockClear() + await act(async () => + mounted.update( + element({ + cards: [...ids('bcd'), card({ messageId: 'a', needsAttention: true })] + }) + ) + ) + await act(async () => { + vi.advanceTimersByTime(QUEUED_ROW_SHOW_SETTLE_MS) + }) + // a is now last, spanning 180–240 of a 100pt list, not first. + expect(scrollTo).toHaveBeenCalledWith({ y: 140, animated: false }) + } finally { + vi.useRealTimers() + } + }) + it('scrolls to a failure the list mounts open for, and settles when no row lays out again', async () => { vi.useFakeTimers() try { diff --git a/mobile/src/session/native-chat/input/MobileNativeChatQueuedMessages.tsx b/mobile/src/session/native-chat/input/MobileNativeChatQueuedMessages.tsx index 7160aacd18d..c8443e60077 100644 --- a/mobile/src/session/native-chat/input/MobileNativeChatQueuedMessages.tsx +++ b/mobile/src/session/native-chat/input/MobileNativeChatQueuedMessages.tsx @@ -111,7 +111,7 @@ export function MobileNativeChatQueuedMessages({ const [showRowId, setShowRowId] = useState( () => cards?.slice(1).find((card) => card.needsAttention)?.messageId ?? null ) - const measureRow = useQueuedRowShow(showRowId, setShowRowId) + const measureRow = useQueuedRowShow(showRowId, setShowRowId, liveIds) // A choice made in one placement never comes back in a later one (a new prompt folds again); // checked first, so a choice this render makes below wins. if (openChoice !== null && openChoice.abovePrompt !== abovePrompt) { diff --git a/mobile/src/session/native-chat/input/mobile-queued-row-frame.ts b/mobile/src/session/native-chat/input/mobile-queued-row-frame.ts index 21ce4d5e8ee..75e169bec58 100644 --- a/mobile/src/session/native-chat/input/mobile-queued-row-frame.ts +++ b/mobile/src/session/native-chat/input/mobile-queued-row-frame.ts @@ -35,13 +35,22 @@ type ShowRow = (row: LayoutRectangle) => void * Scrolls the queue to one row (a failure that opened the list) once that row has laid out again. * A row whose height never changes reports no layout, so the request settles shortly after either way. */ -export function useQueuedRowShow(showId: string | null, setShowId: (messageId: null) => void) { +export function useQueuedRowShow( + showId: string | null, + setShowId: (messageId: null) => void, + /** The queue's current order; a reorder moves rows by transform and lays none of them out. */ + messageIds: readonly string[] +) { const heights = useRef(new Map()) // Rows that reported since the current request, so an old height never places the row asked for. const fresh = useRef(new Set()) const lastShow = useRef(null) - // The list as its rows last laid out; only shown rows lay out, so this is what the person sees. - const latest = useRef<{ messageIds: readonly string[]; showAll: boolean } | null>(null) + // Whether the list was open when its rows last laid out; only shown rows lay out. + const shownAll = useRef(false) + const order = useRef(messageIds) + useLayoutEffect(() => { + order.current = messageIds + }) useLayoutEffect(() => { fresh.current.clear() }, [showId]) @@ -50,10 +59,7 @@ export function useQueuedRowShow(showId: string | null, setShowId: (messageId: n return } const timer = setTimeout(() => { - const current = latest.current - const frame = current?.showAll - ? queuedRowFrame(current.messageIds, heights.current, showId) - : null + const frame = shownAll.current ? queuedRowFrame(order.current, heights.current, showId) : null setShowId(null) if (frame) { lastShow.current?.(frame) @@ -67,15 +73,15 @@ export function useQueuedRowShow(showId: string | null, setShowId: (messageId: n height: number, show: ShowRow ) => { - const { messageIds, showAll } = list - latest.current = list + const { showAll } = list + shownAll.current = showAll heights.current.set(messageId, height) fresh.current.add(messageId) lastShow.current = show if (showId === null || !showAll || !fresh.current.has(showId)) { return } - const frame = queuedRowFrame(messageIds, heights.current, showId) + const frame = queuedRowFrame(list.messageIds, heights.current, showId) if (frame) { setShowId(null) show(frame)