import React, { ReactNode, SyntheticEvent, useCallback, useEffect, useState } from "react";
import { Close, Dashboard, Menu, Security, SystemSecurityUpdateGood } from "@mui/icons-material";
import {
AppBar,
Box,
Divider,
List,
ListItem,
ListItemButton,
ListItemIcon,
ListItemText,
SwipeableDrawer,
Toolbar,
Typography,
} from "@mui/material";
import IconButton from "@mui/material/IconButton";
import { useTranslation } from "react-i18next";
import {
IndexRoute,
SecuritySubRoute,
SettingsRoute,
SettingsTwoFactorAuthenticationSubRoute,
} from "@constants/Routes";
import { useRouterNavigate } from "@hooks/RouterNavigate";
export interface Props {
id?: string;
children?: ReactNode;
title?: string;
titlePrefix?: string;
drawerWidth?: number;
}
const defaultDrawerWidth = 240;
const SettingsLayout = function (props: Props) {
const { t: translate } = useTranslation("settings");
const [drawerOpen, setDrawerOpen] = useState(false);
useEffect(() => {
if (props.title) {
if (props.titlePrefix) {
document.title = `${props.titlePrefix} - ${props.title} - Authelia`;
} else {
document.title = `${props.title} - Authelia`;
}
} else {
if (props.titlePrefix) {
document.title = `${props.titlePrefix} - ${translate("Settings")} - Authelia`;
} else {
document.title = `${translate("Settings")} - Authelia`;
}
}
}, [props.title, props.titlePrefix, translate]);
const drawerWidth = props.drawerWidth === undefined ? defaultDrawerWidth : props.drawerWidth;
const handleToggleDrawer = (event: SyntheticEvent) => {
if (
event.nativeEvent instanceof KeyboardEvent &&
event.nativeEvent.type === "keydown" &&
(event.nativeEvent.key === "Tab" || event.nativeEvent.key === "Shift")
) {
return;
}
setDrawerOpen((state) => !state);
};
const container = window !== undefined ? () => window.document.body : undefined;
const drawer = (
{translate("Settings")}
{navItems.map((item) => (
))}
);
return (
{translate("Settings")}
{drawer}
{props.children}
);
};
interface NavItem {
keyname: string;
text: string;
pathname: string;
icon?: ReactNode;
}
const navItems: NavItem[] = [
{ keyname: "overview", text: "Overview", pathname: SettingsRoute, icon: },
{
keyname: "security",
text: "Security",
pathname: `${SettingsRoute}${SecuritySubRoute}`,
icon: ,
},
{
keyname: "twofactor",
text: "Two-Factor Authentication",
pathname: `${SettingsRoute}${SettingsTwoFactorAuthenticationSubRoute}`,
icon: ,
},
{ keyname: "close", text: "Close", pathname: IndexRoute, icon: },
];
const DrawerNavItem = function (props: NavItem) {
const selected = window.location.pathname === props.pathname || window.location.pathname === props.pathname + "/";
const navigate = useRouterNavigate();
const handleOnClick = useCallback(() => {
if (selected) {
return;
}
navigate(props.pathname);
}, [navigate, props, selected]);
return (
);
};
export default SettingsLayout;