AdminLayout.tsx 9.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300
  1. import {
  2. ApiOutlined,
  3. CheckCircleOutlined,
  4. CloudServerOutlined,
  5. CloseOutlined,
  6. DashboardOutlined,
  7. GlobalOutlined,
  8. InboxOutlined,
  9. KeyOutlined,
  10. MailOutlined,
  11. MenuOutlined,
  12. SafetyCertificateOutlined,
  13. SendOutlined,
  14. SettingOutlined,
  15. UserOutlined,
  16. WarningOutlined
  17. } from '@ant-design/icons';
  18. import {
  19. Avatar,
  20. Button,
  21. Drawer,
  22. Dropdown,
  23. Layout,
  24. Menu,
  25. Select,
  26. Space,
  27. Tag,
  28. Tooltip,
  29. Typography
  30. } from 'antd';
  31. import { useEffect, useMemo, useRef, useState, type ReactNode } from 'react';
  32. import { Outlet, useLocation, useNavigate } from 'react-router-dom';
  33. import { useAppContext } from '../frontend/app-context';
  34. import { useI18n } from '../frontend/i18n/react';
  35. import { useMediaQuery } from '../frontend/use-media-query';
  36. const { Header, Sider, Content } = Layout;
  37. interface NavigationItem {
  38. path: string;
  39. labelKey: string;
  40. icon: ReactNode;
  41. adminOnly?: boolean;
  42. }
  43. interface NavigationGroup {
  44. key: string;
  45. labelKey: string;
  46. items: NavigationItem[];
  47. }
  48. export const navigationGroups: NavigationGroup[] = [
  49. {
  50. key: 'operations',
  51. labelKey: 'nav.group.operations',
  52. items: [
  53. { path: '/overview', labelKey: 'nav.overview', icon: <DashboardOutlined /> },
  54. { path: '/activity', labelKey: 'nav.activity', icon: <SendOutlined /> },
  55. { path: '/domains', labelKey: 'nav.domains', icon: <GlobalOutlined /> },
  56. { path: '/inbox', labelKey: 'nav.inbox', icon: <InboxOutlined /> }
  57. ]
  58. },
  59. {
  60. key: 'integrations',
  61. labelKey: 'nav.group.integrations',
  62. items: [
  63. { path: '/integrations/smtp', labelKey: 'nav.smtpIntegration', icon: <MailOutlined /> },
  64. { path: '/integrations/api-keys', labelKey: 'nav.apiKeys', icon: <KeyOutlined /> },
  65. { path: '/integrations/webhooks', labelKey: 'nav.webhooks', icon: <ApiOutlined /> },
  66. { path: '/integrations/dns', labelKey: 'nav.dnsIntegration', icon: <CloudServerOutlined /> }
  67. ]
  68. },
  69. {
  70. key: 'system',
  71. labelKey: 'nav.group.system',
  72. items: [
  73. { path: '/admin/users', labelKey: 'nav.adminCenter', icon: <SafetyCertificateOutlined />, adminOnly: true },
  74. { path: '/settings', labelKey: 'nav.settings', icon: <SettingOutlined />, adminOnly: true }
  75. ]
  76. }
  77. ];
  78. export function visibleNavigation(isAdmin: boolean) {
  79. return navigationGroups
  80. .map((group) => ({
  81. ...group,
  82. items: group.items.filter((item) => !item.adminOnly || isAdmin)
  83. }))
  84. .filter((group) => group.items.length > 0);
  85. }
  86. export function navigationSelection(pathname: string) {
  87. if (pathname.startsWith('/activity')) return '/activity';
  88. if (pathname.startsWith('/domains')) return '/domains';
  89. if (pathname.startsWith('/inbox')) return '/inbox';
  90. if (pathname.startsWith('/integrations/smtp')) return '/integrations/smtp';
  91. if (pathname.startsWith('/integrations/api-keys')) return '/integrations/api-keys';
  92. if (pathname.startsWith('/integrations/webhooks')) return '/integrations/webhooks';
  93. if (pathname.startsWith('/integrations/dns')) return '/integrations/dns';
  94. if (pathname.startsWith('/admin')) return '/admin/users';
  95. if (pathname.startsWith('/settings')) return '/settings';
  96. return '/overview';
  97. }
  98. export function AdminLayout() {
  99. const { locale, locales, setLocale, t } = useI18n();
  100. const { user, config, logout } = useAppContext();
  101. const location = useLocation();
  102. const navigate = useNavigate();
  103. const isDesktop = useMediaQuery('(min-width: 1024px)');
  104. const [mobileNavigationOpen, setMobileNavigationOpen] = useState(false);
  105. const mainRef = useRef<HTMLElement>(null);
  106. const isAdmin = user?.role === 'admin';
  107. useEffect(() => {
  108. setMobileNavigationOpen(false);
  109. const frame = window.requestAnimationFrame(() => mainRef.current?.focus({ preventScroll: true }));
  110. return () => window.cancelAnimationFrame(frame);
  111. }, [location.pathname]);
  112. useEffect(() => {
  113. if (isDesktop) setMobileNavigationOpen(false);
  114. }, [isDesktop]);
  115. const menuItems = useMemo(() => visibleNavigation(isAdmin).map((group) => ({
  116. type: 'group' as const,
  117. key: group.key,
  118. label: t(group.labelKey),
  119. children: group.items.map((item) => ({
  120. key: item.path,
  121. icon: item.icon,
  122. label: t(item.labelKey)
  123. }))
  124. })), [isAdmin, t]);
  125. const menu = (
  126. <Menu
  127. theme="dark"
  128. mode="inline"
  129. selectedKeys={[navigationSelection(location.pathname)]}
  130. items={menuItems}
  131. onClick={({ key }) => navigate(key)}
  132. className="admin-menu"
  133. />
  134. );
  135. const environmentReady = Boolean(config?.mailHostname && config?.sendingIp);
  136. const environmentDetails = config
  137. ? `${config.mailHostname} · ${config.sendingIp || t('common.unsetSendingIp')}`
  138. : t('common.loadingConfig');
  139. return (
  140. <Layout className="admin-layout">
  141. <a className="skip-link" href="#main-content">{t('shell.skipToMain')}</a>
  142. <Sider width={252} className="admin-sider desktop-sider" trigger={null}>
  143. <NavigationPanel menu={menu} userName={user?.username} role={user?.role} />
  144. </Sider>
  145. <Drawer
  146. className="mobile-navigation-drawer"
  147. placement="left"
  148. width={288}
  149. open={mobileNavigationOpen && !isDesktop}
  150. onClose={() => setMobileNavigationOpen(false)}
  151. closable={false}
  152. styles={{ body: { padding: 0, background: '#0F172A' } }}
  153. aria-label={t('shell.mainNavigation')}
  154. >
  155. <NavigationPanel
  156. menu={menu}
  157. userName={user?.username}
  158. role={user?.role}
  159. onClose={() => setMobileNavigationOpen(false)}
  160. closeLabel={t('shell.closeNavigation')}
  161. />
  162. </Drawer>
  163. <Layout className="admin-main-layout">
  164. <Header className="admin-header">
  165. <Space size={12} className="header-status">
  166. <Button
  167. className="mobile-nav-trigger"
  168. type="text"
  169. icon={<MenuOutlined />}
  170. aria-label={t('shell.openNavigation')}
  171. onClick={() => setMobileNavigationOpen(true)}
  172. />
  173. <Tooltip title={environmentDetails}>
  174. <Tag
  175. className="environment-status"
  176. aria-label={environmentReady ? t('shell.envReady') : t('shell.envNeedsSetup')}
  177. icon={environmentReady ? <CheckCircleOutlined /> : <WarningOutlined />}
  178. color={environmentReady ? 'success' : 'warning'}
  179. >
  180. <span className="environment-status__label">
  181. {environmentReady ? t('shell.envReady') : t('shell.envNeedsSetup')}
  182. </span>
  183. </Tag>
  184. </Tooltip>
  185. </Space>
  186. <Space size={8} className="header-actions">
  187. <Select
  188. aria-label="Language"
  189. value={locale}
  190. options={locales}
  191. onChange={setLocale}
  192. className="language-select"
  193. />
  194. <Dropdown
  195. trigger={['click']}
  196. menu={{
  197. items: [
  198. {
  199. key: 'account',
  200. label: (
  201. <div className="account-menu-summary">
  202. <Typography.Text strong>{user?.username || t('common.user')}</Typography.Text>
  203. <Typography.Text type="secondary">{user?.email || '—'}</Typography.Text>
  204. </div>
  205. ),
  206. disabled: true
  207. },
  208. ...(isAdmin ? [{ key: 'settings', label: t('nav.settings'), onClick: () => navigate('/settings') }] : []),
  209. { type: 'divider' as const },
  210. { key: 'logout', label: t('common.logout'), onClick: () => void logout() }
  211. ]
  212. }}
  213. >
  214. <Button className="user-button" aria-label={t('shell.accountMenu')}>
  215. <Space size={8}>
  216. <Avatar size={24} icon={<UserOutlined />} />
  217. <span className="user-button__name" title={user?.username || t('common.user')}>
  218. {user?.username || t('common.user')}
  219. </span>
  220. </Space>
  221. </Button>
  222. </Dropdown>
  223. </Space>
  224. </Header>
  225. <Content
  226. id="main-content"
  227. ref={mainRef}
  228. tabIndex={-1}
  229. className="admin-content"
  230. >
  231. <Outlet />
  232. </Content>
  233. </Layout>
  234. </Layout>
  235. );
  236. }
  237. function NavigationPanel({
  238. menu,
  239. userName,
  240. role,
  241. onClose,
  242. closeLabel
  243. }: {
  244. menu: ReactNode;
  245. userName?: string;
  246. role?: string;
  247. onClose?: () => void;
  248. closeLabel?: string;
  249. }) {
  250. return (
  251. <div className="admin-sider-inner">
  252. <div className="brand">
  253. <div className="brand-logo" aria-hidden="true">MH</div>
  254. <div>
  255. <div className="brand-title">MailHub</div>
  256. <div className="brand-subtitle">Delivery Operations</div>
  257. </div>
  258. {onClose ? (
  259. <Button
  260. type="text"
  261. className="mobile-nav-close"
  262. icon={<CloseOutlined />}
  263. aria-label={closeLabel}
  264. style={{ minHeight: 44, minWidth: 44 }}
  265. onClick={onClose}
  266. />
  267. ) : null}
  268. </div>
  269. {menu}
  270. {userName ? (
  271. <div className="admin-sider-footer">
  272. <Avatar size={28} icon={<UserOutlined />} className="sider-user-avatar" />
  273. <div className="sider-user-meta">
  274. <div className="sider-user-name">{userName}</div>
  275. <div className="sider-user-role">{role || '—'}</div>
  276. </div>
  277. </div>
  278. ) : null}
  279. </div>
  280. );
  281. }