'use client'; import { createContext, useContext, useState, useEffect, ReactNode } from 'react'; import { useParams } from 'next/navigation'; interface TenantContextType { tenantId: string | null; tenantName: string | null; setTenant: (id: string, name: string) => void; } const TenantContext = createContext(undefined); export function TenantProvider({ children }: { children: ReactNode }) { const params = useParams(); const [tenantId, setTenantId] = useState(null); const [tenantName, setTenantName] = useState(null); useEffect(() => { const tenant = params?.tenant as string | undefined; if (tenant) { setTenantId(tenant); } }, [params]); const setTenant = (id: string, name: string) => { setTenantId(id); setTenantName(name); }; return ( {children} ); } export function useTenant() { const context = useContext(TenantContext); if (context === undefined) { throw new Error('useTenant must be used within a TenantProvider'); } return context; } export function getTenantFromPath(): string | null { if (typeof window === 'undefined') return null; const path = window.location.pathname; const match = path.match(/^\/([a-z0-9][a-z0-9-]{1,30}[a-z0-9])(?:\/|$)/); return match ? match[1] : null; }