Files
FitOS/admin/src/components/Layout.tsx

187 lines
5.9 KiB
TypeScript
Raw Normal View History

import { useState } from 'react';
import { NavLink, Outlet, useNavigate } from 'react-router-dom';
import {
Building2,
Dumbbell,
LogOut,
ScrollText,
ShieldCheck,
Target,
Users,
Wrench,
X,
type LucideIcon,
} from 'lucide-react';
import { useAuth } from '../store/auth';
import { Button, ThemeToggle } from './ui';
import { Topbar } from '../topbar';
import { Brandmark } from './Brandmark';
import { Avatar } from './Avatar';
import { cn } from '../lib/utils';
interface NavItem {
to: string;
label: string;
icon: LucideIcon;
end: boolean;
}
interface NavSection {
label: string;
items: NavItem[];
}
const SECTIONS: NavSection[] = [
{
label: 'Zarządzanie',
items: [
{ to: '/gyms', label: 'Siłownie', icon: Building2, end: false },
{ to: '/verifications', label: 'Weryfikacje', icon: ShieldCheck, end: false },
{ to: '/users', label: 'Użytkownicy', icon: Users, end: false },
],
},
{
label: 'Słowniki',
items: [
{ to: '/focus-areas', label: 'Obszary treningowe', icon: Target, end: false },
{ to: '/exercises', label: 'Rodzaje ćwiczeń', icon: Dumbbell, end: false },
{ to: '/equipment', label: 'Urządzenia', icon: Wrench, end: false },
],
},
{
label: 'System',
items: [{ to: '/logs', label: 'Logi', icon: ScrollText, end: true }],
},
];
/* Wiersze nawigacji wg Ember: aktywny = tint akcentu + pionowy pasek przy krawędzi. */
function NavLinks({ items, onNavigate }: { items: NavItem[]; onNavigate?: () => void }) {
return (
<nav className="flex flex-col gap-0.5 px-3" aria-label="Nawigacja główna">
{items.map((item) => (
<NavLink
key={item.to}
to={item.to}
end={item.end}
onClick={onNavigate}
className={({ isActive }) =>
cn(
'relative flex h-[38px] items-center gap-2.5 rounded-[10px] px-3 text-[13.5px] font-semibold transition-colors duration-150',
isActive ? 'bg-accent/11 text-accent-text' : 'text-dim hover:bg-line/30 hover:text-ink',
)
}
>
{({ isActive }) => (
<>
{isActive && (
<span
aria-hidden="true"
className="absolute -left-3 bottom-[9px] top-[9px] w-[3px] rounded-full bg-(image:--nav-bar-grad)"
/>
)}
<item.icon className="h-[17px] w-[17px] shrink-0" strokeWidth={1.9} />
<span className="min-w-0 truncate">{item.label}</span>
</>
)}
</NavLink>
))}
</nav>
);
}
function NavSections({ onNavigate }: { onNavigate?: () => void }) {
return (
<div className="flex flex-col gap-4">
{SECTIONS.map((section) => (
<div key={section.label}>
<div className="mb-1 px-6 text-[10.5px] font-bold uppercase tracking-[0.13em] text-dim/80">
{section.label}
</div>
<NavLinks items={section.items} onNavigate={onNavigate} />
</div>
))}
</div>
);
}
export default function Layout() {
const { user, logout } = useAuth();
const navigate = useNavigate();
const [drawerOpen, setDrawerOpen] = useState(false);
const fullName = user?.email ?? 'Admin';
const doLogout = async () => {
await logout();
navigate('/login');
};
const footer = (
<div className="mt-auto border-t border-line px-4 pb-4 pt-3">
<div className="mb-3 flex items-center gap-2.5">
<Avatar name={fullName} size={32} />
<div className="min-w-0">
<div className="truncate text-[13px] font-bold">{user?.email}</div>
<div className="text-[11.5px] text-dim">Administrator</div>
</div>
</div>
<div className="flex items-center gap-2">
<ThemeToggle />
<Button variant="secondary" size="sm" className="flex-1" onClick={doLogout}>
<LogOut className="w-4 h-4" />
Wyloguj
</Button>
</div>
</div>
);
return (
<div className="min-h-screen md:flex">
{/* Mobile drawer (telefon): ten sam zestaw pozycji co sidebar */}
{drawerOpen && (
<div className="fixed inset-0 z-50 md:hidden">
<div
className="absolute inset-0 animate-in fade-in-0 bg-black/60 duration-150"
onClick={() => setDrawerOpen(false)}
/>
<aside className="absolute left-0 top-0 flex h-full w-72 animate-in slide-in-from-left flex-col border-r border-line bg-card duration-200">
<div className="flex items-center justify-between px-4 py-4">
<div className="flex items-center gap-2.5">
<Brandmark size={26} />
<span className="font-display text-[1.05rem] font-bold tracking-[-0.02em]">FitOS Admin</span>
</div>
<button
type="button"
onClick={() => setDrawerOpen(false)}
aria-label="Zamknij menu"
className="-m-2 cursor-pointer p-2 text-dim hover:text-ink"
>
<X className="h-5 w-5" />
</button>
</div>
<NavSections onNavigate={() => setDrawerOpen(false)} />
{footer}
</aside>
</div>
)}
{/* Desktop/tablet sidebar z sekcjami */}
<aside className="hidden h-screen flex-col border-r border-line bg-card md:sticky md:top-0 md:flex md:w-64">
<div className="flex items-center gap-2.5 px-4 py-4">
<Brandmark size={26} />
<span className="font-display text-[1.05rem] font-bold tracking-[-0.02em]">FitOS Admin</span>
</div>
<NavSections />
{footer}
</aside>
<main className="min-w-0 flex-1">
<Topbar onMenuOpen={() => setDrawerOpen(true)} />
<div className="mx-auto max-w-5xl px-4 pb-24 pt-6 md:px-8 md:pb-10 md:pt-8">
<Outlet />
</div>
</main>
</div>
);
}