import { useRef, useState } from 'react'; import { ChevronDown, CornerUpLeft, Globe } from 'lucide-react'; import { useScopeContext } from '@/contexts/scope-context'; import { useTenantChildren } from '@/business/scope/queries'; import { TenantLogo } from '@/components/common/TenantLogo'; import { useOnClickOutside } from '@/hooks/useOnClickOutside'; const LEVEL_LABEL: Record = { organization: 'Organization', school: 'School', campus: 'Campus', class: 'Classroom', }; /** * Interactive active-tenant badge + drill-down switcher. Shows the effective * tenant (or "Platform" for global admins with no own tenant); opening it lists * the child tenants the user can drill into (organizations for a global admin), * plus a "back to my scope" reset once a child is selected. */ export function TenantSwitcher() { const { tier, effectiveTenant, selectedTenant, canDrill, drillInto, resetScope } = useScopeContext(); const [open, setOpen] = useState(false); const ref = useRef(null); useOnClickOutside(ref, () => setOpen(false), open); const parent = effectiveTenant ? { level: effectiveTenant.level, id: effectiveTenant.id } : null; const { data, isLoading } = useTenantChildren(open ? parent : null); const children = data?.rows ?? []; // Global admins have no own tenant — show a "Platform" root they can drill from. const isPlatformRoot = !effectiveTenant && tier === 'global'; if (!effectiveTenant && !isPlatformRoot) { return null; } const label = effectiveTenant?.name ?? 'Platform'; const levelLabel = effectiveTenant ? (LEVEL_LABEL[effectiveTenant.level] ?? 'Tenant') : 'Platform'; const mark = effectiveTenant ? ( ) : ( ); return (
{open && (
{selectedTenant && ( )} {canDrill ? ( isLoading ? (
Loading…
) : children.length > 0 ? ( children.map((c) => ( )) ) : (
No child tenants
) ) : (
No deeper scope
)}
)}
); }