/** * useTenants Hook * * React hook for accessing tenants list via TanStack Query. * Provides tenants list with automatic caching and background refetch. * * Features: * - Automatic caching and deduplication * - Background refetch on window focus * - Loading and error states * - Automatic retries on failure * * @layer Presentation */ import { useQuery } from '@tanstack/react-query'; import { queryKeys } from '@/lib/query-keys'; import { tenantsApi } from '@/infrastructure/http/api/tenant'; import { authenticatedUserStore } from '@/infrastructure/storage/AuthenticatedUserStore'; /** * useTenants Hook * * Fetches tenants list for the current company using TanStack Query. * * @returns TanStack Query result with tenants array and pagination metadata * * @example * ```tsx * function TenantsList() { * const { data, isLoading, error } = useTenants(); * * if (isLoading) return
Loading...
; * if (error) return
Error: {error.message}
; * * const { tenants, pagination } = data || { tenants: [], pagination: {} }; * * return ( * * ); * } * ``` */ export function useTenants() { const companyId = authenticatedUserStore.get()?.companyId || ''; return useQuery({ queryKey: queryKeys.tenants.list({ companyId }), queryFn: async () => { const result = await tenantsApi.listTenants(companyId); return result; }, enabled: !!companyId, }); }