import { useEffect, type ReactNode } from "react"; export type UhuruRouteGuardProps = { children?: ReactNode; fallback?: ReactNode; allow?: boolean; check?: () => boolean; onDenied?: () => void; redirectTo?: string; }; /** Protects one route, a route group, or an entire Uhuru app shell. */ export function UhuruRouteGuard({ allow, check, children, fallback = null, onDenied, redirectTo, }: UhuruRouteGuardProps) { const permitted = allow ?? check?.() ?? true; useEffect(() => { if (permitted) { return; } onDenied?.(); if (redirectTo && typeof window !== "undefined" && window.location.pathname !== redirectTo) { window.history.replaceState({}, "", redirectTo); window.dispatchEvent(new PopStateEvent("popstate")); } }, [onDenied, permitted, redirectTo]); return permitted ? children : fallback; }