The best way to control Claude Code, Codex and more from any computer or phone
| 1 | import { createContext, use, useState, type ReactNode } from "react"; |
| 2 | import { Platform } from "react-native"; |
| 3 | import { deriveDeviceType, isDuoDevice, type DeviceType } from "../lib/device-type"; |
| 4 | import type { NativeLayoutMetrics } from "../lib/reserved-regions"; |
| 5 | import { NativeLayoutObserver } from "./NativeLayoutObserver"; |
| 6 | |
| 7 | const IS_PAD = Platform.OS === "ios" && Platform.isPad; |
| 8 | |
| 9 | const NativeLayoutContext = createContext<NativeLayoutMetrics | null>(null); |
| 10 | // Separate from the metrics so device checks skip every resize and safe-area update. |
| 11 | const DeviceTypeContext = createContext<DeviceType>( |
| 12 | deriveDeviceType({ isPad: IS_PAD, metrics: null }), |
| 13 | ); |
| 14 | // Binaries without the native no-hinge fallback never report metrics on ordinary |
| 15 | // iPhones running iOS 27.1 or later; consumers stop waiting at this deadline. |
| 16 | export const NATIVE_METRICS_TIMEOUT_MS = 1000; |
| 17 | const NativeLayoutMetricsDeadlineContext = createContext<number>(0); |
| 18 | // Column reservations come from UIKit's layout, outside the resizing navigator. |
| 19 | export const NativeColumnLayoutMetricsContext = createContext<NativeLayoutMetrics | null>(null); |
| 20 | |
| 21 | // The latest metrics outside React, for decisions made before the first screen renders. |
| 22 | let latestMetrics: NativeLayoutMetrics | null = null; |
| 23 | const metricsWaiters = new Set<(metrics: NativeLayoutMetrics) => void>(); |
| 24 | |
| 25 | /** Resolves with the first native layout metrics, or null after `timeoutMs` without any. */ |
| 26 | export function firstNativeLayoutMetrics(timeoutMs: number) { |
| 27 | if (latestMetrics) return Promise.resolve(latestMetrics); |
| 28 | return new Promise<NativeLayoutMetrics | null>((resolve) => { |
| 29 | const settle = (metrics: NativeLayoutMetrics | null) => { |
| 30 | metricsWaiters.delete(settle); |
| 31 | clearTimeout(timeout); |
| 32 | resolve(metrics); |
| 33 | }; |
| 34 | const timeout = setTimeout(() => settle(null), timeoutMs); |
| 35 | metricsWaiters.add(settle); |
| 36 | }); |
| 37 | } |
| 38 | |
| 39 | export function currentNativeLayoutMetrics() { |
| 40 | return latestMetrics; |
| 41 | } |
| 42 | |
| 43 | export function NativeLayoutMetricsProvider(props: { readonly children: ReactNode }) { |
| 44 | const [metrics, setMetrics] = useState<NativeLayoutMetrics | null>(null); |
| 45 | const [metricsDeadline] = useState(() => Date.now() + NATIVE_METRICS_TIMEOUT_MS); |
| 46 | const publish = (next: NativeLayoutMetrics) => { |
| 47 | latestMetrics = next; |
| 48 | for (const settle of metricsWaiters) settle(next); |
| 49 | setMetrics(next); |
| 50 | }; |
| 51 | const deviceType = deriveDeviceType({ isPad: IS_PAD, metrics }); |
| 52 | return ( |
| 53 | <NativeLayoutMetricsDeadlineContext value={metricsDeadline}> |
| 54 | <NativeLayoutContext value={metrics}> |
| 55 | <DeviceTypeContext value={deviceType}> |
| 56 | <NativeLayoutObserver onChange={publish} /> |
| 57 | {props.children} |
| 58 | </DeviceTypeContext> |
| 59 | </NativeLayoutContext> |
| 60 | </NativeLayoutMetricsDeadlineContext> |
| 61 | ); |
| 62 | } |
| 63 | |
| 64 | export function useNativeLayoutMetrics() { |
| 65 | return use(NativeLayoutContext); |
| 66 | } |
| 67 | |
| 68 | /** Epoch ms after which a consumer still waiting for the first metrics gives up. */ |
| 69 | export function useNativeLayoutMetricsDeadline() { |
| 70 | return use(NativeLayoutMetricsDeadlineContext); |
| 71 | } |
| 72 | |
| 73 | export function useNativeColumnLayoutMetrics() { |
| 74 | return use(NativeColumnLayoutMetricsContext); |
| 75 | } |
| 76 | |
| 77 | export function useDeviceType() { |
| 78 | return use(DeviceTypeContext); |
| 79 | } |
| 80 | |
| 81 | /** |
| 82 | * Where the Duo's floating bottom controls end in this column. Its tab bar floats below the safe |
| 83 | * area, and the controls beside it sit on the same line. |
| 84 | */ |
| 85 | export function useDuoBottomBarInset() { |
| 86 | const deviceType = useDeviceType(); |
| 87 | const metrics = use(NativeColumnLayoutMetricsContext); |
| 88 | // Sheets have no rail to align with and can report zero for the bar region. |
| 89 | // Their controls must keep using the sheet's bottom safe area. |
| 90 | const inset = metrics?.bottomBarInset; |
| 91 | return isDuoDevice(deviceType) && inset !== undefined && inset > 0 ? inset : undefined; |
| 92 | } |