Skip to content

Commit e9faa08

Browse files
committed
feat: refactor routing and endpoint checks across multiple pages
1 parent 52d212b commit e9faa08

File tree

9 files changed

+34
-73
lines changed

9 files changed

+34
-73
lines changed

eslint.config.mjs

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,9 @@ import tseslint from 'typescript-eslint'
44

55
export default tseslint.config(
66
{
7-
ignores: ['dist'],
7+
ignores: ['dist/**', 'node_modules/**'],
8+
},
9+
{
810
settings: {
911
'import/resolver': {
1012
typescript: {},

src/App.tsx

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ import { usePrefersDark } from '@solid-primitives/media'
22
import type { ParentComponent } from 'solid-js'
33
import { twMerge } from 'tailwind-merge'
44
import { Header } from '~/components'
5+
import { ROUTES } from '~/constants'
56
import {
67
MemoryData,
78
TrafficData,
@@ -33,6 +34,7 @@ const ProtectedResources = () => {
3334
}
3435

3536
export const App: ParentComponent = ({ children }) => {
37+
const location = useLocation()
3638
const prefersDark = usePrefersDark()
3739

3840
createEffect(() => {
@@ -45,6 +47,10 @@ export const App: ParentComponent = ({ children }) => {
4547
document.documentElement.setAttribute('data-theme', curTheme())
4648
})
4749

50+
// Route guard: redirect to setup if no endpoint configured
51+
const isSetupPage = () => location.pathname === ROUTES.Setup
52+
const needsRedirect = () => !endpoint() && !isSetupPage()
53+
4854
return (
4955
<div
5056
ref={(el) => setRootElement(el)}
@@ -55,7 +61,14 @@ export const App: ParentComponent = ({ children }) => {
5561
>
5662
<Header />
5763

58-
<div class="flex-1 overflow-y-auto p-2 sm:p-4">{children}</div>
64+
<div class="flex-1 overflow-y-auto p-2 sm:p-4">
65+
<Show
66+
when={!needsRedirect()}
67+
fallback={<Navigate href={ROUTES.Setup} />}
68+
>
69+
{children}
70+
</Show>
71+
</div>
5972

6073
<Show when={!!endpoint()}>
6174
<ProtectedResources />

src/main.tsx

Lines changed: 13 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
import '~/index.css'
33

44
import { MetaProvider } from '@solidjs/meta'
5+
import type { RouteDefinition } from '@solidjs/router'
56
import dayjs from 'dayjs'
67
import 'dayjs/locale/zh-cn'
78
import relativeTime from 'dayjs/plugin/relativeTime'
@@ -12,30 +13,25 @@ import { App } from '~/App'
1213
import { ROUTES } from '~/constants'
1314
import { I18nProvider, locale } from '~/i18n'
1415

15-
const Setup = lazy(() => import('~/pages/Setup'))
16-
const Overview = lazy(() => import('~/pages/Overview'))
17-
const Connections = lazy(() => import('~/pages/Connections'))
18-
const Logs = lazy(() => import('~/pages/Logs'))
19-
const Proxies = lazy(() => import('~/pages/Proxies'))
20-
const Rules = lazy(() => import('~/pages/Rules'))
21-
const Config = lazy(() => import('~/pages/Config'))
16+
const routes: RouteDefinition[] = [
17+
{ path: ROUTES.Setup, component: lazy(() => import('~/pages/Setup')) },
18+
{ path: ROUTES.Overview, component: lazy(() => import('~/pages/Overview')) },
19+
{ path: ROUTES.Proxies, component: lazy(() => import('~/pages/Proxies')) },
20+
{ path: ROUTES.Rules, component: lazy(() => import('~/pages/Rules')) },
21+
{ path: ROUTES.Conns, component: lazy(() => import('~/pages/Connections')) },
22+
{ path: ROUTES.Log, component: lazy(() => import('~/pages/Logs')) },
23+
{ path: ROUTES.Config, component: lazy(() => import('~/pages/Config')) },
24+
// Fallback to overview for unknown routes
25+
{ path: '*', component: lazy(() => import('~/pages/Overview')) },
26+
]
2227

2328
dayjs.extend(relativeTime)
2429

2530
render(
2631
() => (
2732
<I18nProvider locale={locale()}>
2833
<MetaProvider>
29-
<HashRouter root={App}>
30-
<Route path={ROUTES.Setup} component={Setup} />
31-
<Route path="*" component={Overview} />
32-
<Route path={ROUTES.Overview} component={Overview} />
33-
<Route path={ROUTES.Proxies} component={Proxies} />
34-
<Route path={ROUTES.Rules} component={Rules} />
35-
<Route path={ROUTES.Conns} component={Connections} />
36-
<Route path={ROUTES.Log} component={Logs} />
37-
<Route path={ROUTES.Config} component={Config} />
38-
</HashRouter>
34+
<HashRouter root={App}>{routes}</HashRouter>
3935
</MetaProvider>
4036

4137
<Toaster position="bottom-center" />

src/pages/Config.tsx

Lines changed: 0 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -557,14 +557,6 @@ const ConfigForXd = () => {
557557
}
558558

559559
export default () => {
560-
const navigate = useNavigate()
561-
562-
if (!endpoint()) {
563-
navigate('/setup', { replace: true })
564-
565-
return null
566-
}
567-
568560
const [t] = useI18n()
569561

570562
const frontendVersion = `v${import.meta.env.APP_VERSION}`

src/pages/Connections/Connections.tsx

Lines changed: 0 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,6 @@ import {
66
import {
77
connectionsTableColumnOrder,
88
connectionsTableColumnVisibility,
9-
endpoint,
109
setConnectionsTableColumnOrder,
1110
setConnectionsTableColumnVisibility,
1211
} from '~/signals'
@@ -17,14 +16,6 @@ import { MobilePagination, DesktopPagination } from './ConnectionsPagination'
1716
import { useConnectionsTable } from './useConnectionsTable'
1817

1918
export default () => {
20-
const navigate = useNavigate()
21-
22-
if (!endpoint()) {
23-
navigate('/setup', { replace: true })
24-
25-
return null
26-
}
27-
2819
const [t] = useI18n()
2920

3021
let connectionsSettingsModalRef: HTMLDialogElement | undefined

src/pages/Logs.tsx

Lines changed: 3 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ import { twMerge } from 'tailwind-merge'
2121
import { Button, DocumentTitle, LogsSettingsModal } from '~/components'
2222
import { LOG_LEVEL } from '~/constants'
2323
import { useI18n } from '~/i18n'
24-
import { endpoint, logsTableSize, tableSizeClassName, useLogs } from '~/signals'
24+
import { logsTableSize, tableSizeClassName, useLogs } from '~/signals'
2525
import { LogWithSeq } from '~/types'
2626

2727
const fuzzyFilter: FilterFn<LogWithSeq> = (row, columnId, value, addMeta) => {
@@ -38,14 +38,6 @@ const fuzzyFilter: FilterFn<LogWithSeq> = (row, columnId, value, addMeta) => {
3838
}
3939

4040
export default () => {
41-
const navigate = useNavigate()
42-
43-
if (!endpoint()) {
44-
navigate('/setup', { replace: true })
45-
46-
return null
47-
}
48-
4941
let logsSettingsModalRef: HTMLDialogElement | undefined
5042
const [t] = useI18n()
5143
const [globalFilter, setGlobalFilter] = createSignal('')
@@ -126,7 +118,7 @@ export default () => {
126118
<div class="join w-full">
127119
<input
128120
type="search"
129-
class="input input-sm join-item flex-1 flex-shrink-0 input-primary"
121+
class="input input-sm join-item flex-1 shrink-0 input-primary"
130122
placeholder={t('search')}
131123
onInput={(e) => setGlobalFilter(e.target.value)}
132124
/>
@@ -197,7 +189,7 @@ export default () => {
197189
<tbody>
198190
<For each={table.getRowModel().rows}>
199191
{(row) => (
200-
<tr class="hover:!bg-primary hover:text-primary-content">
192+
<tr class="hover:bg-primary! hover:text-primary-content">
201193
<For each={row.getVisibleCells()}>
202194
{(cell) => (
203195
<td class="py-2">

src/pages/Overview.tsx

Lines changed: 0 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -30,14 +30,6 @@ const TrafficWidget: ParentComponent<{ label: JSX.Element }> = (props) => (
3030
)
3131

3232
export default () => {
33-
const navigate = useNavigate()
34-
35-
if (!endpoint()) {
36-
navigate('/setup', { replace: true })
37-
38-
return null
39-
}
40-
4133
const [t] = useI18n()
4234

4335
// Chart refs for real-time updates

src/pages/Proxies.tsx

Lines changed: 0 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,6 @@ import {
2525
} from '~/helpers'
2626
import { useI18n } from '~/i18n'
2727
import {
28-
endpoint,
2928
hideUnAvailableProxies,
3029
iconHeight,
3130
iconMarginRight,
@@ -40,14 +39,6 @@ enum ActiveTab {
4039
}
4140

4241
export default () => {
43-
const navigate = useNavigate()
44-
45-
if (!endpoint()) {
46-
navigate('/setup', { replace: true })
47-
48-
return null
49-
}
50-
5142
let proxiesSettingsModalRef: HTMLDialogElement | undefined
5243

5344
const [t] = useI18n()

src/pages/Rules.tsx

Lines changed: 1 addition & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ import { twMerge } from 'tailwind-merge'
55
import { Button, DocumentTitle } from '~/components'
66
import { formatTimeFromNow, useStringBooleanMap } from '~/helpers'
77
import { useI18n } from '~/i18n'
8-
import { endpoint, useRules } from '~/signals'
8+
import { useRules } from '~/signals'
99
import { Rule, RuleProvider } from '~/types'
1010

1111
enum ActiveTab {
@@ -14,14 +14,6 @@ enum ActiveTab {
1414
}
1515

1616
export default () => {
17-
const navigate = useNavigate()
18-
19-
if (!endpoint()) {
20-
navigate('/setup', { replace: true })
21-
22-
return null
23-
}
24-
2517
const [t] = useI18n()
2618
const {
2719
rules,

0 commit comments

Comments
 (0)