99import { useIsFocused , useNavigation , type StaticScreenProps } from "@react-navigation/native" ;
1010import { EnvironmentId , ThreadId } from "@t3tools/contracts" ;
1111import * as Cause from "effect/Cause" ;
12- import { useEffect , useRef , useState , type RefObject } from "react" ;
12+ import { useCallback , useEffect , useRef , useState , type RefObject } from "react" ;
1313import * as Haptics from "expo-haptics" ;
1414import { Accelerometer } from "expo-sensors" ;
1515import { AppState , BackHandler , Pressable , StatusBar , View } from "react-native" ;
@@ -47,6 +47,7 @@ const OVERLAY_EXITING = FadeOut.duration(120).reduceMotion(ReduceMotion.System);
4747type DevicePreviewRouteScreenProps = StaticScreenProps < {
4848 readonly environmentId : string ;
4949 readonly threadId : string ;
50+ readonly deviceKey ?: string ;
5051} > ;
5152
5253export function DevicePreviewRouteScreen ( { route } : DevicePreviewRouteScreenProps ) {
@@ -62,27 +63,33 @@ export function DevicePreviewRouteScreen({ route }: DevicePreviewRouteScreenProp
6263 < DevicePreviewScreen
6364 environmentId = { EnvironmentId . make ( environmentId ) }
6465 threadId = { ThreadId . make ( threadId ) }
66+ initialDeviceKey = { route . params . deviceKey }
6567 onClose = { onClose }
6668 />
6769 ) ;
6870}
6971
70- function DevicePreviewScreen ( {
72+ export function DevicePreviewScreen ( {
7173 environmentId,
7274 threadId,
7375 onClose,
76+ onMaximize,
77+ initialDeviceKey,
7478} : {
7579 readonly environmentId : EnvironmentId ;
7680 readonly threadId : ThreadId ;
7781 readonly onClose : ( ) => void ;
82+ readonly onMaximize ?: ( deviceKey : string | null ) => void ;
83+ readonly initialDeviceKey ?: string ;
7884} ) {
85+ const embedded = onMaximize !== undefined ;
7986 const insets = useSafeAreaInsets ( ) ;
8087 // NativeColumnContent already insets the sides, so only a book fold moves the reveal handle.
8188 const handleBounds = deriveBottomControlInsets ( useNativeColumnLayoutMetrics ( ) ) ;
8289 const { themeVariables } = useAppearancePreferences ( ) ;
8390 const focused = useIsFocused ( ) ;
8491 const [ foreground , setForeground ] = useState ( AppState . currentState !== "background" ) ;
85- const [ selectedKey , setSelectedKey ] = useState < string | null > ( null ) ;
92+ const [ selectedKey , setSelectedKey ] = useState < string | null > ( initialDeviceKey ?? null ) ;
8693 const [ inputConnected , setInputConnected ] = useState ( false ) ;
8794 const [ streamAttempt , setStreamAttempt ] = useState ( 0 ) ;
8895 const [ shuttingDown , setShuttingDown ] = useState ( false ) ;
@@ -93,10 +100,13 @@ function DevicePreviewScreen({
93100 const state = useEnvironmentQuery ( deviceEnvironment . state ( { environmentId, input : { } } ) ) ;
94101 const previews = threadDevicePreviews ( state . data , threadId ) ;
95102 const preview = selectedThreadDevicePreview ( previews , selectedKey ) ;
96- const onInputConnected = async ( connected : boolean ) => {
97- setInputConnected ( connected ) ;
98- if ( connected ) setControlsVisible ( false ) ;
99- } ;
103+ const onInputConnected = useCallback (
104+ async ( connected : boolean ) => {
105+ setInputConnected ( connected ) ;
106+ if ( connected && ! embedded ) setControlsVisible ( false ) ;
107+ } ,
108+ [ embedded ] ,
109+ ) ;
100110 useEffect ( ( ) => {
101111 const subscription = AppState . addEventListener ( "change" , ( state ) =>
102112 setForeground ( state !== "background" ) ,
@@ -107,7 +117,7 @@ function DevicePreviewScreen({
107117 if ( focused && state . data !== null && previews . length === 0 ) onClose ( ) ;
108118 } , [ focused , state . data , previews . length , onClose ] ) ;
109119 useEffect ( ( ) => {
110- if ( ! focused || ! foreground ) return ;
120+ if ( ! focused || ! foreground || embedded ) return ;
111121 const shaken = createShakeDetector ( ) ;
112122 Accelerometer . setUpdateInterval ( 50 ) ;
113123 const subscription = Accelerometer . addListener ( ( sample ) => {
@@ -116,7 +126,7 @@ function DevicePreviewScreen({
116126 setControlsVisible ( ( visible ) => ! visible ) ;
117127 } ) ;
118128 return ( ) => subscription . remove ( ) ;
119- } , [ focused , foreground ] ) ;
129+ } , [ focused , foreground , embedded ] ) ;
120130 useEffect ( ( ) => {
121131 if ( controlsVisible ) return ;
122132 const subscription = BackHandler . addEventListener ( "hardwareBackPress" , ( ) => {
@@ -260,7 +270,23 @@ function DevicePreviewScreen({
260270 ] ;
261271 return (
262272 < View className = "flex-1" style = { { backgroundColor : themeVariables [ "--color-sheet-solid" ] } } >
263- < StatusBar hidden animated />
273+ { ! embedded ? < StatusBar hidden animated /> : null }
274+ { embedded ? (
275+ < View
276+ className = "flex-row items-center gap-1 px-2 pb-2"
277+ style = { { paddingTop : Math . max ( insets . top , 8 ) } }
278+ >
279+ < SheetCloseButton accessibilityLabel = "Close device preview" onClose = { onClose } />
280+ < AppText numberOfLines = { 1 } className = "min-w-0 flex-1 font-t3-medium text-sm" >
281+ { preview ?. name ?? "Devices" }
282+ </ AppText >
283+ < ControlPill
284+ accessibilityLabel = "Maximize device"
285+ icon = "arrow.up.left.and.arrow.down.right"
286+ onPress = { ( ) => onMaximize ( preview ?. key ?? null ) }
287+ />
288+ </ View >
289+ ) : null }
264290 { preview && focused && foreground ? (
265291 < OpenDevicePreview
266292 key = { `${ preview . key } :${ streamAttempt } ` }
@@ -285,7 +311,20 @@ function DevicePreviewScreen({
285311 ) : null }
286312 </ View >
287313 ) }
288- { controlsVisible ? (
314+ { embedded ? (
315+ < View
316+ className = "flex-row items-center justify-center gap-2 px-3 py-2"
317+ style = { { paddingBottom : Math . max ( insets . bottom , 8 ) } }
318+ >
319+ < ControlPill
320+ accessibilityLabel = "Home"
321+ icon = "house"
322+ disabled = { ! inputConnected }
323+ onPress = { pressHome }
324+ />
325+ < DeviceOptionsMenu items = { menuItems } />
326+ </ View >
327+ ) : controlsVisible ? (
289328 < Animated . View
290329 entering = { OVERLAY_ENTERING }
291330 exiting = { OVERLAY_EXITING }
@@ -321,9 +360,9 @@ function DevicePreviewScreen({
321360 accessibilityLabel = "Show device controls"
322361 accessibilityHint = "Shaking the phone also shows them"
323362 hitSlop = { 12 }
324- className = "absolute h-6 w-16 items-center justify-center self-center"
363+ className = "absolute h-11 w-16 items-center justify-center self-center"
325364 style = { {
326- top : Math . max ( insets . top - 18 , 4 ) ,
365+ top : Math . max ( insets . top , 12 ) ,
327366 transform : [ { translateX : ( handleBounds . left - handleBounds . right ) / 2 } ] ,
328367 } }
329368 onPress = { ( ) => setControlsVisible ( true ) }
0 commit comments