PR — Ouverture automatique / create-pr (push) Successful in 2s
/auth/me renvoie desormais has_avatar et avatar_updated_at (LEFT JOIN user_avatars). Le header affiche l'avatar avec un cache-buster sur updated_at, et retombe sur les initiales en cas d'absence ou d'echec. Le profil rafraichit la session apres ajout/retrait de l'avatar pour que le header se mette a jour immediatement.
171 lines
5.8 KiB
TypeScript
171 lines
5.8 KiB
TypeScript
import { useState } from "react";
|
|
import { NavLink, Outlet } from "react-router-dom";
|
|
import { Button, cn } from "../ui/index.js";
|
|
import { api } from "../lib/api.js";
|
|
import { useSession } from "../lib/session.js";
|
|
import {
|
|
ActivityIcon,
|
|
CloseIcon,
|
|
FolderIcon,
|
|
HardDriveIcon,
|
|
ImageIcon,
|
|
LogoutIcon,
|
|
MenuIcon,
|
|
SearchNavIcon,
|
|
SettingsIcon,
|
|
ShieldIcon,
|
|
TrashIcon,
|
|
} from "./icons.js";
|
|
|
|
interface NavItem {
|
|
to: string;
|
|
label: string;
|
|
icon: typeof FolderIcon;
|
|
}
|
|
|
|
const NAV: NavItem[] = [
|
|
{ to: "/files", label: "Fichiers", icon: FolderIcon },
|
|
{ to: "/search", label: "Recherche", icon: SearchNavIcon },
|
|
{ to: "/gallery", label: "Galerie", icon: ImageIcon },
|
|
{ to: "/trash", label: "Corbeille", icon: TrashIcon },
|
|
];
|
|
|
|
const ADMIN_NAV: NavItem[] = [
|
|
{ to: "/admin/comptes", label: "Comptes", icon: ShieldIcon },
|
|
{ to: "/admin/stockage", label: "Stockage", icon: HardDriveIcon },
|
|
{ to: "/admin/audit", label: "Journal", icon: ActivityIcon },
|
|
];
|
|
|
|
const ACCOUNT_NAV: NavItem = { to: "/profile", label: "Mon compte", icon: SettingsIcon };
|
|
|
|
const initials = (first: string, last: string): string =>
|
|
`${first.charAt(0)}${last.charAt(0)}`.toUpperCase() || "?";
|
|
|
|
const navClass = ({ isActive }: { isActive: boolean }) =>
|
|
cn(
|
|
"flex items-center gap-3 rounded-md px-3 py-2 text-sm font-medium transition-colors",
|
|
isActive
|
|
? "bg-accent/10 text-accent"
|
|
: "text-muted-foreground hover:bg-surface-hover hover:text-foreground",
|
|
);
|
|
|
|
export const AppShell = () => {
|
|
const { user, logout } = useSession();
|
|
const [menuOpen, setMenuOpen] = useState(false);
|
|
if (!user) return null;
|
|
|
|
const isAdmin = user.role_slug === "admin";
|
|
const allNav = [...NAV, ...(isAdmin ? ADMIN_NAV : []), ACCOUNT_NAV];
|
|
|
|
const brand = (
|
|
<NavLink to="/files" className="flex items-center gap-2" onClick={() => setMenuOpen(false)}>
|
|
<img
|
|
src="/icon.png"
|
|
alt=""
|
|
className="h-7 w-7 shrink-0 rounded-md ring-1 ring-black/5"
|
|
/>
|
|
<span className="font-display text-xs font-semibold uppercase tracking-logo text-accent">
|
|
CoHaiSe
|
|
</span>
|
|
<span className="text-border" aria-hidden="true">
|
|
/
|
|
</span>
|
|
<span className="font-display text-xs font-semibold uppercase tracking-label text-foreground">
|
|
Cloud
|
|
</span>
|
|
</NavLink>
|
|
);
|
|
|
|
const renderLink = (item: NavItem) => {
|
|
const Icon = item.icon;
|
|
return (
|
|
<NavLink key={item.to} to={item.to} className={navClass} onClick={() => setMenuOpen(false)}>
|
|
<Icon className="h-4 w-4" />
|
|
{item.label}
|
|
</NavLink>
|
|
);
|
|
};
|
|
|
|
return (
|
|
<div className="flex min-h-dvh bg-background">
|
|
<aside className="hidden w-60 shrink-0 flex-col gap-1 border-r border-border bg-surface p-3 lg:flex">
|
|
<div className="px-1 py-2">{brand}</div>
|
|
{NAV.map(renderLink)}
|
|
{isAdmin ? (
|
|
<>
|
|
<p className="mt-3 px-3 py-1 font-display text-[0.6875rem] font-semibold uppercase tracking-label text-subtle-foreground">
|
|
Administration
|
|
</p>
|
|
{ADMIN_NAV.map(renderLink)}
|
|
</>
|
|
) : null}
|
|
<div className="mt-auto pt-3">{renderLink(ACCOUNT_NAV)}</div>
|
|
</aside>
|
|
|
|
<div className="flex min-w-0 flex-1 flex-col">
|
|
<header className="sticky top-0 z-20 border-b border-border bg-surface">
|
|
<div className="mx-auto flex w-full max-w-5xl items-center justify-between gap-4 px-4 py-3">
|
|
<div className="flex items-center gap-3">
|
|
<button
|
|
type="button"
|
|
className="text-muted-foreground lg:hidden"
|
|
aria-label={menuOpen ? "Fermer le menu" : "Ouvrir le menu"}
|
|
aria-expanded={menuOpen}
|
|
onClick={() => setMenuOpen((value) => !value)}
|
|
>
|
|
{menuOpen ? <CloseIcon className="h-5 w-5" /> : <MenuIcon className="h-5 w-5" />}
|
|
</button>
|
|
<span className="lg:hidden">{brand}</span>
|
|
</div>
|
|
|
|
<div className="flex items-center gap-3">
|
|
<div className="hidden text-right sm:block">
|
|
<p className="text-xs font-medium text-foreground">
|
|
{user.first_name} {user.last_name}
|
|
</p>
|
|
<p className="text-[0.6875rem] uppercase tracking-label text-subtle-foreground">
|
|
{user.role_name}
|
|
</p>
|
|
</div>
|
|
<span
|
|
className="relative flex h-9 w-9 shrink-0 items-center justify-center overflow-hidden rounded-full border border-border bg-surface text-xs font-semibold text-foreground"
|
|
aria-hidden="true"
|
|
>
|
|
{initials(user.first_name, user.last_name)}
|
|
{user.has_avatar ? (
|
|
<img
|
|
src={`${api.avatarUrl(user.id)}?v=${encodeURIComponent(user.avatar_updated_at ?? "")}`}
|
|
alt=""
|
|
className="absolute inset-0 h-full w-full object-cover"
|
|
onError={(event) => {
|
|
event.currentTarget.style.display = "none";
|
|
}}
|
|
/>
|
|
) : null}
|
|
</span>
|
|
<Button
|
|
variant="secondary"
|
|
size="sm"
|
|
iconLeading={<LogoutIcon className="h-3.5 w-3.5" />}
|
|
onClick={() => void logout()}
|
|
>
|
|
<span className="hidden sm:inline">Déconnexion</span>
|
|
</Button>
|
|
</div>
|
|
</div>
|
|
|
|
{menuOpen ? (
|
|
<nav className="flex flex-col gap-1 border-t border-border px-3 py-2 lg:hidden">
|
|
{allNav.map(renderLink)}
|
|
</nav>
|
|
) : null}
|
|
</header>
|
|
|
|
<main className="mx-auto w-full max-w-5xl flex-1 px-4 py-6">
|
|
<Outlet />
|
|
</main>
|
|
</div>
|
|
</div>
|
|
);
|
|
};
|