Skip to content

Commit

Permalink
feat(admin-layout): Improve back button nav
Browse files Browse the repository at this point in the history
  • Loading branch information
Baboo7 committed Mar 2, 2023
1 parent a8f596b commit 2494039
Showing 1 changed file with 17 additions and 5 deletions.
22 changes: 17 additions & 5 deletions packages/client/src/modules/administration/Layout/Layout.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ import ListSubheader from "@mui/material/ListSubheader";
import Typography from "@mui/material/Typography";
import Divider from "@mui/material/Divider";
import Container from "@mui/material/Container";
import { Link, Navigate, Outlet, useMatch } from "react-router-dom";
import { Link, Navigate, Outlet, PathMatch, useMatch } from "react-router-dom";
import GamesIcon from "@mui/icons-material/Games";
import PersonIcon from "@mui/icons-material/Person";
import SchoolIcon from "@mui/icons-material/School";
Expand Down Expand Up @@ -36,7 +36,13 @@ export { Layout };
function Layout() {
const { permissions } = useAuth();
const theme = useTheme();
const isGameAdministrationRoute = useMatch(`administration/games/:gameId/*`);
const routeMatch = useMatch(`administration/*`);

const isNestedRoute = useMemo(() => {
return (
routeMatch && routeMatch?.pathname.split("/").filter(Boolean).length > 2
);
}, [routeMatch]);

if (!permissions.canAccessAdminPanel) {
return <Navigate to="/" />;
Expand All @@ -50,7 +56,9 @@ function Layout() {
pr: "24px", // keep right padding when drawer closed
}}
>
{isGameAdministrationRoute ? renderBackButton() : <></>}
{isNestedRoute ? (
<BackButton to={buildPathToPreviousPage(routeMatch)} />
) : null}
<Typography
component="h1"
variant="h6"
Expand Down Expand Up @@ -109,10 +117,14 @@ function Layout() {
);
}

function renderBackButton(): JSX.Element {
function buildPathToPreviousPage(match: PathMatch | null) {
return match?.pathname.split("/").slice(0, -1).join("/") || "";
}

function BackButton({ to }: { to: string }): JSX.Element {
return (
<>
<Button component={Link} to="/administration/games" sx={{ mr: 2 }}>
<Button component={Link} to={to} sx={{ mr: 2 }}>
<ArrowBackIosNewIcon sx={{ height: "1rem" }} /> Retour
</Button>
<Divider
Expand Down

0 comments on commit 2494039

Please sign in to comment.