From 400afb068fc295da98f5551506410c5955851c4f Mon Sep 17 00:00:00 2001 From: tjtanjin Date: Sun, 20 Oct 2024 19:17:35 +0800 Subject: [PATCH] refactor: Minor code improvements --- src/components/ChatBotContainer.tsx | 4 ++-- src/hooks/internal/useBotEffectsInternal.tsx | 20 +++----------------- src/hooks/internal/useChatWindowInternal.ts | 11 +++++++++-- 3 files changed, 14 insertions(+), 21 deletions(-) diff --git a/src/components/ChatBotContainer.tsx b/src/components/ChatBotContainer.tsx index b193172b..68f24a63 100644 --- a/src/components/ChatBotContainer.tsx +++ b/src/components/ChatBotContainer.tsx @@ -8,7 +8,7 @@ import ChatBotButton from "./ChatBotButton/ChatBotButton"; import ChatBotTooltip from "./ChatBotTooltip/ChatBotTooltip"; import { useButtonInternal } from "../hooks/internal/useButtonsInternal"; import { useChatWindowInternal } from "../hooks/internal/useChatWindowInternal"; -import { useBotEffectInternal } from "../hooks/internal/useBotEffectsInternal"; +import { useBotEffectsInternal } from "../hooks/internal/useBotEffectsInternal"; import { useIsDesktopInternal } from "../hooks/internal/useIsDesktopInternal"; import { useBotRefsContext } from "../context/BotRefsContext"; import { useBotStatesContext } from "../context/BotStatesContext"; @@ -59,7 +59,7 @@ const ChatBotContainer = ({ const { headerButtons, chatInputButtons, footerButtons } = useButtonInternal(); // loads all use effects - useBotEffectInternal(); + useBotEffectsInternal(); /** * Retrieves class name for window state. diff --git a/src/hooks/internal/useBotEffectsInternal.tsx b/src/hooks/internal/useBotEffectsInternal.tsx index 7e440e83..a6dff32a 100644 --- a/src/hooks/internal/useBotEffectsInternal.tsx +++ b/src/hooks/internal/useBotEffectsInternal.tsx @@ -25,7 +25,7 @@ import { Params } from "../../types/Params"; /** * Internal custom hook for common use effects. */ -export const useBotEffectInternal = () => { +export const useBotEffectsInternal = () => { // handles platform const isDesktop = useIsDesktopInternal(); @@ -53,7 +53,6 @@ export const useBotEffectInternal = () => { isChatWindowOpen, isBotTyping, isScrolling, - timeoutId, hasFlowStarted, setIsChatWindowOpen, setTextAreaDisabled, @@ -73,7 +72,7 @@ export const useBotEffectInternal = () => { const { viewportHeight, setViewportHeight, setViewportWidth, openChat } = useChatWindowInternal(); // handles notifications - const { playNotificationSound, setUnreadCount, setUpNotifications } = useNotificationInternal(); + const { playNotificationSound, setUpNotifications } = useNotificationInternal(); // handles user first interaction const { handleFirstInteraction } = useFirstInteractionInternal(); @@ -103,13 +102,9 @@ export const useBotEffectInternal = () => { }; }, []); - // default setup for notifications + // default setup for notifications, text area, chat window, audio and voice useEffect(() => { setUpNotifications(); - }, []) - - // default setup for text area, chat window, audio and voice - useEffect(() => { setTextAreaDisabled(settings.chatInput?.disabled as boolean); setIsChatWindowOpen(settings.chatWindow?.defaultOpen as boolean); setAudioToggledOn(settings.audio?.defaultToggledOn as boolean); @@ -196,13 +191,6 @@ export const useBotEffectInternal = () => { playNotificationSound(); }, [messages.length]); - // resets unread count on opening chat - useEffect(() => { - if (isChatWindowOpen) { - setUnreadCount(0); - } - }, [isChatWindowOpen]); - // handles scrolling/resizing window on mobile devices useEffect(() => { if (isDesktop) { @@ -293,6 +281,4 @@ export const useBotEffectInternal = () => { goToPath("start"); } }, [hasFlowStarted, settings.general?.flowStartTrigger]); - - return { timeoutId } }; diff --git a/src/hooks/internal/useChatWindowInternal.ts b/src/hooks/internal/useChatWindowInternal.ts index 51d6be0f..c88f1f8a 100644 --- a/src/hooks/internal/useChatWindowInternal.ts +++ b/src/hooks/internal/useChatWindowInternal.ts @@ -19,7 +19,8 @@ export const useChatWindowInternal = () => { viewportHeight, setViewportHeight, viewportWidth, - setViewportWidth + setViewportWidth, + setUnreadCount } = useBotStatesContext(); // handles rcb events @@ -42,7 +43,13 @@ export const useChatWindowInternal = () => { return; } } - setIsChatWindowOpen(prev => !prev); + setIsChatWindowOpen(prev => { + // if currently false means opening so set unread count to 0 + if (!prev) { + setUnreadCount(0); + } + return !prev; + }); }, []); /**