All files / src/contexts/Search Search.tsx

81.25% Statements 13/16
70% Branches 7/10
100% Functions 3/3
92.3% Lines 12/13

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            1x   3x   1x 3x 3x   3x 3x 3x   3x 3x     3x       3x            
import { createContext, FunctionComponent, useContext, useMemo } from "react";
import { CatalogSearchAPI } from "../../api/catalog-search";
import { PageLoader } from "../../components/PageLoader";
import { useCatalog } from "../../hooks/useCatalog";
import { useStats } from "../../hooks/useStats";
 
const SearchContext = createContext<CatalogSearchAPI | undefined>(undefined);
 
export const useSearchContext = () => useContext(SearchContext);
 
export const SearchProvider: FunctionComponent = ({ children }) => {
  const catalog = useCatalog();
  const stats = useStats();
 
  const searchAPI = useMemo(() => {
    Iif (catalog.data?.packages === undefined || catalog.isLoading) return;
    Iif (stats.data === undefined || stats.isLoading) return;
 
    const instance = new CatalogSearchAPI(catalog.data.packages, stats.data);
    return instance;
  }, [catalog.data, catalog.isLoading, stats.data, stats.isLoading]);
 
  Iif (!searchAPI) {
    return <PageLoader />;
  }
 
  return (
    <SearchContext.Provider value={searchAPI}>
      {children}
    </SearchContext.Provider>
  );
};