fix(mobile): move native-chat latest-value ref writes out of render

Render-time ref writes can leak from discarded render work (react-freeze
makes discarded renders reachable on mobile) and fail the blocking
React Doctor changed-lines CI gate. Use the layout-effect idiom already
established in use-mobile-native-chat-active-resolution.

Claude-Session: https://claude.ai/code/session_01CNFkBiVT51tM6UA5i2rcaK
This commit is contained in:
Merge Sim
2026-09-01 13:43:18 -07:00
parent b4738619ac
commit 0a4cbb7481
2 changed files with 8 additions and 4 deletions
@@ -1,4 +1,4 @@
import { useCallback, useRef, useState } from 'react'
import { useCallback, useLayoutEffect, useRef, useState } from 'react'
import { CLIPBOARD_IMAGE_TOO_LARGE_ERROR } from '../../../src/shared/clipboard-image'
import type { RpcClient } from '../transport/rpc-client'
import type { ConnectionState } from '../transport/types'
@@ -46,7 +46,9 @@ export function useMobileNativeChatImageUpload(args: {
const [isAttaching, setIsAttaching] = useState(false)
const attachingCount = useRef(0)
const connStateRef = useRef(connState)
connStateRef.current = connState
useLayoutEffect(() => {
connStateRef.current = connState
}, [connState])
const attachImage = useCallback(
async (source: MobileImageSource): Promise<void> => {
@@ -1,4 +1,4 @@
import { useCallback, useEffect, useRef, useState } from 'react'
import { useCallback, useEffect, useLayoutEffect, useRef, useState } from 'react'
import type {
AgentSessionHistoryResult,
AgentSessionSubscribeEvent
@@ -37,7 +37,9 @@ export function useMobileStructuredAgentState(args: {
const [state, setState] = useState<StructuredAgentSessionState>(EMPTY_STRUCTURED_AGENT_SESSION)
const [loadingOlder, setLoadingOlder] = useState(false)
const stateRef = useRef(state)
stateRef.current = state
useLayoutEffect(() => {
stateRef.current = state
}, [state])
const apply = useCallback((action: StructuredAgentSessionAction) => {
setState((current) => reduceStructuredAgentSession(current, action))