routes.tsx 2.4 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768
  1. import { lazy, Suspense } from 'react';
  2. import { createBrowserRouter, type RouteObject } from 'react-router';
  3. import { Spin } from 'antd';
  4. import PanelLayout from '@/layouts/PanelLayout';
  5. const IndexPage = lazy(() => import('@/pages/index/IndexPage'));
  6. const InboundsPage = lazy(() => import('@/pages/inbounds/InboundsPage'));
  7. const ClientsPage = lazy(() => import('@/pages/clients/ClientsPage'));
  8. const GroupsPage = lazy(() => import('@/pages/groups/GroupsPage'));
  9. const NodesPage = lazy(() => import('@/pages/nodes/NodesPage'));
  10. const HostsPage = lazy(() => import('@/pages/hosts/HostsPage'));
  11. const SettingsPage = lazy(() => import('@/pages/settings/SettingsPage'));
  12. const XrayPage = lazy(() => import('@/pages/xray/XrayPage'));
  13. const ApiDocsPage = lazy(() => import('@/pages/api-docs/ApiDocsPage'));
  14. const SponsorsPage = lazy(() => import('@/pages/sponsors/SponsorsPage'));
  15. function withSuspense(node: React.ReactNode) {
  16. return (
  17. <Suspense
  18. fallback={
  19. <div
  20. style={{
  21. display: 'flex',
  22. justifyContent: 'center',
  23. alignItems: 'center',
  24. minHeight: '60vh',
  25. }}
  26. >
  27. <Spin size="large" />
  28. </div>
  29. }
  30. >
  31. {node}
  32. </Suspense>
  33. );
  34. }
  35. const routes: RouteObject[] = [
  36. {
  37. path: '/',
  38. element: <PanelLayout />,
  39. children: [
  40. { index: true, element: withSuspense(<IndexPage />) },
  41. { path: 'inbounds', element: withSuspense(<InboundsPage />) },
  42. { path: 'clients', element: withSuspense(<ClientsPage />) },
  43. { path: 'groups', element: withSuspense(<GroupsPage />) },
  44. { path: 'nodes', element: withSuspense(<NodesPage />) },
  45. { path: 'hosts', element: withSuspense(<HostsPage />) },
  46. { path: 'settings', element: withSuspense(<SettingsPage />) },
  47. { path: 'xray', element: withSuspense(<XrayPage />) },
  48. { path: 'outbound', element: withSuspense(<XrayPage />) },
  49. { path: 'routing', element: withSuspense(<XrayPage />) },
  50. { path: 'api-docs', element: withSuspense(<ApiDocsPage />) },
  51. { path: 'sponsors', element: withSuspense(<SponsorsPage />) },
  52. ],
  53. },
  54. ];
  55. function computeBasename() {
  56. const raw = (typeof window !== 'undefined' && window.X_UI_BASE_PATH) || '/';
  57. const trimmed = raw.replace(/\/+$/, '');
  58. return `${trimmed}/panel`;
  59. }
  60. export const router = createBrowserRouter(routes, {
  61. basename: computeBasename(),
  62. });