metacubexd/src/App.tsx

95 lines
2.5 KiB
TypeScript
Raw Normal View History

import { usePrefersDark } from '@solid-primitives/media'
import { Navigate, Route, Routes, useNavigate } from '@solidjs/router'
import { Show, createEffect, lazy, onMount } from 'solid-js'
import { Toaster } from 'solid-toast'
import { twMerge } from 'tailwind-merge'
import { Header } from '~/components'
2023-09-03 03:36:12 +08:00
import { ROUTES } from '~/constants'
import {
2023-09-08 18:42:15 +08:00
WsMsg,
autoSwitchTheme,
curTheme,
endpoint,
favDayTheme,
favNightTheme,
setCurTheme,
2023-09-08 18:42:15 +08:00
setLatestConnectionMsg,
useProxies,
useTwemoji,
2023-09-08 18:42:15 +08:00
useWsRequest,
} from '~/signals'
2023-08-24 04:20:53 +08:00
const Setup = lazy(() => import('~/pages/Setup'))
const Overview = lazy(() => import('~/pages/Overview'))
const Connections = lazy(() => import('~/pages/Connections'))
const Logs = lazy(() => import('~/pages/Logs'))
const Proxies = lazy(() => import('~/pages/Proxies'))
const Rules = lazy(() => import('~/pages/Rules'))
const Config = lazy(() => import('~/pages/Config'))
const ProtectedResources = () => {
const { fetchProxies } = useProxies()
onMount(fetchProxies)
const latestConnectionMsg = useWsRequest<WsMsg>('connections')
createEffect(() => {
setLatestConnectionMsg(latestConnectionMsg())
})
return null
}
2023-08-24 04:20:53 +08:00
export const App = () => {
const navigate = useNavigate()
const prefersDark = usePrefersDark()
2023-09-02 19:06:02 +08:00
createEffect(() => {
if (autoSwitchTheme()) {
setCurTheme(prefersDark() ? favNightTheme() : favDayTheme())
}
})
onMount(() => {
if (!endpoint()) {
2023-09-04 13:08:05 +08:00
navigate(ROUTES.Setup)
2023-08-24 04:20:53 +08:00
}
})
return (
<div
class={twMerge(
'relative flex h-screen flex-col subpixel-antialiased p-safe',
useTwemoji() ? 'font-twemoji' : 'font-no-twemoji',
)}
data-theme={curTheme()}
>
2023-08-27 22:25:41 +08:00
<Header />
2023-08-24 04:20:53 +08:00
<div class="flex-1 overflow-y-auto p-2 sm:p-4">
2023-08-24 04:20:53 +08:00
<Routes>
<Show when={endpoint()}>
2023-09-03 03:36:12 +08:00
<Route path={ROUTES.Overview} component={Overview} />
<Route path={ROUTES.Proxies} component={Proxies} />
<Route path={ROUTES.Rules} component={Rules} />
<Route path={ROUTES.Conns} component={Connections} />
<Route path={ROUTES.Log} component={Logs} />
<Route path={ROUTES.Config} component={Config} />
<Route path="*" element={<Navigate href={ROUTES.Overview} />} />
2023-08-24 04:20:53 +08:00
</Show>
<Route path={endpoint() ? ROUTES.Setup : '*'} component={Setup} />
2023-08-24 04:20:53 +08:00
</Routes>
<Show when={endpoint()}>
<ProtectedResources />
</Show>
2023-08-24 04:20:53 +08:00
</div>
<Toaster position="bottom-center" />
2023-08-24 04:20:53 +08:00
</div>
)
}