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

326 lines
10 KiB
TypeScript

import { useState } from 'react';
import { NavLink, Outlet, useNavigate } from 'react-router-dom';
import {
AppWindow,
Building2,
Dumbbell,
LogOut,
Megaphone,
Menu,
ScrollText,
ShieldCheck,
Target,
Users,
Wrench,
X,
type LucideIcon,
} from 'lucide-react';
import { useAuth } from '../store/auth';
import { useIsTrainer } from '../api/queries';
import { APP_URL } from '../config';
import { Button, ThemeToggle, LanguageToggle } 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;
/** Link na inny origin (np. aplikacja) — <a href>, nie <NavLink>. */
external?: 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 },
{ to: '/announcements', label: 'Ogłoszenia', icon: Megaphone, 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 },
// Powrót do aplikacji użytkownika — zawsze na końcu, inny origin (aplikacja
// wznowi sesję z własnego ciastka fitos_refresh, panelu nie dotyka).
{ to: APP_URL, label: 'Aplikacja', icon: AppWindow, end: false, external: true },
],
},
];
const SIDEBAR_KEY = 'fitos-admin-sidebar-collapsed';
function readCollapsed(): boolean {
try {
return localStorage.getItem(SIDEBAR_KEY) === '1';
} catch {
return false;
}
}
/* Wiersze nawigacji wg Ember: aktywny = tint akcentu + pionowy pasek przy krawędzi. */
function NavLinks({
items,
collapsed,
onNavigate,
}: {
items: NavItem[];
collapsed: boolean;
onNavigate?: () => void;
}) {
return (
<nav
className={cn('flex flex-col gap-0.5', collapsed ? 'px-2' : 'px-3')}
aria-label="Nawigacja główna"
>
{items.map((item) => {
const rowClass = (isActive: boolean) =>
cn(
'relative flex h-[38px] items-center gap-2.5 rounded-[10px] text-[13.5px] font-semibold transition-colors duration-150',
collapsed ? 'justify-center px-0' : 'px-3',
isActive ? 'bg-accent/11 text-accent-text' : 'text-dim hover:bg-line/30 hover:text-ink',
);
const icon = <item.icon className="h-[17px] w-[17px] shrink-0" strokeWidth={1.9} />;
const label = !collapsed && <span className="min-w-0 truncate">{item.label}</span>;
if (item.external) {
return (
<a
key={item.to}
href={item.to}
onClick={onNavigate}
title={collapsed ? item.label : undefined}
aria-label={collapsed ? item.label : undefined}
className={rowClass(false)}
>
{icon}
{label}
</a>
);
}
return (
<NavLink
key={item.to}
to={item.to}
end={item.end}
onClick={onNavigate}
title={collapsed ? item.label : undefined}
aria-label={collapsed ? item.label : undefined}
className={({ isActive }) => rowClass(isActive)}
>
{({ isActive }) => (
<>
{isActive && (
<span
aria-hidden="true"
className={cn(
'absolute bottom-[9px] top-[9px] w-[3px] rounded-full bg-(image:--nav-bar-grad)',
collapsed ? '-left-2' : '-left-3',
)}
/>
)}
{icon}
{label}
</>
)}
</NavLink>
);
})}
</nav>
);
}
function NavSections({ collapsed, onNavigate }: { collapsed: boolean; onNavigate?: () => void }) {
return (
<div className={cn('flex flex-col', collapsed ? 'gap-2' : 'gap-4')}>
{SECTIONS.map((section) => (
<div key={section.label}>
{!collapsed && (
<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} collapsed={collapsed} onNavigate={onNavigate} />
</div>
))}
</div>
);
}
export default function Layout() {
const { user, logout } = useAuth();
const navigate = useNavigate();
const [drawerOpen, setDrawerOpen] = useState(false);
const [collapsed, setCollapsed] = useState(readCollapsed);
const toggleCollapsed = () =>
setCollapsed((c) => {
const next = !c;
try {
localStorage.setItem(SIDEBAR_KEY, next ? '1' : '0');
} catch {
// ignore
}
return next;
});
const isTrainer = useIsTrainer();
const fullName =
[user?.firstName, user?.lastName].filter(Boolean).join(' ') || user?.email || '?';
const roles = [user?.isAdmin ? 'Admin' : null, isTrainer ? 'Trener' : 'Ćwiczący']
.filter(Boolean)
.join(' · ');
const doLogout = async () => {
await logout();
navigate('/login');
};
/* Stopka usera — pełna (sidebar/desktop i drawer). */
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?.firstName || user?.email}</div>
<div className="text-[11.5px] text-dim">{roles}</div>
</div>
</div>
<div className="flex items-center gap-2">
<ThemeToggle />
<LanguageToggle />
<Button variant="secondary" size="sm" className="flex-1" onClick={doLogout}>
<LogOut className="w-4 h-4" />
Wyloguj
</Button>
</div>
</div>
);
/* Stopka usera — rail (zwinięty sidebar): avatar + ikony. */
const footerRail = (
<div className="mt-auto flex flex-col items-center gap-2.5 border-t border-line px-2 pb-4 pt-3">
<Avatar name={fullName} size={32} />
<ThemeToggle />
<LanguageToggle />
<button
type="button"
onClick={doLogout}
aria-label="Wyloguj"
title="Wyloguj"
className="grid h-[38px] w-[38px] cursor-pointer place-items-center rounded-[10px] border border-line bg-card text-dim shadow-card transition-colors hover:border-danger/45 hover:text-danger"
>
<LogOut className="h-5 w-5" />
</button>
</div>
);
return (
<div className="min-h-screen md:flex">
{/* Mobile drawer (telefon): ten sam zestaw pozycji co sidebar, zawsze rozwinięty */}
{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 collapsed={false} onNavigate={() => setDrawerOpen(false)} />
{footer}
</aside>
</div>
)}
{/* Desktop/tablet sidebar z sekcjami: domyślnie rozwinięty, hamburger zwija do raila ~70px */}
<aside
className={cn(
'hidden h-screen flex-col border-r border-line bg-card transition-[width] duration-200 md:sticky md:top-0 md:flex',
collapsed ? 'md:w-[70px]' : 'md:w-60',
)}
>
{/* zwinięty: hamburger zastępuje logotyp (ta sama pozycja u góry raila) */}
<div
className={cn(
'flex items-center py-4',
collapsed ? 'justify-center px-2' : 'gap-2.5 px-4',
)}
>
{collapsed ? (
<button
type="button"
onClick={toggleCollapsed}
aria-label="Rozwiń menu"
title="Rozwiń menu"
className="grid h-7 w-7 flex-none cursor-pointer place-items-center rounded-[10px] border border-line bg-card text-dim shadow-card transition-colors hover:text-ink"
>
<Menu className="h-4 w-4" />
</button>
) : (
<>
<div className="flex min-w-0 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={toggleCollapsed}
aria-label="Zwiń menu"
title="Zwiń menu"
className="ml-auto grid h-7 w-7 flex-none cursor-pointer place-items-center rounded-[10px] border border-line bg-card text-dim shadow-card transition-colors hover:text-ink"
>
<Menu className="h-4 w-4" />
</button>
</>
)}
</div>
<NavSections collapsed={collapsed} />
{collapsed ? footerRail : 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>
);
}