Vitron-Front/app/hooks/useRequireAdmin.ts
Arda Samadi 0360780411 feat(admin): admin-panel foundation shell + overview (slice 0)
- /admin opted out of shopper chrome in MyLayout; /admin added to server
  protectedRoutes; useRequireAdmin() gates to superusers (UserProfile.isSuperuser).
- Admin shell (admin.tsx) with sidebar nav (Overview live; other sections stubbed)
  and a mobile top bar; Overview dashboard (KPI cards + 14-day orders chart) via
  use-admin-hooks.ts hitting /api/adminpanel/v1/overview/.

Co-Authored-By: Claude Opus 4.8 <noreply@anthropic.com>
2026-07-25 12:00:49 +03:30

24 lines
781 B
TypeScript

import { useEffect } from "react";
import { useNavigate } from "@remix-run/react";
import { useServiceGetProfile } from "~/requestHandler/use-profile-hooks";
/**
* Gate a route to platform superusers. Non-admins (or unauthenticated) are
* bounced to the home page. Server-side, `/admin` is also in `protectedRoutes`
* so logged-out users never reach here — this adds the is-superuser check.
*/
export function useRequireAdmin() {
const navigate = useNavigate();
const { data, isLoading, isError } = useServiceGetProfile();
const isAdmin = !!data?.isSuperuser;
useEffect(() => {
if (isLoading) return;
if (isError || !isAdmin) {
navigate("/", { replace: true });
}
}, [isLoading, isError, isAdmin, navigate]);
return { isLoading, isAdmin };
}