All files / src/hooks/useCompanySearch useCompanySearch.ts

100% Statements 5/5
100% Branches 3/3
100% Functions 2/2
100% Lines 5/5

Press n or j to go to the next uncovered block, b, p or k for the previous block.

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37                                              2x 43x 43x   43x   19x              
/**
 * useCompanySearch Hook
 *
 * React Query hook for company autocomplete search with debouncing
 * AC 2 (Search & Filters), AC 10 (Performance <500ms), AC 15 (Service Integration)
 */
 
import { useQuery } from '@tanstack/react-query';
import { companyApiClient } from '@/services/api/companyApi';
 
/**
 * Search companies by query string (autocomplete)
 *
 * Features:
 * - 15 minute stale time (AC 10 - Caffeine-cached backend search)
 * - Minimum 3 characters required (AC 2 - Search requirement)
 * - Disabled query when query too short
 * - Default limit of 10 results for autocomplete
 *
 * @param query - Search query string
 * @param limit - Maximum number of results (defaults to 10 for autocomplete)
 * @returns React Query result with search results
 */
export const useCompanySearch = (query: string, limit: number = 10) => {
  const trimmedQuery = query?.trim() || '';
  const isQueryValid = trimmedQuery.length >= 3;
 
  return useQuery({
    queryKey: ['companySearch', trimmedQuery, limit],
    queryFn: () => companyApiClient.searchCompanies(trimmedQuery, limit),
    staleTime: 15 * 60 * 1000, // 15 minutes (AC 10 - Backend cached for 15min)
    gcTime: 20 * 60 * 1000, // 20 minutes garbage collection
    enabled: isQueryValid, // Only search with 3+ characters (AC 2 requirement)
    refetchOnWindowFocus: false, // Don't refetch autocomplete on focus
  });
};