|
| 1 | +import { |
| 2 | + Button, |
| 3 | + Menu, |
| 4 | + Image, |
| 5 | + Container, |
| 6 | + Group, |
| 7 | + Burger, |
| 8 | + Drawer, |
| 9 | +} from '@mantine/core'; |
| 10 | +import { useDisclosure } from '@mantine/hooks'; |
| 11 | +import { NavLink, useLocation, useNavigate } from 'react-router'; |
| 12 | +import { |
| 13 | + IconHome2, |
| 14 | + IconLogout, |
| 15 | + IconChevronDown, |
| 16 | + IconUsers, |
| 17 | +} from '@tabler/icons-react'; |
| 18 | +import { useTranslation } from 'react-i18next'; |
| 19 | +import LogoDark from '/logo-dark.webp'; |
| 20 | +import { useUserState } from '@internal/core/states/use-user-state'; |
| 21 | +import { AdminGuard } from '../admin-guard'; |
| 22 | +import styles from './app-header.module.css'; |
| 23 | + |
| 24 | +interface HeaderState { |
| 25 | + title?: string; |
| 26 | + subtitle?: string; |
| 27 | + showTitle?: boolean; |
| 28 | +} |
| 29 | + |
| 30 | +export function AppHeader() { |
| 31 | + const navigate = useNavigate(); |
| 32 | + const { t } = useTranslation('menu'); |
| 33 | + const { clear } = useUserState(); |
| 34 | + const { pathname } = useLocation(); |
| 35 | + |
| 36 | + const handleLogout = () => { |
| 37 | + clear(); |
| 38 | + navigate('/login'); |
| 39 | + }; |
| 40 | + |
| 41 | + const titles: Record<string, HeaderState> = { |
| 42 | + '/app': { |
| 43 | + title: t('dashboard'), |
| 44 | + subtitle: t('home_subtitle'), |
| 45 | + showTitle: true, |
| 46 | + }, |
| 47 | + '/admin/users': { |
| 48 | + title: t('users'), |
| 49 | + subtitle: t('manage_users'), |
| 50 | + showTitle: true, |
| 51 | + }, |
| 52 | + }; |
| 53 | + |
| 54 | + return ( |
| 55 | + <header className={styles.header}> |
| 56 | + <Container size="xl" className={styles.container}> |
| 57 | + <Group justify="space-between" h="100%"> |
| 58 | + <Group gap="lg"> |
| 59 | + <Image src={LogoDark} w={60} h={40} fit="contain" /> |
| 60 | + |
| 61 | + <div className={styles.titleSection}> |
| 62 | + <h1 className={styles.title}>{titles[pathname]?.title}</h1> |
| 63 | + <p className={styles.subtitle}>{titles[pathname]?.subtitle}</p> |
| 64 | + </div> |
| 65 | + </Group> |
| 66 | + |
| 67 | + <Group gap="md" visibleFrom="sm"> |
| 68 | + <Button |
| 69 | + component={NavLink} |
| 70 | + to="/app" |
| 71 | + variant="subtle" |
| 72 | + leftSection={<IconHome2 size={18} stroke={1.5} />} |
| 73 | + className={styles.navButton} |
| 74 | + > |
| 75 | + {t('dashboard')} |
| 76 | + </Button> |
| 77 | + |
| 78 | + <AdminGuard> |
| 79 | + <Menu shadow="md" width={200}> |
| 80 | + <Menu.Target> |
| 81 | + <Button |
| 82 | + variant="subtle" |
| 83 | + rightSection={<IconChevronDown size={18} stroke={1.5} />} |
| 84 | + className={styles.navButton} |
| 85 | + > |
| 86 | + {t('administration')} |
| 87 | + </Button> |
| 88 | + </Menu.Target> |
| 89 | + |
| 90 | + <Menu.Dropdown> |
| 91 | + <Menu.Item |
| 92 | + component={NavLink} |
| 93 | + to="/admin/users" |
| 94 | + leftSection={<IconUsers size={16} stroke={1.5} />} |
| 95 | + > |
| 96 | + {t('users')} |
| 97 | + </Menu.Item> |
| 98 | + </Menu.Dropdown> |
| 99 | + </Menu> |
| 100 | + </AdminGuard> |
| 101 | + |
| 102 | + <Button |
| 103 | + variant="subtle" |
| 104 | + onClick={handleLogout} |
| 105 | + color="red" |
| 106 | + leftSection={<IconLogout size={18} stroke={1.5} />} |
| 107 | + > |
| 108 | + {t('logout')} |
| 109 | + </Button> |
| 110 | + </Group> |
| 111 | + </Group> |
| 112 | + </Container> |
| 113 | + </header> |
| 114 | + ); |
| 115 | +} |
0 commit comments