Barazo default frontend barazo.forum
at main 46 lines 1.1 kB view raw
1'use client' 2 3import { useEffect, useMemo, useState } from 'react' 4import { usePathname, useRouter } from 'next/navigation' 5import { getSetupStatus } from '@/lib/api/client' 6 7const BYPASS_PATHS = ['/setup', '/login', '/auth'] 8 9export function SetupGuard({ children }: { children: React.ReactNode }) { 10 const pathname = usePathname() 11 const router = useRouter() 12 13 const isBypassPath = useMemo( 14 () => BYPASS_PATHS.some((p) => pathname === p || pathname.startsWith(`${p}/`)), 15 [pathname] 16 ) 17 18 const [checked, setChecked] = useState(isBypassPath) 19 20 useEffect(() => { 21 if (isBypassPath || checked) { 22 return 23 } 24 25 async function check() { 26 try { 27 const status = await getSetupStatus() 28 if (!status.initialized) { 29 router.replace('/setup') 30 return 31 } 32 } catch { 33 // If status check fails, allow through (API might be down) 34 } 35 setChecked(true) 36 } 37 38 void check() 39 }, [isBypassPath, checked, router]) 40 41 if (!isBypassPath && !checked) { 42 return null 43 } 44 45 return <>{children}</> 46}