From 17bf8ba577da85c27d206033b30304bd4ffa8b65 Mon Sep 17 00:00:00 2001 From: Caden Buckhalt Date: Wed, 7 Feb 2024 13:09:04 -0800 Subject: [PATCH] fix: drag and drop not working on large rosters another exhaustive deps fix that caused an infinite loop. --- lib/interviewer/hooks/useSearch.js | 73 +++++++++++++++--------------- 1 file changed, 37 insertions(+), 36 deletions(-) diff --git a/lib/interviewer/hooks/useSearch.js b/lib/interviewer/hooks/useSearch.js index 1e295957..c98ae095 100644 --- a/lib/interviewer/hooks/useSearch.js +++ b/lib/interviewer/hooks/useSearch.js @@ -1,10 +1,4 @@ -import { - useMemo, - useState, - useEffect, - useRef, - useCallback, -} from 'react'; +import { useMemo, useState, useEffect, useRef, useCallback } from 'react'; import Fuse from 'fuse.js'; const MIN_QUERY_LENGTH = 1; @@ -68,33 +62,39 @@ const useSearch = (list, options, initialQuery = '') => { const hasQuery = useMemo(() => query.length >= MIN_QUERY_LENGTH, [query]); const isLargeList = useMemo(() => list.length > 100, [list]); - const fuseOptions = useMemo(() => ({ ...defaultFuseOptions, ...options }), [options]); + const fuseOptions = useMemo( + () => ({ ...defaultFuseOptions, ...options }), + [options], + ); const fuse = useMemo(() => new Fuse(list, fuseOptions), [list, fuseOptions]); - const search = useCallback((_query) => { - if (isLargeList) { - clearTimeout(delayRef.current); - setIsWaiting(true); - } - - const res = fuse.search(_query); - - const r = res.map(({ item, score }) => ({ - ...item, - relevance: 1 - score, // fuseJS relevance is reverse nomalized (0 is perfect match) - })); - - if (isLargeList) { - delayRef.current = setTimeout(() => { - setResults(r); - setIsWaiting(false); - }, DEBOUNCE_DELAY); - return; - } - - setResults(r); - setIsWaiting(false); - }, [fuse, isLargeList]); + const search = useCallback( + (_query) => { + if (isLargeList) { + clearTimeout(delayRef.current); + setIsWaiting(true); + } + + const res = fuse.search(_query); + + const r = res.map(({ item, score }) => ({ + ...item, + relevance: 1 - score, // fuseJS relevance is reverse nomalized (0 is perfect match) + })); + + if (isLargeList) { + delayRef.current = setTimeout(() => { + setResults(r); + setIsWaiting(false); + }, DEBOUNCE_DELAY); + return; + } + + setResults(r); + setIsWaiting(false); + }, + [fuse, isLargeList], + ); useEffect(() => { if (!hasQuery) { @@ -102,11 +102,12 @@ const useSearch = (list, options, initialQuery = '') => { } search(query); - }, [query, hasQuery, search]); + }, [query, hasQuery]); // eslint-disable-line react-hooks/exhaustive-deps - const returnResults = useMemo(() => ( - hasQuery ? results : list - ), [hasQuery, list, results]); + const returnResults = useMemo( + () => (hasQuery ? results : list), + [hasQuery, list, results], + ); return [returnResults, query, setQuery, isWaiting, hasQuery]; };