import * as React from "react";
import { useLocation, matchPath, useNavigate } from "react-router-dom";
import {
AppBar,
Toolbar,
Typography,
IconButton,
Tooltip,
Button,
Box,
Drawer,
List,
ListItemButton,
ListItemText,
Divider,
useMediaQuery,
useTheme,
} from "@mui/material";
import MenuIcon from "@mui/icons-material/Menu";
import LogoutIcon from "@mui/icons-material/Logout";
import DarkModeIcon from "@mui/icons-material/DarkMode";
import LightModeIcon from "@mui/icons-material/LightMode";
import { useAuth } from "../react-auth";
import { ColorModeContext } from "./shared-theme/AppTheme";
interface HeaderProps {
routerMapping: {
path: string;
headerTitle: string;
}[];
onDrawerToggle?: () => void;
}
const NAV_LINKS = [
{ label: "Home", path: "/" },
{ label: "Expenses", path: "/expenses" },
{ label: "Reports", path: "/reports" },
{ label: "Fetch Requests", path: "/fetch-requests" },
];
function isActive(path: string, locationPath: string): boolean {
if (path === "/") return locationPath === "/" || locationPath === "/home";
return matchPath({ path, end: false }, locationPath) !== null;
}
export default function Header({ routerMapping, onDrawerToggle }: HeaderProps) {
const location = useLocation();
const navigate = useNavigate();
const { currentUser, logout } = useAuth();
const { mode, toggleColorMode } = React.useContext(ColorModeContext);
const theme = useTheme();
const isMobile = useMediaQuery(theme.breakpoints.down("md"));
const [mobileOpen, setMobileOpen] = React.useState(false);
const isAuthenticated = !!currentUser;
const navContent = (
{NAV_LINKS.map((link) => {
const active = isActive(link.path, location.pathname);
return (
);
})}
);
const authSection = (
{isAuthenticated ? (
<>
>
) : (
)}
);
const drawerContent = (
{routerMapping
.filter((r) => !r.path.startsWith("/login") && !r.path.startsWith("/register") && !r.path.startsWith("/profile"))
.map((route) => {
const active = isActive(route.path, location.pathname);
return (
{
navigate(route.path);
setMobileOpen(false);
}}
sx={{
borderRadius: "6px",
mx: 1,
mb: 0.5,
"&.Mui-selected": {
backgroundColor: "action.selected",
},
}}
>
);
})}
{isAuthenticated ? (
<>
navigate("/profile/me")} sx={{ borderRadius: "6px", mx: 1 }}>
>
) : (
navigate("/login")} sx={{ borderRadius: "6px", mx: 1 }}>
)}
);
return (
<>
theme.zIndex.drawer + 1,
backdropFilter: "blur(8px)",
boxShadow: "none",
borderBottom: "1px solid",
borderColor: "divider",
backgroundColor: "background.default",
}}
>
{/* MOBILE MENU */}
{isMobile && (
setMobileOpen(true)}
aria-label="Open navigation"
sx={{ mr: 1 }}
>
)}
{/* BRAND — always leftmost */}
navigate("/")} />
{navContent}
{/* THEME TOGGLE — right side */}
{mode === "dark" ? : }
{authSection}
setMobileOpen(false)}
ModalProps={{ keepMounted: true }}
>
{drawerContent}
>
);
}
function Brand({ onClick }: { onClick?: () => void }) {
return (
K
Khata
);
}