-
Notifications
You must be signed in to change notification settings - Fork 108
/
App.jsx
174 lines (159 loc) · 5.73 KB
/
App.jsx
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
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
import { useContext, useEffect, useState, useRef } from 'react'
import { ColorSchemeContext, SettingsContext, ThemeContext } from './contexts/Settings'
import { useReset, useStateSelector, useUpdate } from './contexts/Store'
import { AnimatePresence, motion} from 'framer-motion'
import ActiveElements from './components/ActiveElements/ActiveElements'
import QueryField from './components/QueryField/QueryField'
import Settings from './components/Settings/Settings'
import LayoutButton from './components/LayoutButton/LayoutButton'
import { BsGearFill, BsChevronRight } from 'react-icons/bs'
import { RiMenu5Fill } from 'react-icons/ri'
import { allowedModes } from './rules'
import { isMobile } from 'react-device-detect'
import classes from './App.module.css'
import './App.css'
const ignoreMobile = localStorage.getItem('ignoreMobile')
function App() {
// settings
const settings = useContext(SettingsContext)
// theme
const theme = useContext(ThemeContext)
// color scheme
const colorScheme = useContext(ColorSchemeContext)
/* store */
const mode = useStateSelector(state => state.mode)
const redirected = useStateSelector(state => state.redirected)
const timestamp = useStateSelector(state => state.timestamp)
const updateStore = useUpdate()
const resetStore = useReset()
// ---
const [showSettings, setShowSettings] = useState(false)
const [showReset, setShowReset] = useState(false)
/* handlers */
const onContextMenuRef = useRef(null)
const onKeyUpRef = useRef(null)
const onKeyDownRef = useRef(null)
function switchMacrosMenu() {
if (mode === 'default')
updateStore({ mode: 'opened' })
else if (mode === 'opened')
updateStore({ mode: 'default' })
}
onKeyUpRef.current = e => {
if (e.key === 'Shift')
if (allowedModes.get('Chevron').has(mode))
if (mode === 'opened')
updateStore({ mode: 'default' })
}
onKeyDownRef.current = e => {
if (e.key === 'Shift')
if (allowedModes.get('Chevron').has(mode))
if (mode === 'default')
updateStore({ mode: 'opened' })
}
onContextMenuRef.current = e => {
switchMacrosMenu()
e.preventDefault()
}
// ---
// adding event listeners
useEffect(() => {
const onContextMenu = e => onContextMenuRef.current(e)
const onKeyUp = e => onKeyUpRef.current(e)
const onKeyDown = e => onKeyDownRef.current(e)
document.addEventListener('keyup', onKeyUp)
document.addEventListener('keydown', onKeyDown)
document.addEventListener('contextmenu', onContextMenu)
return () => {
document.removeEventListener('keyup', onKeyUp)
document.removeEventListener('keydown', onKeyDown)
document.removeEventListener('contextmenu', onContextMenu)
}
}, [])
/* setting document title */
useEffect(() => {
document.title = settings.general.tabTitle
}, [settings.general.tabTitle])
// ---
/* setting theme variables */
useEffect(() => {
const root = document.documentElement
for (const variable in theme)
root.style.setProperty('--' + variable, theme[variable])
}, [theme])
// ---
/* setting color scheme variables */
useEffect(() => {
document.body.setAttribute('data-color-scheme', colorScheme)
}, [colorScheme])
// ---
/* firefox history back caching fix [#1 issue] */
const handleVisibilityChange = useRef(null)
handleVisibilityChange.current = () => {
// reset store if user went back from a redirected page
if (document.visibilityState === 'visible' && redirected)
setShowReset(true)
}
useEffect(() => {
document.addEventListener('visibilitychange', () => handleVisibilityChange.current())
}, [])
// ---
return (
<div className='app'>
{
!isMobile || ignoreMobile
? <AnimatePresence>
{
showSettings
? <Settings key='settings' onClose={() => {
setShowSettings(false)
resetStore()}}/>
:
<motion.div
key={timestamp}
className={classes['container']}
initial={{ opacity: 0 }}
animate={{ opacity: 1 }}
exit={redirected || { opacity: 0 }}>
<ActiveElements/>
<QueryField/>
<LayoutButton
id='settings'
style={{ right: 0, top: 0 }}
onClick={() => setShowSettings(state => !state)}>
<BsGearFill/>
</LayoutButton>
<LayoutButton
id='macros-menu'
style={{ right: 0, bottom: 0 }}
onClick={switchMacrosMenu}>
{
mode === 'default' && <RiMenu5Fill/>
}
{
mode === 'opened' && <BsChevronRight/>
}
</LayoutButton>
{
showReset && <div className={classes['cancel-button']} onClick={() => location.reload()}>Cancel</div>
}
</motion.div>
}
</AnimatePresence>
: <div className={classes['mobile-warning']}>
<div>
Mobile devices are not supported :( <br />
<span className={classes['ignore-mobile-button']}
onClick={() => {
localStorage.setItem('ignoreMobile', true)
location.reload()
}}>
ignore this warning
</span>
</div>
</div>
}
</div>
)
}
export default App