|
| 1 | +'use client'; |
| 2 | + |
| 3 | +import { usePathname } from 'next/navigation'; |
| 4 | +import { useModuleAvailability } from '@/contexts/ModuleAvailabilityContext'; |
| 5 | +import { |
| 6 | + getModuleNameFromPath, |
| 7 | + getModuleDisplayName, |
| 8 | +} from '@/lib/utils/module-utils'; |
| 9 | +import { ModuleNotFound } from '@/components/module-not-found/ModuleNotFound'; |
| 10 | +import { Skeleton } from '@/components/ui/skeleton'; |
| 11 | + |
| 12 | +interface ModuleGuardProps { |
| 13 | + children: React.ReactNode; |
| 14 | +} |
| 15 | + |
| 16 | +export function ModuleGuard({ children }: ModuleGuardProps) { |
| 17 | + const pathname = usePathname(); |
| 18 | + const { modules, isLoading, isModuleAvailable, isModuleServing } = |
| 19 | + useModuleAvailability(); |
| 20 | + |
| 21 | + // Get module name from current path |
| 22 | + const moduleName = getModuleNameFromPath(pathname); |
| 23 | + |
| 24 | + // If not a module path, loading, or settings route, render children |
| 25 | + if (!moduleName || isLoading || pathname.startsWith('/settings')) { |
| 26 | + if (isLoading) { |
| 27 | + return ( |
| 28 | + <div className="flex items-center justify-center min-h-[60vh] p-4"> |
| 29 | + <div className="space-y-4 w-full max-w-md"> |
| 30 | + <Skeleton className="h-8 w-full" /> |
| 31 | + <Skeleton className="h-4 w-3/4" /> |
| 32 | + <Skeleton className="h-10 w-full" /> |
| 33 | + </div> |
| 34 | + </div> |
| 35 | + ); |
| 36 | + } |
| 37 | + return <>{children}</>; |
| 38 | + } |
| 39 | + |
| 40 | + // Check if module is available and serving |
| 41 | + const available = isModuleAvailable(moduleName); |
| 42 | + const serving = isModuleServing(moduleName); |
| 43 | + |
| 44 | + // If module is not available or not serving, show module not found |
| 45 | + if (!available || !serving) { |
| 46 | + // Map the API module name to the URL path for display name lookup |
| 47 | + const urlPath = pathname.split('/')[1]; |
| 48 | + const displayName = getModuleDisplayName(urlPath); |
| 49 | + return ( |
| 50 | + <ModuleNotFound |
| 51 | + moduleName={displayName} |
| 52 | + isAvailable={available} |
| 53 | + isServing={serving} |
| 54 | + /> |
| 55 | + ); |
| 56 | + } |
| 57 | + |
| 58 | + // Module is available and serving, render children |
| 59 | + return <>{children}</>; |
| 60 | +} |
0 commit comments