112 lines
4.6 KiB
TypeScript
112 lines
4.6 KiB
TypeScript
import {
|
||
Group, Burger, Text, Avatar, Menu, ActionIcon, Button, Kbd, Breadcrumbs, Box, UnstyledButton,
|
||
useMantineColorScheme, useComputedColorScheme,
|
||
} from '@mantine/core'
|
||
import { spotlight } from '@mantine/spotlight'
|
||
import { IconLogout, IconSearch, IconSun, IconMoon, IconUser } from '@tabler/icons-react'
|
||
import { useLocation } from 'react-router-dom'
|
||
import { useAuth } from '@/hooks/useAuth'
|
||
import { findTrail } from './navigation'
|
||
|
||
interface HeaderProps {
|
||
toggleMobile: () => void
|
||
toggleDesktop: () => void
|
||
mobileOpened: boolean
|
||
desktopOpened: boolean
|
||
}
|
||
|
||
function ColorSchemeToggle() {
|
||
const { setColorScheme } = useMantineColorScheme()
|
||
const computed = useComputedColorScheme('light', { getInitialValueInEffect: true })
|
||
return (
|
||
<ActionIcon
|
||
size="lg"
|
||
aria-label="Переключить тему"
|
||
onClick={() => setColorScheme(computed === 'light' ? 'dark' : 'light')}
|
||
>
|
||
{computed === 'dark' ? <IconSun size={18} stroke={1.6} /> : <IconMoon size={18} stroke={1.6} />}
|
||
</ActionIcon>
|
||
)
|
||
}
|
||
|
||
export default function Header({ toggleMobile, toggleDesktop, mobileOpened, desktopOpened }: HeaderProps) {
|
||
const { user, logout } = useAuth()
|
||
const { pathname } = useLocation()
|
||
const trail = findTrail(pathname)
|
||
const role = user?.is_root ? 'Суперадмин' : user?.is_observer ? 'Наблюдатель' : 'Администратор'
|
||
|
||
return (
|
||
<Group h="100%" px="md" justify="space-between" wrap="nowrap">
|
||
<Group gap="sm" wrap="nowrap">
|
||
<Burger opened={mobileOpened} onClick={toggleMobile} hiddenFrom="sm" size="sm" />
|
||
<Burger opened={desktopOpened} onClick={toggleDesktop} visibleFrom="sm" size="sm" />
|
||
|
||
<Group gap={8} wrap="nowrap">
|
||
<Box
|
||
w={28} h={28}
|
||
style={{
|
||
borderRadius: 8, display: 'grid', placeItems: 'center',
|
||
background: 'var(--mantine-primary-color-filled)', color: '#fff',
|
||
fontWeight: 700, fontSize: 14,
|
||
}}
|
||
>
|
||
Р
|
||
</Box>
|
||
<Text fw={600} size="sm" visibleFrom="xs">Портал РусПЭТ</Text>
|
||
</Group>
|
||
|
||
{trail.length > 0 && (
|
||
<Breadcrumbs separator="/" visibleFrom="md" ml="md">
|
||
{trail.map((t, i) => (
|
||
<Text key={i} size="sm" c={i === trail.length - 1 ? undefined : 'dimmed'} fw={i === trail.length - 1 ? 500 : 400}>
|
||
{t}
|
||
</Text>
|
||
))}
|
||
</Breadcrumbs>
|
||
)}
|
||
</Group>
|
||
|
||
<Group gap="xs" wrap="nowrap">
|
||
<Button
|
||
variant="default"
|
||
size="xs"
|
||
visibleFrom="sm"
|
||
leftSection={<IconSearch size={14} />}
|
||
rightSection={<Kbd size="xs">Ctrl K</Kbd>}
|
||
onClick={spotlight.open}
|
||
c="dimmed"
|
||
fw={400}
|
||
w={220}
|
||
justify="space-between"
|
||
>
|
||
Поиск
|
||
</Button>
|
||
<ActionIcon size="lg" hiddenFrom="sm" onClick={spotlight.open} aria-label="Поиск">
|
||
<IconSearch size={18} stroke={1.6} />
|
||
</ActionIcon>
|
||
|
||
<ColorSchemeToggle />
|
||
|
||
<Menu width={220} position="bottom-end">
|
||
<Menu.Target>
|
||
<UnstyledButton aria-label="Профиль">
|
||
<Avatar size="sm" radius="xl" color="brand">
|
||
{user?.login?.charAt(0).toUpperCase() || 'A'}
|
||
</Avatar>
|
||
</UnstyledButton>
|
||
</Menu.Target>
|
||
<Menu.Dropdown>
|
||
<Box px="sm" py={6}>
|
||
<Text size="sm" fw={600}>{user?.login || 'Admin'}</Text>
|
||
<Text size="xs" c="dimmed">{role}</Text>
|
||
</Box>
|
||
<Menu.Divider />
|
||
<Menu.Item leftSection={<IconLogout size={14} />} color="red" onClick={logout}>
|
||
Выйти
|
||
</Menu.Item>
|
||
</Menu.Dropdown>
|
||
</Menu>
|
||
</Group>
|
||
</Group>
|
||
)
|
||
} |