-
Notifications
You must be signed in to change notification settings - Fork 30
/
Copy pathApp.tsx
76 lines (68 loc) · 1.94 KB
/
App.tsx
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
import cn from 'classnames'
import { type FC, type ReactElement, useEffect } from 'react'
import { useRoutes } from 'react-router-dom'
import {
ErrorBoundary,
LanguageSelector,
Offline,
ThemeSwitcher,
} from 'components'
import { routes } from 'router/Router'
import {
THEME_NAMES,
localStorageAppKey,
reduxHydrationAction,
} from 'constants/commonConstants'
import { type RootState, useAppDispatch, useAppSelector } from 'store/store'
import { switchToDark } from 'store/theme/themeSlice'
import { isServer } from 'utils'
const App: FC = (): ReactElement => {
const content = useRoutes(routes)
const currentTheme = useAppSelector((state) => state.theme.theme)
const dispatch = useAppDispatch()
useEffect(() => {
if (
window.__PRELOADED_STATE__?.theme?.theme == null &&
JSON.parse(localStorage.getItem(localStorageAppKey) as string)?.theme ==
null &&
window.matchMedia('(prefers-color-scheme: dark)').matches
) {
dispatch(switchToDark())
}
/*
If it is SSR version, we need to render a server version first
to avoid "Text content does not match server-rendered HTML" error.
Then rehydrate all the state with persisted data from local storage.
Otherwise, check index.tsx file.
*/
if (
!isServer &&
!NO_SSR &&
localStorage.getItem(localStorageAppKey) != null
) {
const localStoragePersistedState: Partial<RootState> = JSON.parse(
localStorage.getItem(localStorageAppKey) as string,
)
dispatch({
type: reduxHydrationAction,
payload: localStoragePersistedState,
})
}
}, [dispatch])
return (
<ErrorBoundary>
<div
className={cn(
'app-wrapper',
currentTheme === THEME_NAMES.DARK && 'theme-dark',
)}
>
<LanguageSelector />
<ThemeSwitcher />
<Offline />
{content}
</div>
</ErrorBoundary>
)
}
export { App }