2023-09-04 09:49:39 +03:00
|
|
|
import './search-command.css';
|
|
|
|
|
2023-09-09 09:09:50 +03:00
|
|
|
import { memo } from 'preact/compat';
|
2023-09-04 09:49:39 +03:00
|
|
|
import { useRef, useState } from 'preact/hooks';
|
|
|
|
import { useHotkeys } from 'react-hotkeys-hook';
|
|
|
|
|
|
|
|
import SearchForm from './search-form';
|
|
|
|
|
2023-09-09 09:09:50 +03:00
|
|
|
export default memo(function SearchCommand({ onClose = () => {} }) {
|
2023-09-04 09:49:39 +03:00
|
|
|
const [showSearch, setShowSearch] = useState(false);
|
|
|
|
const searchFormRef = useRef(null);
|
|
|
|
|
|
|
|
useHotkeys(
|
|
|
|
'/',
|
|
|
|
(e) => {
|
|
|
|
setShowSearch(true);
|
|
|
|
setTimeout(() => {
|
|
|
|
searchFormRef.current?.focus?.();
|
|
|
|
}, 0);
|
|
|
|
},
|
|
|
|
{
|
|
|
|
preventDefault: true,
|
|
|
|
ignoreEventWhen: (e) => {
|
|
|
|
const isSearchPage = /\/search/.test(location.hash);
|
|
|
|
const hasModal = !!document.querySelector('#modal-container > *');
|
|
|
|
return isSearchPage || hasModal;
|
|
|
|
},
|
|
|
|
},
|
|
|
|
);
|
|
|
|
|
|
|
|
const closeSearch = () => {
|
|
|
|
setShowSearch(false);
|
|
|
|
onClose();
|
|
|
|
};
|
|
|
|
|
|
|
|
useHotkeys(
|
|
|
|
'esc',
|
|
|
|
(e) => {
|
|
|
|
searchFormRef.current?.blur?.();
|
|
|
|
closeSearch();
|
|
|
|
},
|
|
|
|
{
|
|
|
|
enabled: showSearch,
|
|
|
|
enableOnFormTags: true,
|
|
|
|
preventDefault: true,
|
|
|
|
},
|
|
|
|
);
|
|
|
|
|
|
|
|
return (
|
|
|
|
<div
|
|
|
|
id="search-command-container"
|
|
|
|
hidden={!showSearch}
|
|
|
|
onClick={(e) => {
|
|
|
|
console.log(e);
|
|
|
|
if (e.target === e.currentTarget) {
|
|
|
|
closeSearch();
|
|
|
|
}
|
|
|
|
}}
|
|
|
|
>
|
|
|
|
<SearchForm
|
|
|
|
ref={searchFormRef}
|
|
|
|
onSubmit={() => {
|
|
|
|
closeSearch();
|
|
|
|
}}
|
|
|
|
/>
|
|
|
|
</div>
|
|
|
|
);
|
2023-09-09 09:09:50 +03:00
|
|
|
});
|