fix(mobile): float the pill above bottom bars, reopen the owner's own screen, and tell a refused start wherever you are

This commit is contained in:
Kelvin Amoaba
2026-11-09 16:56:03 +00:00
parent 2a1d2c7026
commit fcbf43189e
7 changed files with 330 additions and 185 deletions
@@ -11,6 +11,7 @@ import { themedStyleSheet } from '../../theme/themed-styles'
import { useMobileColorScheme } from '../../theme/use-mobile-color-scheme'
import { BOTTOM_DRAWER_EASING } from '../bottom-drawer/bottom-drawer-constants'
import { MobileSearchField } from '../MobileSearchField'
import { useMobileDictationClearance } from '../../dictation/mobile-dictation-host-context'
const FADE_ID = 'bottom-search-bar-fade'
const BUTTON_SIZE = 44
@@ -76,6 +77,7 @@ export function BottomSearchBar({
useEffect(() => {
onCoveredHeightChange?.(coveredHeight)
}, [coveredHeight, onCoveredHeightChange])
useMobileDictationClearance(coveredHeight)
// Close stays until the keyboard is down: iOS's late click at the tapped spot would otherwise hit the primary action that replaced it.
const searching = focused || keyboardUp || (hasSearch ?? value.trim() !== '')
+10 -3
View File
@@ -24,7 +24,11 @@ import {
} from './mobile-dictation-controls-copy'
import { dictationAwayCopy } from './mobile-dictation-away-copy'
import { composerInputStyles } from '../components/composer-input/composer-input-styles'
import { useMobileDictationHost, type MobileDictationHostToast } from './mobile-dictation-host'
import {
useMobileDictationHost,
useMobileDictationHostLive,
type MobileDictationHostToast
} from './mobile-dictation-host-context'
const TOAST_MS = 4000
// Long enough to read the target's name and reach Open.
@@ -188,8 +192,11 @@ export function MobileDictationPillLayer(): React.JSX.Element {
useMobileColorScheme()
useMobileTranslation()
const insets = useSafeAreaInsets()
const { dictation, stop, pill } = useMobileDictationHost()
const pillBottom = insets.bottom + spacing.md
const host = useMobileDictationHost()
const dictation = useMobileDictationHostLive(host)
const { stop, pill } = host
// Above the home indicator, or above a screen's bottom bar when it reported one.
const pillBottom = Math.max(insets.bottom + spacing.md, pill.clearance + spacing.sm)
return (
<>
{pill.away ? (
@@ -0,0 +1,95 @@
import { createContext, useContext, useEffect, useState, useSyncExternalStore } from 'react'
import type { UseMobileDictationResult } from '../hooks/mobile-dictation-session-state'
import type { RpcClient } from '../transport/rpc/rpc-client-contract'
import type { MobileDictationAwayTarget } from './mobile-dictation-away-insert'
export type MobileDictationHostToast = { message: string; open?: () => void }
// Who started the dictation and where its words go, fixed at the tap: leaving the screen never moves them.
export type MobileDictationOwner = {
// The starting screen instance, and the session it shows (a reopened screen of that session takes over its controls).
readonly instance: object
readonly sessionKey: string
readonly client: RpcClient | null
readonly target: MobileDictationAwayTarget
// The target tab's title, for the pill and its toasts.
readonly title: string
// The session route that opens the target tab again when its screen is gone.
readonly href: string
// The target tab's pane key, for a screen still in the stack to switch back to it.
readonly paneKey: string | undefined
readonly dictationMode: string | undefined
readonly removeFillerWords: boolean
}
// A mounted session screen, as the dictation host sees it.
export type MobileDictationScreen = {
readonly instance: object
readonly sessionKey: string
// The one "shown" rule (mobile-dictation-shown.ts): true while the in-place controls stand for the dictation.
readonly shown: () => boolean
// The starting screen inserts where it started (cursor, live input); false when it cannot, so the host inserts instead.
readonly insert: (text: string) => boolean
// Setup refusals open its setup sheet; other failures toast on it.
readonly reportFailure: (err: Error) => void
readonly toast: (message: string) => void
// Pop the screens above this one and show the target tab.
readonly bringToFront: (paneKey: string | undefined) => void
}
// Stable across the dictation's ticks: screens read the live dictation through subscribe, so a word heard re-renders only who shows it.
export type MobileDictationHost = {
readonly owner: MobileDictationOwner | null
readonly start: (owner: MobileDictationOwner) => Promise<void>
// Stop with the selection tick, as every stop button does.
readonly stop: () => void
readonly attach: (screen: MobileDictationScreen) => () => void
// A screen's shown() answer changed; the pill reads it on render.
readonly refresh: () => void
readonly subscribe: (listener: () => void) => () => void
readonly getDictation: () => UseMobileDictationResult
// The same dictation as nobody's: no status, words or failure; its identity holds until the meter or callbacks change.
readonly getIdleDictation: () => UseMobileDictationResult
// Starting, recording, finishing, or a failure kept for Retry.
readonly getUnderway: () => boolean
// The floating pill: shown while the dictation's own screen is not, with the host's toast.
readonly pill: {
readonly away: boolean
readonly title: string
readonly open: () => void
readonly toast: MobileDictationHostToast | null
readonly clearToast: () => void
// The tallest bottom bar a screen reported, so the pill floats above it.
readonly clearance: number
}
}
export const MobileDictationHostContext = createContext<MobileDictationHost | null>(null)
export function useMobileDictationHost(): MobileDictationHost {
const host = useContext(MobileDictationHostContext)
if (!host) {
throw new Error('useMobileDictationHost needs a MobileDictationHostProvider')
}
return host
}
// The live dictation, re-rendering on each of its changes.
export function useMobileDictationHostLive(host: MobileDictationHost): UseMobileDictationResult {
return useSyncExternalStore(host.subscribe, host.getDictation)
}
// Bottom bars report the height they cover, so the pill floats above them rather than over their buttons.
export const MobileDictationClearanceContext = createContext<
((id: object, height: number) => void) | null
>(null)
// A bottom bar's covered height, cleared when it unmounts; a hidden screen's bar lays out at zero.
export function useMobileDictationClearance(height: number): void {
const setClearance = useContext(MobileDictationClearanceContext)
const [id] = useState(() => ({}))
useEffect(() => {
setClearance?.(id, height)
}, [height, id, setClearance])
useEffect(() => () => setClearance?.(id, 0), [id, setClearance])
}
+160 -120
View File
@@ -1,102 +1,56 @@
import {
createContext,
useCallback,
useContext,
useMemo,
useRef,
useState,
type ReactNode
} from 'react'
import { useCallback, useLayoutEffect, useMemo, useRef, useState, type ReactNode } from 'react'
import { useMobileDictation } from '../hooks/use-mobile-dictation'
import type { UseMobileDictationResult } from '../hooks/mobile-dictation-session-state'
import type { RpcClient } from '../transport/rpc/rpc-client-contract'
import { useRouteHandoff } from '../navigation/route-handoff'
import { triggerError } from '../platform/haptics'
import { useClipboardWriter } from '../platform/clipboard'
import { dictationFailureMessage } from './mobile-dictation-setup'
import { useMobileDictationHaptics } from './use-mobile-dictation-haptics'
import { useMobileDictationSilenceStop } from './use-mobile-dictation-silence-stop'
import {
insertMobileDictationAway,
type MobileDictationAwayTarget
} from './mobile-dictation-away-insert'
import { insertMobileDictationAway } from './mobile-dictation-away-insert'
import { nativeChatDictationCopy } from './mobile-session-native-chat-dictation-copy'
import { dictationAwayCopy } from './mobile-dictation-away-copy'
import {
MobileDictationClearanceContext,
MobileDictationHostContext,
type MobileDictationHost,
type MobileDictationHostToast,
type MobileDictationOwner,
type MobileDictationScreen
} from './mobile-dictation-host-context'
export type MobileDictationHostToast = { message: string; open?: () => void }
// Who started the dictation and where its words go, fixed at the tap: leaving the screen never moves them.
export type MobileDictationOwner = {
// The starting screen instance, and the session it shows (a reopened screen of that session takes over its controls).
readonly instance: object
readonly sessionKey: string
readonly client: RpcClient | null
readonly target: MobileDictationAwayTarget
// The target tab's title, for the pill and its toasts.
readonly title: string
// The session route that opens the target tab again.
readonly href: string
readonly dictationMode: string | undefined
readonly removeFillerWords: boolean
}
// A mounted session screen, as the dictation host sees it.
export type MobileDictationScreen = {
readonly instance: object
readonly sessionKey: string
// The one "shown" rule (mobile-dictation-shown.ts): true while the in-place controls stand for the dictation.
readonly shown: () => boolean
// The starting screen inserts where it started (cursor, live input); false when it cannot, so the host inserts instead.
readonly insert: (text: string) => boolean
// Setup refusals open its setup sheet; other failures toast on it.
readonly reportFailure: (err: Error) => void
readonly toast: (message: string) => void
}
type MobileDictationHost = {
readonly dictation: UseMobileDictationResult
readonly owner: MobileDictationOwner | null
readonly start: (owner: MobileDictationOwner) => Promise<void>
// Stop with the selection tick, as every stop button does.
readonly stop: () => void
readonly attach: (screen: MobileDictationScreen) => () => void
// A screen's shown() answer changed; the pill reads it on render.
readonly refresh: () => void
// The floating pill's state: the dictation when its screen is not shown, and the host's toast.
readonly pill: {
readonly away: boolean
readonly title: string
readonly open: () => void
readonly toast: MobileDictationHostToast | null
readonly clearToast: () => void
}
}
const MobileDictationHostContext = createContext<MobileDictationHost | null>(null)
export function useMobileDictationHost(): MobileDictationHost {
const host = useContext(MobileDictationHostContext)
if (!host) {
throw new Error('useMobileDictationHost needs a MobileDictationHostProvider')
}
return host
}
export {
useMobileDictationHost,
type MobileDictationOwner,
type MobileDictationScreen
} from './mobile-dictation-host-context'
// The owner's screen: the one in front when two show its session, else the first; startingOnly asks for the screen that started it.
function screenFor(
screens: ReadonlySet<MobileDictationScreen>,
owner: MobileDictationOwner | null,
startingOnly: boolean
): MobileDictationScreen | null {
let found: MobileDictationScreen | null = null
for (const screen of screens) {
if (
owner &&
screen.sessionKey === owner.sessionKey &&
(!startingOnly || screen.instance === owner.instance)
) {
if (!owner || screen.sessionKey !== owner.sessionKey) {
continue
}
if (startingOnly ? screen.instance === owner.instance : screen.shown()) {
return screen
}
found ??= startingOnly ? null : screen
}
return null
return found
}
function isUnderway(dictation: UseMobileDictationResult): boolean {
return (
dictation.isStarting ||
dictation.isRecording ||
dictation.isProcessing ||
dictation.failure !== null
)
}
// One dictation per host document, above its screens, so it keeps going when its session leaves the screen.
@@ -109,30 +63,45 @@ export function MobileDictationHostProvider({ children }: { children: ReactNode
const refresh = useCallback(() => setRevision((revision) => revision + 1), [])
const [toast, setToast] = useState<MobileDictationHostToast | null>(null)
const clearToast = useCallback(() => setToast(null), [])
const [clearances, setClearances] = useState<ReadonlyMap<object, number>>(new Map())
const clipboard = useClipboardWriter()
// Bring the target back: pop to its session when that screen is still in the stack, otherwise open it again.
// Bring a dictation's target back: its screen if one is still mounted, otherwise open the session again.
const openOwner = useCallback(
(target: MobileDictationOwner) => {
const screen = screenFor(screensRef.current, target, false)
if (screen) {
screen.bringToFront(target.paneKey)
} else {
router.push(target.href)
}
},
[router]
)
const open = useCallback(() => {
const current = ownerRef.current
if (!current) {
return
if (ownerRef.current) {
openOwner(ownerRef.current)
}
if (screenFor(screensRef.current, current, false)) {
router.dismissTo(current.href)
} else {
router.push(current.href)
}
}, [router])
}, [openOwner])
// Tell the user where words went: on the screen when it shows the dictation, else the host's toast with Open.
const say = (message: string, withOpen: boolean) => {
// Say it on the screen that shows the dictation, else in the host's toast (with Open for the dictation it is about).
const say = (message: string, about?: MobileDictationOwner) => {
const screen = screenFor(screensRef.current, ownerRef.current, false)
if (screen?.shown()) {
screen.toast(message)
} else {
setToast(withOpen ? { message, open } : { message })
setToast(about ? { message, open: () => openOwner(about) } : { message })
}
}
const reportFailureRef = useRef((err: Error) => {
const screen = screenFor(screensRef.current, ownerRef.current, false)
if (screen?.shown()) {
screen.reportFailure(err)
return
}
triggerError()
setToast({ message: dictationFailureMessage(err.message) })
})
const dictation = useMobileDictation({
client: owner?.client ?? null,
@@ -149,42 +118,78 @@ export function MobileDictationHostProvider({ children }: { children: ReactNode
}
void insertMobileDictationAway(current, text).then((inserted) => {
if (inserted) {
say(dictationAwayCopy.insertedIn(current), true)
say(dictationAwayCopy.insertedIn(current), current)
return
}
triggerError()
void clipboard.writeText(text).then(
() => say(nativeChatDictationCopy.insertFailedCopied(), false),
() => say(nativeChatDictationCopy.insertFailed(), false)
() => say(nativeChatDictationCopy.insertFailedCopied()),
() => say(nativeChatDictationCopy.insertFailed())
)
})
},
onError: (err, { kept }) => {
if (kept) {
triggerError()
return
} else {
reportFailureRef.current(err)
}
const screen = screenFor(screensRef.current, ownerRef.current, false)
if (screen?.shown()) {
screen.reportFailure(err)
return
}
triggerError()
setToast({ message: dictationFailureMessage(err.message) })
},
onAudioLimitReached: () => say(nativeChatDictationCopy.stoppedAtCloudLimit(), false)
onAudioLimitReached: () => say(nativeChatDictationCopy.stoppedAtCloudLimit())
})
const stop = useMobileDictationHaptics(dictation)
useMobileDictationSilenceStop(dictation, owner?.dictationMode, stop, (message) =>
say(message, false)
useMobileDictationSilenceStop(dictation, owner?.dictationMode, stop, (message) => say(message))
// Screens subscribe to the live dictation; the host's context only changes when its owner or pill does.
const liveRef = useRef(dictation)
const listenersRef = useRef(new Set<() => void>())
const { meter, start: startDictation, stop: stopDictation, cancel, retry } = dictation
const idle = useMemo<UseMobileDictationResult>(
() => ({
status: 'idle',
isStarting: false,
isRecording: false,
isProcessing: false,
transcriptSoFar: '',
error: null,
failure: null,
engine: null,
meter,
start: startDictation,
stop: stopDictation,
cancel,
retry
}),
[cancel, meter, retry, startDictation, stopDictation]
)
const idleRef = useRef(idle)
useLayoutEffect(() => {
liveRef.current = dictation
idleRef.current = idle
for (const listener of listenersRef.current) {
listener()
}
})
const subscribe = useCallback((listener: () => void) => {
listenersRef.current.add(listener)
return () => {
listenersRef.current.delete(listener)
}
}, [])
const getDictation = useCallback(() => liveRef.current, [])
const getIdleDictation = useCallback(() => idleRef.current, [])
const getUnderway = useCallback(() => isUnderway(liveRef.current), [])
const { start: startDictation } = dictation
const start = useCallback(
async (next: MobileDictationOwner) => {
ownerRef.current = next
setOwner(next)
await startDictation(next.client)
try {
await startDictation(next.client)
} catch (err) {
// The starting screen may already be gone, so a refused start is told wherever the user is now.
reportFailureRef.current(err instanceof Error ? err : new Error(String(err)))
}
},
[startDictation]
)
@@ -200,25 +205,60 @@ export function MobileDictationHostProvider({ children }: { children: ReactNode
},
[refresh]
)
const setClearance = useCallback((id: object, height: number) => {
setClearances((current) => {
if ((current.get(id) ?? 0) === height) {
return current
}
const next = new Map(current)
if (height > 0) {
next.set(id, height)
} else {
next.delete(id)
}
return next
})
}, [])
const underway =
dictation.isStarting ||
dictation.isRecording ||
dictation.isProcessing ||
dictation.failure !== null
const away = owner !== null && underway && !screenFor(screensRef.current, owner, false)?.shown()
const away =
owner !== null && isUnderway(dictation) && !screenFor(screensRef.current, owner, false)?.shown()
const title = owner?.title ?? ''
const clearance = Math.max(0, ...clearances.values())
const pill = useMemo(
() => ({ away, title, open, toast, clearToast }),
[away, clearToast, open, title, toast]
() => ({ away, title, open, toast, clearToast, clearance }),
[away, clearToast, clearance, open, title, toast]
)
const value = useMemo<MobileDictationHost>(
() => ({ dictation, owner, start, stop, attach, refresh, pill }),
[attach, dictation, owner, pill, refresh, start, stop]
() => ({
owner,
start,
stop,
attach,
refresh,
subscribe,
getDictation,
getIdleDictation,
getUnderway,
pill
}),
[
attach,
getDictation,
getIdleDictation,
getUnderway,
owner,
pill,
refresh,
start,
stop,
subscribe
]
)
return (
<MobileDictationHostContext.Provider value={value}>
{children}
</MobileDictationHostContext.Provider>
<MobileDictationClearanceContext.Provider value={setClearance}>
<MobileDictationHostContext.Provider value={value}>
{children}
</MobileDictationHostContext.Provider>
</MobileDictationClearanceContext.Provider>
)
}
@@ -11,8 +11,9 @@ export function useMobileDictationHaptics(dictation: UseMobileDictationResult):
}
}, [dictation.isRecording])
const { stop } = dictation
return useCallback(() => {
triggerSelection()
void dictation.stop()
}, [dictation])
void stop()
}, [stop])
}
@@ -47,7 +47,7 @@ vi.mock('expo-keep-awake', () => ({
}))
vi.mock('expo-router', () => {
const router = { canGoBack: () => false, push: () => {}, dismissTo: () => {} }
return { useFocusEffect: () => {}, useRouter: () => router }
return { useFocusEffect: () => {}, useRouter: () => router, useNavigation: () => undefined }
})
vi.mock('../platform/haptics', () => ({
triggerError: () => {
@@ -367,7 +367,7 @@ describe('a dictation whose session screen was left', () => {
const rpc = createFakeRpcClient()
const mounted = await startRecording(rpc)
mounted.leave()
expect(mounted.host().dictation.isRecording).toBe(true)
expect(mounted.host().getDictation().isRecording).toBe(true)
expect(mounted.host().pill.away).toBe(true)
act(() => mounted.host().stop())
await settle(rpc, 'ship it')
@@ -1,7 +1,14 @@
import { useCallback, useEffect, useLayoutEffect, useMemo, useRef, useState } from 'react'
import { useFocusEffect } from 'expo-router'
import {
useCallback,
useEffect,
useLayoutEffect,
useRef,
useState,
useSyncExternalStore
} from 'react'
import { useFocusEffect, useNavigation } from 'expo-router'
import { HOST_SESSION_ROUTE } from '../navigation/host-stack-navigation'
import { useMobileSessionDictationSetup } from './use-mobile-session-dictation-setup'
import type { UseMobileDictationResult } from '../hooks/mobile-dictation-session-state'
import { triggerError } from '../platform/haptics'
import { useClipboardWriter } from '../platform/clipboard'
import {
@@ -24,30 +31,6 @@ import { mobileSessionTabPaneKey } from './tabs/notification-pane-tab'
import type { MobileSessionFeedbackCapabilitiesModel } from './use-mobile-session-feedback-capabilities'
import type { useMobileNativeChatController } from './native-chat/use-mobile-native-chat-controller'
// What a session screen's controls see when the dictation belongs to another screen, or this one is behind another.
function idleView(dictation: UseMobileDictationResult): UseMobileDictationResult {
return {
...dictation,
status: 'idle',
isStarting: false,
isRecording: false,
isProcessing: false,
transcriptSoFar: '',
error: null,
failure: null,
engine: null
}
}
function isUnderway(dictation: UseMobileDictationResult): boolean {
return (
dictation.isStarting ||
dictation.isRecording ||
dictation.isProcessing ||
dictation.failure !== null
)
}
// The session screen's side of the page's one dictation: start it here, show it in place while this screen is in front, and insert where it started.
export function useMobileSessionDictation(
scope: MobileSessionFeedbackCapabilitiesModel,
@@ -85,9 +68,14 @@ export function useMobileSessionDictation(
ownedHere: host.owner?.sessionKey === sessionKey,
inFront
})
const raw = host.dictation
const dictation = useMemo(() => (shown ? raw : idleView(raw)), [raw, shown])
const busyElsewhere = !shown && isUnderway(raw)
// Only the screen showing the dictation re-renders on its every word; the rest see one idle object and whether it is busy.
const dictation = useSyncExternalStore(
host.subscribe,
shown ? host.getDictation : host.getIdleDictation
)
const busyElsewhere = useSyncExternalStore(host.subscribe, host.getUnderway) && !shown
// The screen's own navigation: bringing it back pops what was pushed over it, never another session's screen.
const navigation = useNavigation()
// Share dictation error handling for start rejection and live errors so disabled host setup always opens the setup sheet.
const reportDictationFailure = useCallback(
@@ -143,6 +131,10 @@ export function useMobileSessionDictation(
return true
}
const navigationRef = useRef(navigation)
useLayoutEffect(() => {
navigationRef.current = navigation
})
const screenRef = useRef({
shown,
insert: insertHere,
@@ -169,7 +161,20 @@ export function useMobileSessionDictation(
dictationRouteContextRef.current = null
screenRef.current.reportFailure(err)
},
toast: (message) => screenRef.current.showToast(message)
toast: (message) => screenRef.current.showToast(message),
bringToFront: (paneKey) => {
const current = navigationRef.current
// From this screen POP_TO starts at this route (the dispatch's source), so it stops here; merge keeps its params and swaps the pane.
current?.dispatch({
type: 'POP_TO',
payload: {
name: HOST_SESSION_ROUTE,
params: paneKey ? { paneKey } : undefined,
merge: true
},
target: current.getState()?.key
})
}
}),
[attach, dictationRouteContextRef, instance, sessionKey]
)
@@ -209,31 +214,26 @@ export function useMobileSessionDictation(
dictationRouteContextRef.current = routeContext
const target = destination.captureDestination(showNativeChat, activeHandle)
const paneKey = mobileSessionTabPaneKey(activeSessionTab)
void host
.start({
instance,
sessionKey,
client,
target:
target?.field === 'chat'
? target.scope === null
? null
: { field: 'chat', scope: target.scope }
: target,
title: activeSessionTab?.title ?? '',
href: workspaceRouteHref(
`/h/${encodeURIComponent(hostId)}/session/${encodeURIComponent(worktreeId)}${paneKey ? `?paneKey=${encodeURIComponent(paneKey)}` : ''}`,
executionHost
),
dictationMode: scope.dictationMode,
removeFillerWords
})
.catch((err) => {
if (dictationRouteContextRef.current === routeContext) {
dictationRouteContextRef.current = null
}
reportDictationFailure(err)
})
// A refused start comes back through this screen's reportFailure while it is shown, or the host's toast.
void host.start({
instance,
sessionKey,
client,
target:
target?.field === 'chat'
? target.scope === null
? null
: { field: 'chat', scope: target.scope }
: target,
title: activeSessionTab?.title ?? '',
paneKey,
href: workspaceRouteHref(
`/h/${encodeURIComponent(hostId)}/session/${encodeURIComponent(worktreeId)}${paneKey ? `?paneKey=${encodeURIComponent(paneKey)}` : ''}`,
executionHost
),
dictationMode: scope.dictationMode,
removeFillerWords
})
}, [
activeHandle,
activeSessionTab,
@@ -259,8 +259,8 @@ export function useMobileSessionDictation(
const stopDictation = host.stop
const cancelDictation = useCallback(() => {
dictationRouteContextRef.current = null
void raw.cancel()
}, [dictationRouteContextRef, raw])
void dictation.cancel()
}, [dictationRouteContextRef, dictation])
const handleDictationToggle = useCallback(() => {
if (dictation.isProcessing) {