init
This commit is contained in:
@@ -0,0 +1,143 @@
|
||||
import {
|
||||
ApartmentOutlined,
|
||||
BarChartOutlined,
|
||||
CameraOutlined,
|
||||
LogoutOutlined,
|
||||
OrderedListOutlined,
|
||||
UserOutlined,
|
||||
} from '@ant-design/icons'
|
||||
import { Avatar, Button, Layout, Menu, Result, Space, Spin, Tag, Typography } from 'antd'
|
||||
import { Outlet, useLocation } from 'react-router-dom'
|
||||
import { useAuth } from '@/auth/AuthContext'
|
||||
import { ROLE_MANAGE_AREA, ROLE_MANAGE_ATTRACTION } from '@/auth/roles'
|
||||
|
||||
const { Header, Sider, Content } = Layout
|
||||
|
||||
const ROLE_LABELS: Record<string, string> = {
|
||||
[ROLE_MANAGE_AREA]: 'управление зонами',
|
||||
[ROLE_MANAGE_ATTRACTION]: 'управление контентом',
|
||||
}
|
||||
|
||||
export function AdminLayout() {
|
||||
const { ready, authenticated, userName, roles, logout } = useAuth()
|
||||
const location = useLocation()
|
||||
|
||||
if (!ready) {
|
||||
return (
|
||||
<div className="fullscreen-spin">
|
||||
<Spin size="large" tip="Проверка авторизации…">
|
||||
<div style={{ width: 160, height: 60 }} />
|
||||
</Spin>
|
||||
</div>
|
||||
)
|
||||
}
|
||||
|
||||
if (!authenticated) {
|
||||
return (
|
||||
<Result
|
||||
status="warning"
|
||||
title="Не удалось авторизоваться"
|
||||
subTitle="Сервер Keycloak недоступен или сессия истекла. Попробуйте войти снова."
|
||||
extra={
|
||||
<Button type="primary" onClick={logout}>
|
||||
Войти заново
|
||||
</Button>
|
||||
}
|
||||
/>
|
||||
)
|
||||
}
|
||||
|
||||
const hasManageArea = roles.includes(ROLE_MANAGE_AREA)
|
||||
const hasManageAttraction = roles.includes(ROLE_MANAGE_ATTRACTION)
|
||||
|
||||
if (!hasManageArea && !hasManageAttraction) {
|
||||
return (
|
||||
<Result
|
||||
status="403"
|
||||
title="Нет административных прав"
|
||||
subTitle="Вашей учётной записи не назначена роль manage_area или manage_attraction в Keycloak."
|
||||
extra={
|
||||
<Button icon={<LogoutOutlined />} onClick={logout}>
|
||||
Выйти
|
||||
</Button>
|
||||
}
|
||||
/>
|
||||
)
|
||||
}
|
||||
|
||||
const menuItems = [
|
||||
hasManageArea && { key: '/areas', icon: <ApartmentOutlined />, label: 'Зоны' },
|
||||
hasManageAttraction && {
|
||||
key: '/attractions',
|
||||
icon: <CameraOutlined />,
|
||||
label: 'Достопримечательности',
|
||||
},
|
||||
hasManageArea && {
|
||||
key: '/sets',
|
||||
icon: <OrderedListOutlined />,
|
||||
label: 'Наборы',
|
||||
},
|
||||
hasManageArea && { key: '/visits', icon: <BarChartOutlined />, label: 'Визиты' },
|
||||
].filter(Boolean) as { key: string; icon: React.ReactNode; label: string }[]
|
||||
|
||||
const selectedKey =
|
||||
'/' +
|
||||
(location.pathname.split('/')[1] ?? '')
|
||||
|
||||
return (
|
||||
<Layout style={{ minHeight: '100vh' }}>
|
||||
<Header
|
||||
style={{
|
||||
display: 'flex',
|
||||
alignItems: 'center',
|
||||
justifyContent: 'space-between',
|
||||
gap: 16,
|
||||
}}
|
||||
>
|
||||
<Space size="middle">
|
||||
<Typography.Title level={4} style={{ color: '#fff', margin: 0 }}>
|
||||
Городар · Админ-панель
|
||||
</Typography.Title>
|
||||
</Space>
|
||||
<Space size="middle">
|
||||
<Space size={4} wrap>
|
||||
{roles
|
||||
.filter((role) => ROLE_LABELS[role])
|
||||
.map((role) => (
|
||||
<Tag key={role} color="gold">
|
||||
{ROLE_LABELS[role]}
|
||||
</Tag>
|
||||
))}
|
||||
</Space>
|
||||
<Space size={8}>
|
||||
<Avatar size="small" icon={<UserOutlined />} />
|
||||
<Typography.Text style={{ color: 'rgba(255,255,255,0.85)' }}>
|
||||
{userName}
|
||||
</Typography.Text>
|
||||
<Button
|
||||
type="text"
|
||||
icon={<LogoutOutlined />}
|
||||
onClick={logout}
|
||||
style={{ color: 'rgba(255,255,255,0.85)' }}
|
||||
>
|
||||
Выйти
|
||||
</Button>
|
||||
</Space>
|
||||
</Space>
|
||||
</Header>
|
||||
<Layout>
|
||||
<Sider theme="light" width={220}>
|
||||
<Menu
|
||||
mode="inline"
|
||||
selectedKeys={[selectedKey]}
|
||||
items={menuItems}
|
||||
style={{ height: '100%', borderRight: 0, paddingTop: 8 }}
|
||||
/>
|
||||
</Sider>
|
||||
<Content style={{ padding: 24 }}>
|
||||
<Outlet />
|
||||
</Content>
|
||||
</Layout>
|
||||
</Layout>
|
||||
)
|
||||
}
|
||||
Reference in New Issue
Block a user