/** * useDashboard Hook * * Aggregates tenant and integration key stats for the dashboard overview. * * @layer Presentation */ import { useMemo } from 'react'; import { useTenants } from '@/features/tenants/hooks/useTenants'; export interface DashboardStats { totalTenants: number; activeTenants: number; totalIntegrationKeys: number; activeIntegrationKeys: number; } export interface UseDashboard { stats: DashboardStats; isLoading: boolean; error: string | null; refetch: () => void; } export function useDashboard(): UseDashboard { const { data, isLoading, error, refetch } = useTenants(); const stats = useMemo(() => { const tenants = data?.tenants ?? []; const activeTenants = tenants.filter((t) => t.isActive()); // Sum integrationKeysCount across all tenants for total, // and across active tenants only for active keys count const totalIntegrationKeys = tenants.reduce((sum, t) => sum + t.integrationKeysCount, 0); const activeIntegrationKeys = activeTenants.reduce((sum, t) => sum + t.integrationKeysCount, 0); return { totalTenants: tenants.length, activeTenants: activeTenants.length, totalIntegrationKeys, activeIntegrationKeys, }; }, [data]); return { stats, isLoading, error: error?.message ?? null, refetch, }; }