|
| 1 | +import { |
| 2 | + Anchor, |
| 3 | + AppShell, |
| 4 | + Breadcrumbs, |
| 5 | + Burger, |
| 6 | + Group, |
| 7 | + Stack, |
| 8 | +} from "@mantine/core"; |
| 9 | +import { useDisclosure } from "@mantine/hooks"; |
| 10 | +import { IconHome, IconUsersGroup } from "@tabler/icons-react"; |
| 11 | +import { useMemo } from "react"; |
| 12 | +import { Link, useLocation } from "react-router-dom"; |
| 13 | + |
| 14 | +interface AppLayoutProps { |
| 15 | + children: React.ReactNode; |
| 16 | +} |
| 17 | + |
| 18 | +const links = [ |
| 19 | + { |
| 20 | + icon: <IconHome size="1rem" />, |
| 21 | + color: "blue", |
| 22 | + label: "Home Feed", |
| 23 | + href: "/", |
| 24 | + }, |
| 25 | + { |
| 26 | + icon: <IconUsersGroup size="1rem" />, |
| 27 | + color: "teal", |
| 28 | + label: "Users", |
| 29 | + href: "/users", |
| 30 | + }, |
| 31 | +]; |
| 32 | + |
| 33 | +export function AppLayout({ children }: AppLayoutProps) { |
| 34 | + const { pathname } = useLocation(); |
| 35 | + |
| 36 | + const breadCrumbLinks = useMemo(() => { |
| 37 | + const routes = pathname.split("/"); |
| 38 | + routes.shift(); |
| 39 | + const links: string[] = []; |
| 40 | + for (let i = 0; i < routes.length + 1; i++) { |
| 41 | + if (routes[i] && routes[i] !== "/") |
| 42 | + if (routes[i] === "posts") { |
| 43 | + links.push(`/`); |
| 44 | + } else links.push(`/${routes.slice(0, i + 1).join("/")}`); |
| 45 | + } |
| 46 | + return links; |
| 47 | + }, [pathname]); |
| 48 | + |
| 49 | + if (breadCrumbLinks.length === 1) { |
| 50 | + breadCrumbLinks.unshift("/"); |
| 51 | + } |
| 52 | + |
| 53 | + const [opened, { toggle }] = useDisclosure(); |
| 54 | + |
| 55 | + return ( |
| 56 | + <AppShell |
| 57 | + header={{ height: 60 }} |
| 58 | + navbar={{ width: 300, breakpoint: "sm", collapsed: { mobile: !opened } }} |
| 59 | + padding="xl" |
| 60 | + > |
| 61 | + <AppShell.Header> |
| 62 | + <Group h="100%" px="md"> |
| 63 | + <Burger opened={opened} onClick={toggle} size="sm" /> |
| 64 | + Vite with React Query |
| 65 | + </Group> |
| 66 | + </AppShell.Header> |
| 67 | + <AppShell.Navbar p="md"> |
| 68 | + {links.map((link) => ( |
| 69 | + <Anchor component={Link} key={link.label} to={link.href}> |
| 70 | + {link.label} |
| 71 | + </Anchor> |
| 72 | + ))} |
| 73 | + </AppShell.Navbar> |
| 74 | + <AppShell.Main mb="xl"> |
| 75 | + <Stack gap="md" mt="lg"> |
| 76 | + <Breadcrumbs aria-label="breadcrumb" pb="md"> |
| 77 | + {breadCrumbLinks.map((link, index) => ( |
| 78 | + <Anchor |
| 79 | + c="inherit" |
| 80 | + component={Link} |
| 81 | + key={index} |
| 82 | + td="none" |
| 83 | + to={link} |
| 84 | + tt="capitalize" |
| 85 | + style={{ |
| 86 | + cursor: "pointer", |
| 87 | + }} |
| 88 | + > |
| 89 | + {link === "/" ? "Home Feed" : link.split("/").pop()} |
| 90 | + </Anchor> |
| 91 | + ))} |
| 92 | + </Breadcrumbs> |
| 93 | + {children} |
| 94 | + </Stack> |
| 95 | + </AppShell.Main> |
| 96 | + </AppShell> |
| 97 | + ); |
| 98 | +} |
0 commit comments