feat: add router config, router, and sidenav config
This commit is contained in:
61
src/App.tsx
61
src/App.tsx
@@ -1,68 +1,17 @@
|
||||
import {
|
||||
Alert,
|
||||
Box,
|
||||
CssBaseline,
|
||||
TextField,
|
||||
Typography,
|
||||
useColorScheme,
|
||||
} from '@mui/material';
|
||||
import { CssBaseline } from '@mui/material';
|
||||
import './App.css';
|
||||
import { useTranslation } from 'react-i18next';
|
||||
import { LanguageManager } from './components/LanguageManager';
|
||||
import { LanguageManager } from '@/components/LanguageManager';
|
||||
import { RouterProvider } from 'react-router-dom';
|
||||
import { router } from '@/routes';
|
||||
|
||||
function App() {
|
||||
const { t } = useTranslation();
|
||||
const showToast = useToast();
|
||||
|
||||
return (
|
||||
<>
|
||||
<CssBaseline />
|
||||
<LanguageManager />
|
||||
<div style={{ padding: '16px' }}>
|
||||
<Typography variant="h3">{t('helloWorld')}</Typography>
|
||||
<Box
|
||||
sx={{ display: 'flex', flexDirection: 'column', gap: '10px', mt: 5 }}
|
||||
>
|
||||
<ThemeToggleButton />
|
||||
<Button color="secondary" variant="contained">
|
||||
secondary button
|
||||
</Button>
|
||||
<TextField label={t('helloWorld')} />
|
||||
<Alert severity="success" variant="filled">
|
||||
success
|
||||
</Alert>
|
||||
<Alert severity="warning" variant="filled">
|
||||
warning
|
||||
</Alert>
|
||||
<Alert severity="info" variant="filled">
|
||||
info
|
||||
</Alert>
|
||||
<Alert severity="error" variant="filled">
|
||||
error
|
||||
</Alert>
|
||||
</Box>
|
||||
</div>
|
||||
<Button onClick={() => showToast({ message: 'info toast' })}>
|
||||
toast
|
||||
</Button>
|
||||
<RouterProvider router={router} />
|
||||
</>
|
||||
);
|
||||
}
|
||||
|
||||
export default App;
|
||||
|
||||
import { Button } from '@mui/material';
|
||||
import { useToast } from '@rkheftan/harmony-ui';
|
||||
|
||||
export const ThemeToggleButton = () => {
|
||||
const { mode, setMode } = useColorScheme();
|
||||
|
||||
return (
|
||||
<Button
|
||||
variant="contained"
|
||||
onClick={() => setMode(mode === 'light' ? 'dark' : 'light')}
|
||||
>
|
||||
Switch to {mode === 'light' ? 'Dark' : 'Light'} Mode
|
||||
</Button>
|
||||
);
|
||||
};
|
||||
|
||||
46
src/components/Layout/Layout.tsx
Normal file
46
src/components/Layout/Layout.tsx
Normal file
@@ -0,0 +1,46 @@
|
||||
import { SideNav } from '@rkheftan/harmony-ui';
|
||||
import { buildNavItems } from './navItems';
|
||||
import { appRoutes } from '@/routes/config';
|
||||
import { Outlet, useLocation } from 'react-router-dom';
|
||||
import { Box } from '@mui/material';
|
||||
import { grey } from '@mui/material/colors';
|
||||
|
||||
export const Layout = () => {
|
||||
const navItemConfigs = buildNavItems(appRoutes);
|
||||
const location = useLocation();
|
||||
|
||||
return (
|
||||
<Box
|
||||
sx={{
|
||||
height: '100vh',
|
||||
display: 'flex',
|
||||
justifyContent: 'center',
|
||||
alignItems: 'center',
|
||||
}}
|
||||
>
|
||||
<Box
|
||||
sx={{
|
||||
width: 1070,
|
||||
height: '80%',
|
||||
display: 'flex',
|
||||
flexDirection: 'row-reverse',
|
||||
borderRadius: 4,
|
||||
backgroundColor: 'background.paper',
|
||||
overflow: 'hidden',
|
||||
padding: 1,
|
||||
}}
|
||||
>
|
||||
<Box sx={{ flex: 1 }}>
|
||||
<Outlet />
|
||||
</Box>
|
||||
|
||||
<SideNav
|
||||
navConfig={navItemConfigs}
|
||||
activePath={location.pathname + location.hash}
|
||||
selectedVariant="textOnly"
|
||||
positioning="absolute"
|
||||
/>
|
||||
</Box>
|
||||
</Box>
|
||||
);
|
||||
};
|
||||
27
src/components/Layout/navItems.tsx
Normal file
27
src/components/Layout/navItems.tsx
Normal file
@@ -0,0 +1,27 @@
|
||||
// src/components/SideNav.tsx (Conceptual Example)
|
||||
|
||||
import { useTranslation } from 'react-i18next';
|
||||
import { type RouteConfig } from '@/routes/config';
|
||||
import { Icon, type NavItemConfig } from '@rkheftan/harmony-ui';
|
||||
import type { Icon as Iconsax } from 'iconsax-react';
|
||||
|
||||
const getIcon = (icon?: Iconsax) => (isSelected: boolean) =>
|
||||
icon ? (
|
||||
<Icon Component={icon} variant={isSelected ? 'Bold' : 'Broken'} />
|
||||
) : undefined;
|
||||
|
||||
export function buildNavItems(routes: RouteConfig[]): NavItemConfig[] {
|
||||
const { t } = useTranslation();
|
||||
|
||||
return routes
|
||||
.filter((route) => route.navConfig)
|
||||
.map((route) => {
|
||||
const { title, icon } = route.navConfig!;
|
||||
return {
|
||||
text: t(title),
|
||||
getIcon: getIcon(icon),
|
||||
path: route.path,
|
||||
children: route.children ? buildNavItems(route.children) : undefined,
|
||||
};
|
||||
});
|
||||
}
|
||||
48
src/routes/config.tsx
Normal file
48
src/routes/config.tsx
Normal file
@@ -0,0 +1,48 @@
|
||||
import { Layout } from '@/components/Layout/Layout';
|
||||
import { Mobile, Personalcard, ProfileCircle, type Icon } from 'iconsax-react';
|
||||
import { type ReactNode } from 'react';
|
||||
import { Navigate } from 'react-router-dom';
|
||||
|
||||
export interface RouteConfig {
|
||||
path: string;
|
||||
element?: ReactNode;
|
||||
navConfig?: {
|
||||
title: string; // Translation key
|
||||
icon?: Icon;
|
||||
};
|
||||
children?: RouteConfig[];
|
||||
}
|
||||
|
||||
export const appRoutes: RouteConfig[] = [
|
||||
{
|
||||
path: '/',
|
||||
element: <Navigate to="/profile" replace />,
|
||||
},
|
||||
{
|
||||
path: '/profile',
|
||||
// can lazy load component if needed (ex. lazy(() => import('@/features/home/routes/HomePage'));)
|
||||
element: <Layout />,
|
||||
navConfig: {
|
||||
title: 'side.account',
|
||||
icon: ProfileCircle,
|
||||
},
|
||||
children: [
|
||||
{
|
||||
path: '/profile/info',
|
||||
element: <div>Personal Info Section</div>,
|
||||
navConfig: {
|
||||
title: 'side.personalInfo',
|
||||
icon: Personalcard,
|
||||
},
|
||||
},
|
||||
{
|
||||
path: '/profile/contact-info',
|
||||
element: <div>Personal Info Section</div>,
|
||||
navConfig: {
|
||||
title: 'side.contactInfo',
|
||||
icon: Mobile,
|
||||
},
|
||||
},
|
||||
],
|
||||
},
|
||||
];
|
||||
34
src/routes/index.tsx
Normal file
34
src/routes/index.tsx
Normal file
@@ -0,0 +1,34 @@
|
||||
import { Suspense, type ReactNode } from 'react';
|
||||
import { createBrowserRouter, type RouteObject } from 'react-router-dom';
|
||||
import { appRoutes, type RouteConfig } from './config';
|
||||
|
||||
/**
|
||||
* A recursive function to map our custom route config to the format
|
||||
* that react-router-dom expects, applying layouts and guards.
|
||||
*/
|
||||
function mapRoutes(routes: RouteConfig[]): RouteObject[] {
|
||||
return routes.map((route) => {
|
||||
// Start with the base element, wrapped in Suspense for lazy loading
|
||||
let element: ReactNode = (
|
||||
<Suspense fallback={<div>Loading...</div>}>{route.element}</Suspense>
|
||||
);
|
||||
|
||||
// Conditionally wrap the element in the specified layout
|
||||
// if (route.layout) {
|
||||
// element = <route.layout>{element}</route.layout>;
|
||||
// }
|
||||
|
||||
// Conditionally wrap the element in the authentication guard
|
||||
// if (route.authRequired) {
|
||||
// element = <ProtectedRoute>{element}</ProtectedRoute>;
|
||||
// }
|
||||
|
||||
return {
|
||||
path: route.path,
|
||||
element: element,
|
||||
...(route.children && { children: mapRoutes(route.children) }),
|
||||
};
|
||||
});
|
||||
}
|
||||
|
||||
export const router = createBrowserRouter(mapRoutes(appRoutes));
|
||||
Reference in New Issue
Block a user