fix(mobile): place a failed queued row by the queue's current order

A reorder of equal-height rows moves them by transform and lays none of
them out, so the failure scroll's settle path kept the order from the last
layout and could scroll to the wrong row. The hook now takes the queue's
current order every render.

Follow-up to #41611 (Neil post-merge C1, F41611-C1-1).
This commit is contained in:
Jinjing
2026-11-12 11:27:05 -08:00
parent d1ae177242
commit befdd0d6ae
3 changed files with 58 additions and 11 deletions
@@ -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 {
@@ -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) {
@@ -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<string, number>())
// Rows that reported since the current request, so an old height never places the row asked for.
const fresh = useRef(new Set<string>())
const lastShow = useRef<ShowRow | null>(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)