Skip to content

Commit

Permalink
fix: drag and drop not working on large rosters
Browse files Browse the repository at this point in the history
another exhaustive deps fix that caused an infinite loop.
  • Loading branch information
buckhalt committed Feb 7, 2024
1 parent 2f1e4c9 commit 17bf8ba
Showing 1 changed file with 37 additions and 36 deletions.
73 changes: 37 additions & 36 deletions lib/interviewer/hooks/useSearch.js
Original file line number Diff line number Diff line change
@@ -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;
Expand Down Expand Up @@ -68,45 +62,52 @@ 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) {
return;
}

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];
};
Expand Down

0 comments on commit 17bf8ba

Please sign in to comment.