-
Notifications
You must be signed in to change notification settings - Fork 23
Expand file tree
/
Copy pathApplicationModeContext.tsx
More file actions
103 lines (103 loc) · 3.25 KB
/
ApplicationModeContext.tsx
File metadata and controls
103 lines (103 loc) · 3.25 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
import { jsx as _jsx } from "react/jsx-runtime";
import { createContext, useContext, useState, useEffect } from 'react';
import { createLogger } from '../utils/logger';
const logger = createLogger('ApplicationModeContext');
const defaultContext = {
mode: 'desktop',
previousMode: null,
isXRMode: false,
isMobileView: false,
setMode: () => { },
layoutSettings: {
showPanels: true,
showViewport: true,
showControls: true
}
};
// Create the context
const ApplicationModeContext = createContext(defaultContext);
/**
* Provider component for application mode
*/
export const ApplicationModeProvider = ({ children }) => {
const [mode, setMode] = useState('desktop');
const [previousMode, setPreviousMode] = useState(null);
const [isMobileView, setIsMobileView] = useState(false);
// Check for mobile view on mount and resize
useEffect(() => {
const handleResize = () => {
const isMobile = window.innerWidth < 768; // Breakpoint for mobile view
setIsMobileView(isMobile);
// Auto-switch to mobile mode based on screen size
// but don't override XR mode
if (isMobile && mode !== 'xr') {
setMode('mobile');
}
else if (!isMobile && mode === 'mobile') {
setMode('desktop');
}
};
// Initial check
handleResize();
// Add event listener
window.addEventListener('resize', handleResize);
// Cleanup
return () => {
window.removeEventListener('resize', handleResize);
};
}, [mode]);
// Handle mode change
const handleModeChange = (newMode) => {
logger.info(`Changing mode: ${mode} -> ${newMode}`);
setPreviousMode(mode);
setMode(newMode);
};
// Compute layout settings based on current mode
const getLayoutSettings = () => {
switch (mode) {
case 'desktop':
return {
showPanels: true,
showViewport: true,
showControls: true
};
case 'mobile':
return {
showPanels: true,
showViewport: true,
showControls: true
};
case 'xr':
return {
showPanels: false,
showViewport: true,
showControls: false
};
default:
return {
showPanels: true,
showViewport: true,
showControls: true
};
}
};
const contextValue = {
mode,
previousMode,
isXRMode: mode === 'xr',
isMobileView,
setMode: handleModeChange,
layoutSettings: getLayoutSettings()
};
return (_jsx(ApplicationModeContext.Provider, { value: contextValue, children: children }));
};
/**
* Hook to use the application mode context
*/
export const useApplicationMode = () => {
const context = useContext(ApplicationModeContext);
if (!context) {
throw new Error('useApplicationMode must be used within an ApplicationModeProvider');
}
return context;
};