import React from 'react' ; import algoliasearch , { Response } from 'algoliasearch' ; const algolia = { appId : 'M0TXMKQA8U' , apiKey : 'a73f705885dd5341ab59e7a7c8e3d870' , } ; const client = algoliasearch ( algolia . appId , algolia . apiKey ) ; const startersIndex = client . initIndex ( 'prod_STARTERS' ) ; export interface IStarter { objectID : string ; owner : string ; repo : string ; name : string ; description ? : string ; signature : string ; dependencies : IStarterDependency [ ] ; } export interface IStarterDependency { name : string ; } export interface IStarterDependencyFacet { value : string ; count : number ; } param dependencies export async function searchStarters ( query , dependencies , page = 0 , state = '' ) { const filters = dependencies . map ( dependency => `dependencies:${ dependency . name }` ) . join ( ' AND ' ) ; const res = await startersIndex . search < IStarter > ( { query : query , filters : filters , page : page , hitsPerPage : 10 , } ) ; return { ... res , state } ; } param query param dependencies export async function searchDependencies ( query , dependencies ) { const filters = dependencies . map ( dependency => `dependencies:${ dependency . name }` ) . join ( ' AND ' ) ; const res = await startersIndex . searchForFacetValues ( { facetName : 'dependencies' , facetQuery : query , filters : filters , } ) ; return res . facetHits ; } export type WithSearchContext < X > = X & { starters : IStarter [ ] ; facets : IStarterDependencyFacet [ ] ; } ; export const SearchContext = React . createContext < { starters : IStarter [ ] ; facets : IStarterDependencyFacet [ ] ; } > ( { starters : [ ] , facets : [ ] } ) ;