header fixes
This commit is contained in:
@@ -1,4 +1,8 @@
|
|||||||
import * as React from "react";
|
import * as React from "react";
|
||||||
|
import {
|
||||||
|
useLocation,
|
||||||
|
matchPath
|
||||||
|
} from "react-router-dom";
|
||||||
import {
|
import {
|
||||||
AppBar,
|
AppBar,
|
||||||
Toolbar,
|
Toolbar,
|
||||||
@@ -16,14 +20,23 @@ import { useNavigate } from "react-router-dom";
|
|||||||
import { useAuth } from "../react-auth";
|
import { useAuth } from "../react-auth";
|
||||||
|
|
||||||
interface HeaderProps {
|
interface HeaderProps {
|
||||||
headerTitle: string;
|
routerMapping: {
|
||||||
onDrawerToggle?: () => void; // optional (only used where needed)
|
path: string;
|
||||||
|
headerTitle: string;
|
||||||
|
}[];
|
||||||
|
onDrawerToggle?: () => void;
|
||||||
}
|
}
|
||||||
|
|
||||||
export default function Header({
|
export default function Header({
|
||||||
headerTitle,
|
routerMapping,
|
||||||
onDrawerToggle,
|
onDrawerToggle,
|
||||||
}: HeaderProps) {
|
}: HeaderProps) {
|
||||||
|
const location = useLocation();
|
||||||
|
const matchedRoute = routerMapping.find((route) =>
|
||||||
|
matchPath({ path: route.path, end: false }, location.pathname)
|
||||||
|
);
|
||||||
|
const headerTitle = matchedRoute?.headerTitle ?? "Khata";
|
||||||
|
|
||||||
const navigate = useNavigate();
|
const navigate = useNavigate();
|
||||||
const { currentUser, logout } = useAuth();
|
const { currentUser, logout } = useAuth();
|
||||||
|
|
||||||
|
|||||||
25
src/main.jsx
25
src/main.jsx
@@ -27,21 +27,38 @@ const rootElement = document.getElementById('root');
|
|||||||
const root = createRoot(rootElement);
|
const root = createRoot(rootElement);
|
||||||
const AUTH_BASE = import.meta.env.VITE_AUTH_BASE_URL;
|
const AUTH_BASE = import.meta.env.VITE_AUTH_BASE_URL;
|
||||||
|
|
||||||
|
const routerMapping = [
|
||||||
|
{ path: "/", component: Home, headerTitle: "Home" },
|
||||||
|
{ path: "/home", component: Home, headerTitle: "Home" },
|
||||||
|
{ path: "/admin/*", component: Admin, headerTitle: "Admin" },
|
||||||
|
];
|
||||||
|
|
||||||
root.render(
|
root.render(
|
||||||
<BrowserRouter>
|
<BrowserRouter>
|
||||||
<AuthProvider authBaseUrl={AUTH_BASE}>
|
<AuthProvider authBaseUrl={AUTH_BASE}>
|
||||||
<AppTheme>
|
<AppTheme>
|
||||||
<CssBaseline enableColorScheme />
|
<CssBaseline enableColorScheme />
|
||||||
<Header />
|
<Header routerMapping={routerMapping} />
|
||||||
|
|
||||||
<Box sx={{ pb: 8 }}>
|
<Box sx={{ pb: 8 }}>
|
||||||
<Toolbar />
|
<Toolbar />
|
||||||
|
|
||||||
<Routes>
|
<Routes>
|
||||||
<Route path="/" element={<Home />} />
|
{routerMapping.map(({ path, component: Component }) => (
|
||||||
<Route path="/home" element={<Home />} />
|
<Route
|
||||||
<Route path="/admin/*" element={<Admin basePath="/admin" />} />
|
key={path}
|
||||||
|
path={path}
|
||||||
|
element={
|
||||||
|
path.startsWith("/admin") ? (
|
||||||
|
<Component basePath="/admin" />
|
||||||
|
) : (
|
||||||
|
<Component />
|
||||||
|
)
|
||||||
|
}
|
||||||
|
/>
|
||||||
|
))}
|
||||||
</Routes>
|
</Routes>
|
||||||
|
|
||||||
</Box>
|
</Box>
|
||||||
|
|
||||||
<Footer />
|
<Footer />
|
||||||
|
|||||||
Reference in New Issue
Block a user