diff --git a/src/components/Chat/ChatContent/Message/CommandPrompt/CommandPrompt.tsx b/src/components/Chat/ChatContent/Message/CommandPrompt/CommandPrompt.tsx index 47cf653..e4c67e6 100644 --- a/src/components/Chat/ChatContent/Message/CommandPrompt/CommandPrompt.tsx +++ b/src/components/Chat/ChatContent/Message/CommandPrompt/CommandPrompt.tsx @@ -1,9 +1,12 @@ import React, { useEffect, useRef, useState } from 'react'; import useStore from '@store/store'; + import { useTranslation } from 'react-i18next'; import { matchSorter } from 'match-sorter'; import { Prompt } from '@type/prompt'; +import useHideOnOutsideClick from '@hooks/useHideOnOutsideClick'; + const CommandPrompt = ({ _setContent, }: { @@ -11,10 +14,10 @@ const CommandPrompt = ({ }) => { const { t } = useTranslation(); const prompts = useStore((state) => state.prompts); - const [dropDown, setDropDown] = useState(false); const [_prompts, _setPrompts] = useState(prompts); const [input, setInput] = useState(''); - const dropdownRef = useRef(null); + + const [dropDown, setDropDown, dropDownRef] = useHideOnOutsideClick(); useEffect(() => { const filteredPrompts = matchSorter(useStore.getState().prompts, input, { @@ -28,29 +31,8 @@ const CommandPrompt = ({ setInput(''); }, [prompts]); - useEffect(() => { - const handleClickOutside = (event: MouseEvent) => { - if ( - dropdownRef.current && - !dropdownRef.current.contains(event.target as Node) - ) { - setDropDown(false); - } - }; - - if (dropDown) { - document.addEventListener('mousedown', handleClickOutside); - } else { - document.removeEventListener('mousedown', handleClickOutside); - } - - return () => { - document.removeEventListener('mousedown', handleClickOutside); - }; - }, [dropdownRef, dropDown]); - return ( -
+