11import type { DevicePlatform , EnvironmentId } from "@t3tools/contracts" ;
2- import { useCallback , useEffect , useMemo , useRef , useState , type ReactNode } from "react" ;
2+ import { useEffect , useRef , useState , type ReactNode } from "react" ;
33
44import { cn } from "~/lib/utils" ;
55import { Button } from "~/components/ui/button" ;
@@ -27,6 +27,35 @@ import { observeResize } from "~/lib/observeResize";
2727const AX_POLL_INTERVAL_MS = 2_000 ;
2828const CONTROLS_RAIL_WIDTH = 56 ;
2929
30+ function screenAspect ( platform : DevicePlatform , screen : DeviceScreenSize | null ) : number {
31+ if ( ! screen ) return platform === "ios" ? 9 / 19.5 : 9 / 20 ;
32+ const landscape =
33+ screen . orientation === "landscape_left" || screen . orientation === "landscape_right" ;
34+ const w = landscape
35+ ? Math . max ( screen . width , screen . height )
36+ : Math . min ( screen . width , screen . height ) ;
37+ const h = landscape
38+ ? Math . min ( screen . width , screen . height )
39+ : Math . max ( screen . width , screen . height ) ;
40+ return w / h ;
41+ }
42+
43+ // serve-sim streams the raw framebuffer; rotate the display for a device
44+ // that reports landscape while its frames stay portrait.
45+ function screenRotation ( platform : DevicePlatform , screen : DeviceScreenSize | null ) : number {
46+ if ( platform !== "ios" || ! screen || screen . width > screen . height ) return 0 ;
47+ switch ( screen . orientation ) {
48+ case "landscape_left" :
49+ return 90 ;
50+ case "landscape_right" :
51+ return - 90 ;
52+ case "portrait_upside_down" :
53+ return 180 ;
54+ default :
55+ return 0 ;
56+ }
57+ }
58+
3059export interface DeviceViewControls {
3160 readonly phone : boolean ;
3261 readonly streaming : boolean ;
@@ -68,20 +97,20 @@ export function DeviceStreamView(props: {
6897 const [ presentation , setPresentation ] = useState < "phone" | "flat" > ( "phone" ) ;
6998 const [ keyboardAttached , setKeyboardAttached ] = useState ( false ) ;
7099 const [ phoneUnavailable , setPhoneUnavailable ] = useState ( false ) ;
71- const onPhoneUnavailable = useCallback ( ( ) => setPhoneUnavailable ( true ) , [ ] ) ;
100+ const onPhoneUnavailable = ( ) => setPhoneUnavailable ( true ) ;
72101 const cancelPhoneInputRef = useRef < ( ( ) => void ) | null > ( null ) ;
73- const cancelPhoneInput = useCallback ( ( ) => cancelPhoneInputRef . current ?.( ) , [ ] ) ;
102+ const cancelPhoneInput = ( ) => cancelPhoneInputRef . current ?.( ) ;
74103 const resetViewRef = useRef < ( ( ) => void ) | null > ( null ) ;
75- const onResetReady = useCallback ( ( reset : ( ( ) => void ) | null ) => {
104+ const onResetReady = ( reset : ( ( ) => void ) | null ) => {
76105 resetViewRef . current = reset ;
77- } , [ ] ) ;
106+ } ;
78107 const frameListenerRef = useRef < ( ( ) => void ) | null > ( null ) ;
79- const onFrameListener = useCallback ( ( listener : ( ( ) => void ) | null ) => {
108+ const onFrameListener = ( listener : ( ( ) => void ) | null ) => {
80109 frameListenerRef . current = listener ;
81- } , [ ] ) ;
82- const onInputCancel = useCallback ( ( cancel : ( ( ) => void ) | null ) => {
110+ } ;
111+ const onInputCancel = ( cancel : ( ( ) => void ) | null ) => {
83112 cancelPhoneInputRef . current = cancel ;
84- } , [ ] ) ;
113+ } ;
85114 const access = useDeviceHubAccess ( props . environmentId , props . hostId ) ;
86115 const canvasRef = useRef < HTMLCanvasElement | null > ( null ) ;
87116 const clientRef = useRef < DeviceStreamClient | null > ( null ) ;
@@ -93,9 +122,9 @@ export function DeviceStreamView(props: {
93122 const [ foldAngle , setFoldAngle ] = useState < number | null > ( null ) ;
94123 const [ mjpegUrl , setMjpegUrl ] = useState < string | null > ( null ) ;
95124 const [ mjpegGeneration , setMjpegGeneration ] = useState ( 0 ) ;
96- const attachMjpegImage = useCallback ( ( image : HTMLImageElement | null ) => {
125+ const attachMjpegImage = ( image : HTMLImageElement | null ) => {
97126 clientRef . current ?. setMjpegImage ( image ) ;
98- } , [ ] ) ;
127+ } ;
99128 const [ inputState , setInputState ] = useState < { connected : boolean ; detail ?: string } > ( {
100129 connected : false ,
101130 } ) ;
@@ -169,18 +198,7 @@ export function DeviceStreamView(props: {
169198 props . visible ,
170199 ] ) ;
171200
172- const aspect = useMemo ( ( ) => {
173- if ( ! screen ) return props . platform === "ios" ? 9 / 19.5 : 9 / 20 ;
174- const landscape =
175- screen . orientation === "landscape_left" || screen . orientation === "landscape_right" ;
176- const w = landscape
177- ? Math . max ( screen . width , screen . height )
178- : Math . min ( screen . width , screen . height ) ;
179- const h = landscape
180- ? Math . min ( screen . width , screen . height )
181- : Math . max ( screen . width , screen . height ) ;
182- return w / h ;
183- } , [ props . platform , screen ] ) ;
201+ const aspect = screenAspect ( props . platform , screen ) ;
184202
185203 const retainingFrame =
186204 status === "connecting" && hasFrame && inputState . connected && screen !== null ;
@@ -220,25 +238,9 @@ export function DeviceStreamView(props: {
220238 update ( ) ;
221239 return observeResize ( element , update ) ;
222240 } , [ ] ) ;
223- const frame = useMemo ( ( ) => {
224- return fitDeviceFrame ( aspect , host . width , host . height , controlsInset ) ;
225- } , [ aspect , controlsInset , host ] ) ;
241+ const frame = fitDeviceFrame ( aspect , host . width , host . height , controlsInset ) ;
226242
227- // serve-sim streams the raw framebuffer; rotate the display for a device
228- // that reports landscape while its frames stay portrait.
229- const rotation = useMemo ( ( ) => {
230- if ( props . platform !== "ios" || ! screen || screen . width > screen . height ) return 0 ;
231- switch ( screen . orientation ) {
232- case "landscape_left" :
233- return 90 ;
234- case "landscape_right" :
235- return - 90 ;
236- case "portrait_upside_down" :
237- return 180 ;
238- default :
239- return 0 ;
240- }
241- } , [ props . platform , screen ] ) ;
243+ const rotation = screenRotation ( props . platform , screen ) ;
242244
243245 const sideways = rotation === 90 || rotation === - 90 ;
244246 const mediaStyle : React . CSSProperties = sideways
@@ -299,13 +301,13 @@ export function DeviceStreamView(props: {
299301 : null ;
300302
301303 const keyboardSource = deviceKeyboard ( props . platform , props . deviceName ?? "" ) ;
302- const resetView = useCallback ( ( ) => {
304+ const resetView = ( ) => {
303305 if ( ! isDuo ) {
304306 const orientation = keyboardAttached ? "landscape_right" : "portrait" ;
305307 if ( screen ?. orientation !== orientation ) clientRef . current ?. setOrientation ( orientation ) ;
306308 }
307309 resetViewRef . current ?.( ) ;
308- } , [ isDuo , keyboardAttached , screen ?. orientation ] ) ;
310+ } ;
309311 const profile = resolveDeviceShape ( {
310312 platform : props . platform ,
311313 name : props . deviceName ?? "" ,
0 commit comments