All files / src/hooks/useSearch useSearch.ts

85.71% Statements 6/7
33.33% Branches 1/3
100% Functions 2/2
85.71% Lines 6/7

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 38                  1x         3x   3x           3x 3x                       3x    
import { useMemo } from "react";
import type { CatalogSearchParams } from "../../api/catalog-search";
import { useSearchContext } from "../../contexts/Search";
 
/**
 * A hook which returns a list of packages returned by the CatalogSearchAPI.
 * Generally this hook will be called via `useCatalogSearchResults` which wraps this functionality with pagination
 * This hook depends on an upstream provider - `<SearchProvider />`, which wraps all pages.
 */
export const useSearch = ({
  query,
  filters,
  sort,
}: CatalogSearchParams = {}) => {
  const instance = useSearchContext();
 
  Iif (!instance) {
    throw new Error(
      "This hook can only be called within a descendant of a <SearchProvider />"
    );
  }
 
  const results = useMemo(
    () => [
      ...instance
        .search({
          query,
          filters,
          sort,
        })
        .values(),
    ],
    [instance, query, filters, sort]
  );
 
  return results;
};