204 lines
7.3 KiB
TypeScript
204 lines
7.3 KiB
TypeScript
import { createContext, useContext, useEffect, useState, type ReactNode } from "react";
|
|
import { NavLink, Outlet } from "react-router-dom";
|
|
import { cn } from "../ui/index.js";
|
|
import { useSession } from "../lib/session.js";
|
|
import { useI18n } from "../lib/i18n.js";
|
|
import { Avatar } from "./Avatar.js";
|
|
import { ColorBendsBackdrop } from "./ColorBendsBackdrop.js";
|
|
import {
|
|
ActivityIcon,
|
|
CloseIcon,
|
|
FolderIcon,
|
|
HardDriveIcon,
|
|
ImageIcon,
|
|
LogoutIcon,
|
|
MenuIcon,
|
|
SearchNavIcon,
|
|
SettingsIcon,
|
|
ShieldIcon,
|
|
StarIcon,
|
|
TrashIcon,
|
|
} from "./icons.js";
|
|
import { IconAction } from "./IconAction.js";
|
|
|
|
interface NavItem {
|
|
to: string;
|
|
label: string;
|
|
icon: typeof FolderIcon;
|
|
}
|
|
|
|
const NAV: NavItem[] = [
|
|
{ to: "/files", label: "Fichiers", icon: FolderIcon },
|
|
{ to: "/favorites", label: "Favoris", icon: StarIcon },
|
|
{ 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 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",
|
|
);
|
|
|
|
type PageHeaderContextValue = {
|
|
header: ReactNode | null;
|
|
setHeader: (header: ReactNode | null) => void;
|
|
};
|
|
|
|
const PageHeaderContext = createContext<PageHeaderContextValue | null>(null);
|
|
|
|
export const usePageHeader = (header: ReactNode | null) => {
|
|
const ctx = useContext(PageHeaderContext);
|
|
useEffect(() => {
|
|
if (!ctx) return;
|
|
ctx.setHeader(header);
|
|
return () => ctx.setHeader(null);
|
|
}, [ctx, header]);
|
|
};
|
|
|
|
export const AppShell = () => {
|
|
const { user, logout } = useSession();
|
|
const { t } = useI18n();
|
|
const [menuOpen, setMenuOpen] = useState(false);
|
|
const [pageHeader, setPageHeader] = useState<ReactNode | null>(null);
|
|
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" />
|
|
{t(item.label)}
|
|
</NavLink>
|
|
);
|
|
};
|
|
|
|
return (
|
|
<PageHeaderContext.Provider value={{ header: pageHeader, setHeader: setPageHeader }}>
|
|
<div className="relative grid h-dvh grid-cols-[auto_1fr] grid-rows-[auto_1fr] overflow-hidden bg-background">
|
|
<ColorBendsBackdrop variant="shell" />
|
|
|
|
<header className="relative z-50 col-span-2 row-start-1 border-b border-border/50 bg-surface/70 backdrop-blur-md">
|
|
<div className="flex w-full 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 ? t("Fermer le menu") : t("Ouvrir le menu")}
|
|
aria-expanded={menuOpen}
|
|
onClick={() => setMenuOpen((value) => !value)}
|
|
>
|
|
{menuOpen ? <CloseIcon className="h-5 w-5" /> : <MenuIcon className="h-5 w-5" />}
|
|
</button>
|
|
{brand}
|
|
</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>
|
|
<Avatar
|
|
userId={user.id}
|
|
firstName={user.first_name}
|
|
lastName={user.last_name}
|
|
hasAvatar={user.has_avatar}
|
|
avatarUpdatedAt={user.avatar_updated_at}
|
|
/>
|
|
<IconAction
|
|
label={t("Déconnexion")}
|
|
icon={<LogoutIcon className="h-4 w-4" />}
|
|
onClick={() => void logout()}
|
|
/>
|
|
</div>
|
|
</div>
|
|
|
|
</header>
|
|
|
|
<aside className="relative z-40 col-start-1 row-start-2 hidden w-60 flex-col gap-1 overflow-y-auto border-r border-border/50 bg-surface/70 p-3 backdrop-blur-md lg:flex">
|
|
{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">
|
|
{t("Administration")}
|
|
</p>
|
|
{ADMIN_NAV.map(renderLink)}
|
|
</>
|
|
) : null}
|
|
<div className="mt-auto pt-3">{renderLink(ACCOUNT_NAV)}</div>
|
|
</aside>
|
|
|
|
{menuOpen ? (
|
|
<div className="lg:hidden">
|
|
<div className="fixed inset-0 z-40 bg-black/50 backdrop-blur-sm" onClick={() => setMenuOpen(false)} aria-hidden="true" />
|
|
<nav className="fixed inset-0 z-50 flex flex-col bg-background/95 backdrop-blur-lg transition-transform duration-300 sm:absolute sm:inset-y-0 sm:left-0 sm:w-72 sm:inset-x-auto">
|
|
<div className="flex items-center justify-between border-b border-border/50 px-4 py-3">
|
|
{brand}
|
|
<button
|
|
type="button"
|
|
className="text-muted-foreground"
|
|
aria-label={t("Fermer le menu")}
|
|
onClick={() => setMenuOpen(false)}
|
|
>
|
|
<CloseIcon className="h-5 w-5" />
|
|
</button>
|
|
</div>
|
|
<div className="flex flex-1 flex-col gap-1 overflow-y-auto p-3">
|
|
{allNav.map(renderLink)}
|
|
</div>
|
|
</nav>
|
|
</div>
|
|
) : null}
|
|
|
|
<div className="relative z-10 col-start-2 row-start-2 min-h-0 overflow-hidden">
|
|
<div className="h-full w-full bg-surface/30 backdrop-blur-lg">
|
|
<div className="flex h-full flex-col">
|
|
{pageHeader ? <div className="relative z-30 shrink-0">{pageHeader}</div> : null}
|
|
<main className="min-h-0 flex-1 overflow-y-auto overflow-x-hidden px-4 pb-6 pt-4 [scrollbar-gutter:stable]">
|
|
<Outlet />
|
|
</main>
|
|
</div>
|
|
</div>
|
|
</div>
|
|
</div>
|
|
</PageHeaderContext.Provider>
|
|
);
|
|
};
|