-
Notifications
You must be signed in to change notification settings - Fork 333
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(algolia): add
getAlgoliaFacetHits
preset
- Loading branch information
1 parent
6b0c0ca
commit 3ec4b0e
Showing
17 changed files
with
501 additions
and
5 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,87 @@ | ||
/** @jsx h */ | ||
import { | ||
AutocompletePlugin, | ||
getAlgoliaFacetHits, | ||
highlightHit, | ||
} from '@algolia/autocomplete-js'; | ||
import { Hit } from '@algolia/client-search'; | ||
import { SearchClient } from 'algoliasearch/lite'; | ||
import { h, Fragment } from 'preact'; | ||
|
||
type CategoryItem = { | ||
label: string; | ||
count: number; | ||
}; | ||
|
||
type CreateCategoriesPluginProps = { | ||
searchClient: SearchClient; | ||
}; | ||
|
||
export function createCategoriesPlugin({ | ||
searchClient, | ||
}: CreateCategoriesPluginProps): AutocompletePlugin<CategoryItem, undefined> { | ||
return { | ||
getSources({ query }) { | ||
return [ | ||
{ | ||
sourceId: 'categoriesPlugin', | ||
getItems() { | ||
return getAlgoliaFacetHits({ | ||
searchClient, | ||
queries: [ | ||
{ | ||
indexName: 'instant_search', | ||
params: { | ||
facetName: 'categories', | ||
facetQuery: query, | ||
maxFacetHits: query ? 3 : 10, | ||
}, | ||
}, | ||
], | ||
}); | ||
}, | ||
templates: { | ||
header() { | ||
return ( | ||
<Fragment> | ||
<span className="aa-SourceHeaderTitle">Categories</span> | ||
<div className="aa-SourceHeaderLine" /> | ||
</Fragment> | ||
); | ||
}, | ||
item({ item }) { | ||
return ( | ||
<Fragment> | ||
<div className="aa-ItemIcon aa-ItemIcon--no-border"> | ||
<svg | ||
viewBox="0 0 24 24" | ||
width="18" | ||
height="18" | ||
fill="none" | ||
stroke="currentColor" | ||
strokeWidth="2" | ||
strokeLinecap="round" | ||
strokeLinejoin="round" | ||
> | ||
<path d="M21 16V8a2 2 0 0 0-1-1.73l-7-4a2 2 0 0 0-2 0l-7 4A2 2 0 0 0 3 8v8a2 2 0 0 0 1 1.73l7 4a2 2 0 0 0 2 0l7-4A2 2 0 0 0 21 16z" /> | ||
<polyline points="3.27 6.96 12 12.01 20.73 6.96" /> | ||
<line x1="12" y1="22.08" x2="12" y2="12" /> | ||
</svg> | ||
</div> | ||
<div className="aa-ItemContent"> | ||
<div className="aa-ItemContentTitle"> | ||
{highlightHit<Hit<CategoryItem>>({ | ||
hit: item as any, | ||
attribute: 'label', | ||
})} | ||
</div> | ||
</div> | ||
</Fragment> | ||
); | ||
}, | ||
}, | ||
}, | ||
]; | ||
}, | ||
}; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,17 @@ | ||
import { | ||
getAlgoliaFacetHits as getAlgoliaFacetHitsOriginal, | ||
SearchForFacetValuesParams, | ||
} from '@algolia/autocomplete-preset-algolia'; | ||
|
||
import { version } from './version'; | ||
|
||
export function getAlgoliaFacetHits({ | ||
searchClient, | ||
queries, | ||
}: Pick<SearchForFacetValuesParams, 'searchClient' | 'queries'>) { | ||
return getAlgoliaFacetHitsOriginal({ | ||
searchClient, | ||
queries, | ||
userAgents: [{ segment: 'autocomplete-js', version }], | ||
}); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
export type UserAgent = { segment: string; version?: string }; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
38 changes: 38 additions & 0 deletions
38
packages/autocomplete-preset-algolia/src/search/getAlgoliaFacetHits.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,38 @@ | ||
import { | ||
searchForFacetValues, | ||
SearchForFacetValuesParams, | ||
} from './searchForFacetValues'; | ||
|
||
type FacetHit = { | ||
label: string; | ||
count: number; | ||
_highlightResult: { | ||
label: { | ||
value: string; | ||
}; | ||
}; | ||
}; | ||
|
||
export function getAlgoliaFacetHits({ | ||
searchClient, | ||
queries, | ||
userAgents, | ||
}: SearchForFacetValuesParams): Promise<FacetHit[][]> { | ||
return searchForFacetValues({ searchClient, queries, userAgents }).then( | ||
(response) => { | ||
return response.map((result) => | ||
result.facetHits.map((facetHit) => { | ||
return { | ||
label: facetHit.value, | ||
count: facetHit.count, | ||
_highlightResult: { | ||
label: { | ||
value: facetHit.highlighted, | ||
}, | ||
}, | ||
}; | ||
}) | ||
); | ||
} | ||
); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.