sonda/web/src/pages/admin/AdminLayout.tsx
tgiachi 52569a0f8d feat(web): a Users section in the admin area
Opens at /admin/users next to Libraries, for admins only; anyone else gets the notice and the server is not asked. README says what the admin area covers.
2026-11-12 14:58:01 +01:00

44 lines
1.6 KiB
TypeScript

import { useTranslation } from 'react-i18next'
import { NavLink, Outlet } from 'react-router-dom'
import { useSession } from '@/auth/session'
import { EmptyMessage } from '@/components/Status'
import { cn } from '@/lib/utils'
/** The sections of the admin area; each is a route under `/admin`. */
const SECTIONS = [
{ to: '/admin/libraries', key: 'admin.sectionLibraries' },
{ to: '/admin/users', key: 'admin.sectionUsers' },
]
/**
* The frame of the admin area. Only an admin gets the sections: anyone else gets a notice, and since the
* section pages are never rendered for them, nothing is asked of the server (which would say 403 anyway).
*/
export default function AdminLayout() {
const { t } = useTranslation()
const { user } = useSession()
if (!user?.is_admin) return <EmptyMessage>{t('admin.adminOnly')}</EmptyMessage>
return (
<div className="flex flex-col gap-6">
<h1 className="text-3xl font-extrabold tracking-tight">{t('admin.title')}</h1>
<nav aria-label={t('admin.sections')} className="flex flex-wrap gap-2">
{SECTIONS.map((section) => (
<NavLink
key={section.to}
to={section.to}
className={({ isActive }) =>
cn(
'flex min-h-11 items-center rounded-lg px-4 font-semibold focus-visible:outline-2 focus-visible:outline-offset-2 focus-visible:outline-ring',
isActive ? 'bg-secondary text-foreground' : 'text-muted-foreground hover:text-foreground',
)
}
>
{t(section.key)}
</NavLink>
))}
</nav>
<Outlet />
</div>
)
}