Wizard V2: Packages refactor, recommendations fix.

This commit is contained in:
Andrew Dewar 2024-04-30 22:26:57 +01:00 committed by Michal Gold
parent 8271e6d159
commit f9aae48dd1
10 changed files with 408 additions and 236 deletions

View file

@ -0,0 +1,32 @@
import { useEffect, useState } from 'react';
import { isEqual } from 'lodash';
import { DEBOUNCED_SEARCH_WAIT_TIME } from '../constants';
function useDebounce<T>(
value: T,
delay: number = DEBOUNCED_SEARCH_WAIT_TIME
): T {
const [debouncedValue, setDebouncedValue] = useState<T>(value);
useEffect(() => {
// We need to make sure that we compare-deep here as the default useEffect deps do not.
if (!isEqual(value, debouncedValue)) {
const timer = setTimeout(
() => setDebouncedValue(value),
value === '' ? 0 : delay !== undefined ? delay : 500 //If value is empty string, instantly return
);
return () => {
clearTimeout(timer);
};
}
// Eslint apparrently likes cyclic dependencies.
// eslint-disable-next-line react-hooks/exhaustive-deps
}, [value, delay]);
return debouncedValue;
}
export default useDebounce;