L'admin etait une 2e app front Vite independante (apps/admin-web, port 5174,
Dockerfile nginx, service compose dedie, job CI propre) non demandee. Toute
l'UI passe dans apps/web sous le prefixe /admin ; seul le frontend est
fusionne, l'authentification admin reste entierement separee.
Front (apps/web/src) :
- pages -> pages/admin/{login,dashboard,monitoring,corrections,catalog}/,
layout -> layouts/AdminLayout.tsx, contexte + garde -> features/admin/.
- client API -> api/admin-client.ts : classe AdminApiError (evite la
collision avec ApiError), lit VITE_API_URL (plus de VITE_ADMIN_API_URL).
- routes /admin/* dans App.tsx, enveloppees d'AdminAuthProvider +
RequireAdmin -> le probe GET /admin/auth/me ne tourne que sous /admin.
- reutilise l'i18n, lib/zod-errors, services/error-message.service et le
theme SCSS de apps/web ; bloc i18n admin.* fusionne dans la locale fr
(les cles errors etaient deja toutes presentes).
- corrige une race dans CatalogPage (reponse d'un onglet precedent qui
ecrasait l'onglet courant, exposee par le double-mount StrictMode) via
un ref requestSeq.
Auth admin inchangee : table AdminUser, cookie admin_session,
ADMIN_JWT_SECRET, script create-admin.ts.
Infra :
- docker-compose : service admin-web + ADMIN_WEB_PORT supprimes (l'app
`app` sert deja le front construit).
- ADMIN_CORS_ORIGIN retire (meme origine) : env.ts, app.ts, .env.example.
- job CI "Run admin-web E2E tests" supprime ; les specs admin-* tournent
dans le job web (apps/web/cypress/e2e/admin-*.{cy.ts,feature}).
- apps/api/.env.example : ajout ADMIN_JWT_SECRET / ADMIN_INITIAL_*.
- recharts ajoute a apps/web ; pnpm-lock regenere.
- specs/backend-architecture.md : section admin mise a jour.
Verifie : biome + tsc -b (web/api) + pnpm -r build verts ; Cypress web
102/103 (l'unique echec est le flake pre-existant recipe-form.feature
"Preloads ..." de clipping headless, sans rapport) ; 16/16 specs admin ;
45/45 composants.
Co-Authored-By: Claude Sonnet 5 <noreply@anthropic.com>
86 lines
3 KiB
TypeScript
86 lines
3 KiB
TypeScript
import { Activity, LayoutDashboard, ListChecks, PackageSearch } from "lucide-react";
|
|
import { useState } from "react";
|
|
import { useTranslation } from "react-i18next";
|
|
import { NavLink, Outlet, useNavigate } from "react-router-dom";
|
|
import { useAdminAuth } from "../features/admin/AdminAuthContext";
|
|
import "./AdminLayout.scss";
|
|
|
|
/**
|
|
* One entry in the admin sidebar's nav. `key` maps to `admin.nav.<key>` in
|
|
* the locale file — adding a section is one array entry plus one locale key.
|
|
* Paths are absolute under `/admin` (the admin route group lives inside
|
|
* `apps/web`'s `App.tsx`, mounted at `/admin`).
|
|
*/
|
|
const NAV_ITEMS = [
|
|
{ to: "/admin", key: "dashboard", Icon: LayoutDashboard, end: true },
|
|
{ to: "/admin/monitoring", key: "monitoring", Icon: Activity, end: false },
|
|
{ to: "/admin/corrections", key: "corrections", Icon: ListChecks, end: false },
|
|
{ to: "/admin/catalogue", key: "catalog", Icon: PackageSearch, end: false },
|
|
] as const;
|
|
|
|
/**
|
|
* Shell for every authenticated admin page: a fixed sidebar (brand, section
|
|
* nav, the signed-in admin's name + logout) plus a main area rendering the
|
|
* matched child route via `<Outlet />`. Mounted once as the parent of the
|
|
* whole `RequireAdmin`-guarded route group (see `App.tsx`), so `admin` is
|
|
* guaranteed non-null here.
|
|
*/
|
|
export function AdminLayout() {
|
|
const { t } = useTranslation();
|
|
const { admin, logout } = useAdminAuth();
|
|
const navigate = useNavigate();
|
|
const [isLoggingOut, setIsLoggingOut] = useState(false);
|
|
|
|
async function handleLogout() {
|
|
setIsLoggingOut(true);
|
|
try {
|
|
await logout();
|
|
void navigate("/admin/login");
|
|
} catch {
|
|
// Even if the network call failed, the local session state was
|
|
// cleared optimistically enough for the guard to bounce to /login;
|
|
// nothing useful to show the operator here.
|
|
void navigate("/admin/login");
|
|
}
|
|
}
|
|
|
|
return (
|
|
<div className="admin-layout">
|
|
<aside className="admin-sidebar">
|
|
<div className="admin-sidebar__brand">
|
|
batchCooking <span>Admin</span>
|
|
</div>
|
|
|
|
<nav className="admin-sidebar__nav">
|
|
{NAV_ITEMS.map(({ to, key, Icon, end }) => (
|
|
<NavLink
|
|
key={to}
|
|
to={to}
|
|
end={end}
|
|
className={({ isActive }) => (isActive ? "active" : undefined)}
|
|
>
|
|
<Icon size={18} aria-hidden="true" />
|
|
<span>{t(`admin.nav.${key}`)}</span>
|
|
</NavLink>
|
|
))}
|
|
</nav>
|
|
|
|
<div className="admin-sidebar__footer">
|
|
<span className="admin-sidebar__who" title={admin?.email}>
|
|
{admin?.name}
|
|
</span>
|
|
<button type="button" onClick={handleLogout} disabled={isLoggingOut}>
|
|
{t("admin.layout.logout")}
|
|
</button>
|
|
<p className="admin-sidebar__version" aria-hidden="true">
|
|
v{__APP_VERSION__}
|
|
</p>
|
|
</div>
|
|
</aside>
|
|
|
|
<main className="admin-content">
|
|
<Outlet />
|
|
</main>
|
|
</div>
|
|
);
|
|
}
|