Search integrations for all substrings + don't search on description (#104099)

This commit is contained in:
Kyle Pollich 2021-07-01 11:30:13 -04:00 committed by GitHub
parent 027446634e
commit 0fe301d083
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23

View file

@ -5,19 +5,20 @@
* 2.0.
*/
import { Search as LocalSearch } from 'js-search';
import { Search as LocalSearch, AllSubstringsIndexStrategy } from 'js-search';
import { useEffect, useRef } from 'react';
import type { PackageList } from '../../../types';
export const searchIdField = 'id';
export const fieldsToSearch = ['description', 'name', 'title'];
export const fieldsToSearch = ['name', 'title'];
export function useLocalSearch(packageList: PackageList) {
const localSearchRef = useRef<LocalSearch | null>(null);
useEffect(() => {
const localSearch = new LocalSearch(searchIdField);
localSearch.indexStrategy = new AllSubstringsIndexStrategy();
fieldsToSearch.forEach((field) => localSearch.addIndex(field));
localSearch.addDocuments(packageList);
localSearchRef.current = localSearch;