import { useEffect, useRef } from 'react' import { Navigate, useLocation } from 'react-router-dom' import { toast } from 'sonner' import { useAuth } from '../contexts/AuthContext' export function AdminRoute({ children }: { children: React.ReactNode }) { const { user, loading, isAdmin } = useAuth() const location = useLocation() const toastShownRef = useRef(false) useEffect(() => { if (!loading && user && !isAdmin && !toastShownRef.current) { toastShownRef.current = true toast.error('Admin access required') } }, [loading, user, isAdmin]) if (loading) { return (
) } if (!user) { return } if (!isAdmin) { return } return <>{children} }