Use custom search index for search results. Hide sort options while there's a query.

This commit is contained in:
James Grugett 2022-07-29 18:13:53 -07:00
parent 003301762c
commit 87f6949d80
2 changed files with 22 additions and 12 deletions

View File

@ -28,6 +28,7 @@ const searchClient = algoliasearch(
) )
const indexPrefix = ENV === 'DEV' ? 'dev-' : '' const indexPrefix = ENV === 'DEV' ? 'dev-' : ''
const searchIndexName = ENV === 'DEV' ? 'dev-contracts' : 'contractsIndex'
const sortOptions = [ const sortOptions = [
{ label: 'Newest', value: 'newest' }, { label: 'Newest', value: 'newest' },
@ -116,12 +117,7 @@ export function ContractSearch(props: {
track('select search category', { category: pill ?? 'all' }) track('select search category', { category: pill ?? 'all' })
} }
let facetFilters = query const additionalFilters = [
? []
: [
filter === 'open' ? 'isResolved:false' : '',
filter === 'closed' ? 'isResolved:false' : '',
filter === 'resolved' ? 'isResolved:true' : '',
additionalFilter?.creatorId additionalFilter?.creatorId
? `creatorId:${additionalFilter.creatorId}` ? `creatorId:${additionalFilter.creatorId}`
: '', : '',
@ -129,6 +125,14 @@ export function ContractSearch(props: {
additionalFilter?.groupSlug additionalFilter?.groupSlug
? `groupLinks.slug:${additionalFilter.groupSlug}` ? `groupLinks.slug:${additionalFilter.groupSlug}`
: '', : '',
]
let facetFilters = query
? additionalFilters
: [
...additionalFilters,
filter === 'open' ? 'isResolved:false' : '',
filter === 'closed' ? 'isResolved:false' : '',
filter === 'resolved' ? 'isResolved:true' : '',
pillFilter && pillFilter !== 'personal' && pillFilter !== 'your-bets' pillFilter && pillFilter !== 'personal' && pillFilter !== 'your-bets'
? `groupLinks.slug:${pillFilter}` ? `groupLinks.slug:${pillFilter}`
: '', : '',
@ -162,6 +166,10 @@ export function ContractSearch(props: {
const indexName = `${indexPrefix}contracts-${sort}` const indexName = `${indexPrefix}contracts-${sort}`
const index = useMemo(() => searchClient.initIndex(indexName), [indexName]) const index = useMemo(() => searchClient.initIndex(indexName), [indexName])
const searchIndex = useMemo(
() => searchClient.initIndex(searchIndexName),
[searchIndexName]
)
const [page, setPage] = useState(0) const [page, setPage] = useState(0)
const [numPages, setNumPages] = useState(1) const [numPages, setNumPages] = useState(1)
@ -171,7 +179,9 @@ export function ContractSearch(props: {
useEffect(() => { useEffect(() => {
let wasMostRecentQuery = true let wasMostRecentQuery = true
index const algoliaIndex = query ? searchIndex : index
algoliaIndex
.search(query, { .search(query, {
facetFilters, facetFilters,
numericFilters, numericFilters,
@ -198,7 +208,7 @@ export function ContractSearch(props: {
} }
// Note numeric filters are unique based on current time, so can't compare // Note numeric filters are unique based on current time, so can't compare
// them by value. // them by value.
}, [query, page, index, JSON.stringify(facetFilters), filter]) }, [query, page, index, searchIndex, JSON.stringify(facetFilters), filter])
const loadMore = () => { const loadMore = () => {
if (page >= numPages - 1) return if (page >= numPages - 1) return
@ -269,7 +279,7 @@ export function ContractSearch(props: {
<option value="all">All</option> <option value="all">All</option>
</select> </select>
)} )}
{!hideOrderSelector && ( {!hideOrderSelector && !query && (
<select <select
className="select select-bordered" className="select select-bordered"
value={sort} value={sort}

View File

@ -104,7 +104,7 @@ export function useQueryAndSortParams(options?: {
const pushQuery = useMemo( const pushQuery = useMemo(
() => () =>
debounce((query: string | undefined) => { debounce((query: string | undefined) => {
const queryObj = { ...router.query, q: query || undefined } const queryObj = { ...router.query, q: query }
if (!query) delete queryObj.q if (!query) delete queryObj.q
router.replace({ query: queryObj }, undefined, { router.replace({ query: queryObj }, undefined, {
shallow: true, shallow: true,