routes.tsx 2.2 KB

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