Skip to content

Commit

Permalink
fix: main search debounce
Browse files Browse the repository at this point in the history
Fixes JOB-883
  • Loading branch information
johnshift committed Dec 20, 2024
1 parent a090074 commit 6adf11b
Showing 1 changed file with 21 additions and 5 deletions.
26 changes: 21 additions & 5 deletions src/search/hooks/use-search-input.ts
Original file line number Diff line number Diff line change
@@ -1,18 +1,34 @@
import { useAtom } from 'jotai';
import { useEffect, useState } from 'react';

import { useSetAtom } from 'jotai';

import { useDebouncedValue } from '@/shared/hooks/use-debounced-value';

import { searchQueryAtom } from '@/search/core/atoms';

const DEBOUNCE_DELAY = 500;

export const useSearchInput = () => {
const [value, setValue] = useAtom(searchQueryAtom);
const [inputValue, setInputValue] = useState('');
const debouncedValue = useDebouncedValue(inputValue, DEBOUNCE_DELAY);
const setSearchQuery = useSetAtom(searchQueryAtom);

useEffect(() => {
setSearchQuery(debouncedValue);
}, [debouncedValue, setSearchQuery]);

const onChange: React.ChangeEventHandler<HTMLInputElement> = (e) => {
setValue(e.target.value);
setInputValue(e.target.value);
};

const onClear = () => setValue('');

const onClear = () => {
setSearchQuery('')
setInputValue('');
};

return {
value,
value: inputValue,
onChange,
onClear,
};
Expand Down

0 comments on commit 6adf11b

Please sign in to comment.