/** * usePageAnalyses Hook * * TanStack Query hook for fetching page analyses for a tenant. * Provides automatic caching, request deduplication, and background refetching. * * @layer Presentation */ import { useQuery } from '@tanstack/react-query'; import { queryKeys } from '@/lib/query-keys'; import { infoCenterApi } from '@/infrastructure/http/api/info-center'; import type { PageAnalysis } from '@/domain/entities/PageAnalysis'; import type { ReliabilityStatus } from '@archer/domain'; /** * usePageAnalyses Hook Options */ export interface UsePageAnalysesOptions { /** * Tenant ID to fetch analyses for */ tenantId?: string; /** * Number of items to skip (pagination) * @default 0 */ skip?: number; /** * Maximum number of items to return * @default 100 */ limit?: number; /** * Filter by reliability status */ reliabilityStatus?: ReliabilityStatus; /** * Enable/disable the query * @default true (when tenantId is provided) */ enabled?: boolean; } /** * usePageAnalyses Hook Return Type */ export interface UsePageAnalysesReturn { /** * Page analyses array */ analyses: PageAnalysis[]; /** * Loading state */ isLoading: boolean; /** * Error state */ error: Error | null; /** * Total count of analyses (from pagination metadata) */ total: number; /** * Whether there are more analyses to load */ hasMore: boolean; /** * Refetch function */ refetch: () => void; } /** * Fetch page analyses for a tenant * * Uses TanStack Query for automatic caching, request deduplication, * and background refetching. * * @param options - Query options * @returns Page analyses with loading/error states * * @example * ```tsx * function AnalysesList() { * const { analyses, isLoading, error } = usePageAnalyses({ * tenantId: 'tenant-123', * limit: 100, * }); * * if (isLoading) return ; * if (error) return ; * * return ( * * ); * } * ``` */ export function usePageAnalyses(options: UsePageAnalysesOptions = {}): UsePageAnalysesReturn { const { tenantId, skip = 0, limit = 100, reliabilityStatus, enabled, } = options; // Query parameters for cache key const queryParams = { skip, limit, ...(reliabilityStatus && { reliabilityStatus }), }; // Query const query = useQuery({ queryKey: queryKeys.infoCenter.analyses(tenantId ?? '', queryParams), queryFn: async () => { if (!tenantId) { throw new Error('tenantId is required'); } const result = await infoCenterApi.getPageAnalyses({ tenantId, skip, limit, reliabilityStatus, }); return result; }, enabled: enabled !== undefined ? enabled : !!tenantId, staleTime: 5 * 60 * 1000, // 5 minutes gcTime: 10 * 60 * 1000, // 10 minutes (renamed from cacheTime in v5) }); return { analyses: query.data?.items ?? [], isLoading: query.isLoading, error: query.error as Error | null, total: query.data?.meta.total ?? 0, hasMore: query.data?.meta.hasNext ?? false, refetch: query.refetch, }; }