2023-03-29 00:08:02 +08:00
|
|
|
import React, { useCallback, useEffect, useState } from 'react';
|
2023-03-18 01:40:49 -04:00
|
|
|
import { debounce } from 'lodash';
|
|
|
|
|
import { Search } from 'lucide-react';
|
2023-03-29 00:08:02 +08:00
|
|
|
import { useRecoilState } from 'recoil';
|
2023-03-28 20:36:21 +08:00
|
|
|
|
|
|
|
|
import store from '~/store';
|
2023-03-18 01:40:49 -04:00
|
|
|
|
2023-03-18 17:49:24 -04:00
|
|
|
export default function SearchBar({ fetch, clearSearch }) {
|
2023-03-28 20:36:21 +08:00
|
|
|
// const dispatch = useDispatch();
|
|
|
|
|
const [inputValue, setInputValue] = useState('');
|
2023-03-29 00:08:02 +08:00
|
|
|
const [searchQuery, setSearchQuery] = useRecoilState(store.searchQuery);
|
2023-03-28 20:36:21 +08:00
|
|
|
|
2023-03-22 16:06:11 -04:00
|
|
|
// const [inputValue, setInputValue] = useState('');
|
2023-03-18 14:28:10 -04:00
|
|
|
|
2023-03-18 01:40:49 -04:00
|
|
|
const debouncedChangeHandler = useCallback(
|
2023-03-28 20:36:21 +08:00
|
|
|
debounce(q => {
|
|
|
|
|
setSearchQuery(q);
|
2023-03-18 01:40:49 -04:00
|
|
|
}, 750),
|
2023-03-28 20:36:21 +08:00
|
|
|
[setSearchQuery]
|
2023-03-18 01:40:49 -04:00
|
|
|
);
|
|
|
|
|
|
2023-03-29 00:08:02 +08:00
|
|
|
useEffect(() => {
|
|
|
|
|
if (searchQuery.length > 0) {
|
|
|
|
|
fetch(searchQuery, 1);
|
|
|
|
|
setInputValue(searchQuery);
|
|
|
|
|
}
|
|
|
|
|
}, [searchQuery]);
|
|
|
|
|
|
2023-03-28 20:36:21 +08:00
|
|
|
const handleKeyUp = e => {
|
2023-03-18 01:40:49 -04:00
|
|
|
const { value } = e.target;
|
2023-03-28 20:36:21 +08:00
|
|
|
if (e.keyCode === 8 && value === '') {
|
2023-03-18 01:40:49 -04:00
|
|
|
// Value after clearing input: ""
|
|
|
|
|
console.log(`Value after clearing input: "${value}"`);
|
2023-03-28 20:36:21 +08:00
|
|
|
setSearchQuery('');
|
2023-03-18 14:28:10 -04:00
|
|
|
clearSearch();
|
2023-03-28 20:36:21 +08:00
|
|
|
}
|
2023-03-18 01:40:49 -04:00
|
|
|
};
|
|
|
|
|
|
2023-03-28 20:36:21 +08:00
|
|
|
const changeHandler = e => {
|
2023-03-18 01:40:49 -04:00
|
|
|
let q = e.target.value;
|
2023-03-28 20:36:21 +08:00
|
|
|
setInputValue(q);
|
2023-03-18 01:40:49 -04:00
|
|
|
q = q.trim();
|
|
|
|
|
|
2023-03-18 14:28:10 -04:00
|
|
|
if (q === '') {
|
2023-03-28 20:36:21 +08:00
|
|
|
setSearchQuery('');
|
2023-03-18 14:28:10 -04:00
|
|
|
clearSearch();
|
2023-03-18 01:40:49 -04:00
|
|
|
} else {
|
|
|
|
|
debouncedChangeHandler(q);
|
|
|
|
|
}
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
return (
|
|
|
|
|
<div className="flex cursor-pointer items-center gap-3 rounded-md py-3 px-3 text-sm text-white transition-colors duration-200 hover:bg-gray-500/10">
|
|
|
|
|
{<Search className="h-4 w-4" />}
|
|
|
|
|
<input
|
|
|
|
|
// ref={inputRef}
|
|
|
|
|
type="text"
|
|
|
|
|
className="m-0 mr-0 w-full border-none bg-transparent p-0 text-sm leading-tight outline-none"
|
|
|
|
|
value={inputValue}
|
|
|
|
|
onChange={changeHandler}
|
|
|
|
|
placeholder="Search messages"
|
|
|
|
|
onKeyUp={handleKeyUp}
|
|
|
|
|
// onBlur={onRename}
|
|
|
|
|
/>
|
|
|
|
|
</div>
|
|
|
|
|
);
|
|
|
|
|
}
|