admin_ruspet/src/components/Layout/MainLayout.tsx

68 lines
2.4 KiB
TypeScript

import { AppShell } from '@mantine/core'
import { useDisclosure } from '@mantine/hooks'
import { Spotlight } from '@mantine/spotlight'
import { Outlet, useLocation, useNavigate } from 'react-router-dom'
import { useMemo } from 'react'
import Sidebar from './Sidebar'
import Header from './Header'
import ErrorBoundary from '../DiagnosticErrorBoundary'
import { useAuth } from '@/hooks/useAuth'
import { navigation, flattenNav } from './navigation'
export default function MainLayout() {
const [mobileOpened, { toggle: toggleMobile, close: closeMobile }] = useDisclosure()
const [desktopOpened, { toggle: toggleDesktop }] = useDisclosure(true)
const location = useLocation()
const navigate = useNavigate()
const { user } = useAuth()
const actions = useMemo(
() =>
flattenNav(navigation, user?.is_observer === true).map((i) => ({
id: i.path,
label: i.label,
description: i.trail.slice(0, -1).join(' / '),
leftSection: i.icon ? <i.icon size={20} stroke={1.5} /> : undefined,
onClick: () => { navigate(i.path); closeMobile() },
})),
[user?.is_observer, navigate, closeMobile],
)
return (
<AppShell
header={{ height: 56 }}
navbar={{
width: 264,
breakpoint: 'sm',
collapsed: { mobile: !mobileOpened, desktop: !desktopOpened },
}}
padding="lg"
>
<AppShell.Header>
<Header
toggleMobile={toggleMobile}
toggleDesktop={toggleDesktop}
mobileOpened={mobileOpened}
desktopOpened={desktopOpened}
/>
</AppShell.Header>
<AppShell.Navbar>
<Sidebar />
</AppShell.Navbar>
<AppShell.Main miw={0} style={{ background: 'var(--app-bg)' }}>
<ErrorBoundary key={location.pathname}>
<Outlet />
</ErrorBoundary>
</AppShell.Main>
<Spotlight
actions={actions}
shortcut={['mod + K', '/']}
nothingFound="Ничего не найдено"
searchProps={{ placeholder: 'Перейти к разделу…' }}
/>
</AppShell>
)
}