From 227ba954a87eeb2587fc15a4a3c5895d4a2dae96 Mon Sep 17 00:00:00 2001 From: Daniel O'Connell Date: Sat, 19 Aug 2023 16:15:49 +0200 Subject: [PATCH] unify search class --- app/components/search.tsx | 21 ++-- stampy-search/README.md | 29 ++++++ stampy-search/example/index.html | 11 ++- stampy-search/src/index.ts | 4 +- stampy-search/src/search.ts | 161 ++++++++++++++++++------------- 5 files changed, 141 insertions(+), 85 deletions(-) diff --git a/app/components/search.tsx b/app/components/search.tsx index 3bc6ab4c..7b4ce175 100644 --- a/app/components/search.tsx +++ b/app/components/search.tsx @@ -1,9 +1,7 @@ import {useState, useEffect, useRef, MutableRefObject, FocusEvent} from 'react' import debounce from 'lodash/debounce' import { - setupSearch, - searchLive, - searchUnpublished, + Searcher, Question as QuestionType, SearchResult, } from 'stampy-search' @@ -34,21 +32,22 @@ export default function Search({ const [arePendingSearches, setPendingSearches] = useState(false) const [results, setResults] = useState([] as SearchResult[]) + const [searcher, setSearcher] = useState() - useEffect(() => { - setupSearch({ - getAllQuestions: () => onSiteAnswersRef.current, - }) - }, [onSiteAnswersRef]) + useEffect(() => { + setSearcher(new Searcher({ + getAllQuestions: () => onSiteAnswersRef.current, + })) + }, [onSiteAnswersRef]) - const searchFn = (rawValue: string) => { + const searchFn = (rawValue: string) => { const value = rawValue.trim() if (value === searchInputRef.current) return searchInputRef.current = value setPendingSearches(true) - searchLive(value).then((res) => { + searcher.searchLive(value).then((res) => { if (res) { setPendingSearches(false) setResults(res as SearchResult[]) @@ -59,7 +58,7 @@ export default function Search({ useEffect(() => { initialQuery && searchFn(initialQuery) - }, [initialQuery]) + }, [initialQuery, searchFn]) const handleChange = debounce(searchFn, 100) diff --git a/stampy-search/README.md b/stampy-search/README.md index 34839c77..c6b8d1f2 100644 --- a/stampy-search/README.md +++ b/stampy-search/README.md @@ -24,6 +24,7 @@ Configuration is done by calling `stampySeach.setupSearch()`, where ` , + onResolveCallback: (query: string, res: SearchResult[] | null) => console.log(query, 'resolved to', res), + numResults: 12, + server: 'http://127.0.0.1:3123', + }) + + // search for live questions + console.log('got', await searcher.searchLive('bla bla bla')) + + // search for live questions with a callback + searcher.searchConfig.onResolveCallback = (query, res) => { + // process the results + } + searcher.liveSearch('bla bla bla') + + // search for unpublished questions + console.log(await searcher.searchUnpublished('bla bla bla')) + + // Generic search function + + console.log(searcher.search(SearchType.LiveOnSite, 'bla bla bla')) + console.log(searcher.search(SearchType.Unpublished, 'bla bla bla')) diff --git a/stampy-search/example/index.html b/stampy-search/example/index.html index e60014a2..da6cc9c1 100644 --- a/stampy-search/example/index.html +++ b/stampy-search/example/index.html @@ -16,15 +16,18 @@