From b3f421cde9d2953afd0bdd5fb1ba497218243817 Mon Sep 17 00:00:00 2001 From: Tindo Arsel Date: Thu, 20 Apr 2023 15:35:51 +0100 Subject: [PATCH] refactor: Add hideOnClickOutside feature to reuse code (#240) * remove console log * refactor: Add hideOnClickOutside feature to reuse code This change abstracts the code repetition in three different files and promote the DRY principle by adding the hideOnClickOutside feature to the handleClickOutside function. The feature is designed to hide an element when clicking outside of its area. * refactor: hooks --------- Co-authored-by: Jing Hua --- .../Message/CommandPrompt/CommandPrompt.tsx | 30 ++++------------ .../Chat/ChatContent/Message/RoleSelector.tsx | 26 ++------------ src/components/Menu/ChatFolder.tsx | 27 +++----------- src/hooks/useHideOnOutsideClick.ts | 36 +++++++++++++++++++ 4 files changed, 49 insertions(+), 70 deletions(-) create mode 100644 src/hooks/useHideOnOutsideClick.ts 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 ( -
+