Skip to content

Commit

Permalink
refactor: Minor code improvements
Browse files Browse the repository at this point in the history
  • Loading branch information
tjtanjin committed Oct 20, 2024
1 parent 24b4a00 commit 400afb0
Show file tree
Hide file tree
Showing 3 changed files with 14 additions and 21 deletions.
4 changes: 2 additions & 2 deletions src/components/ChatBotContainer.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -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";
Expand Down Expand Up @@ -59,7 +59,7 @@ const ChatBotContainer = ({
const { headerButtons, chatInputButtons, footerButtons } = useButtonInternal();

// loads all use effects
useBotEffectInternal();
useBotEffectsInternal();

/**
* Retrieves class name for window state.
Expand Down
20 changes: 3 additions & 17 deletions src/hooks/internal/useBotEffectsInternal.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -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();

Expand Down Expand Up @@ -53,7 +53,6 @@ export const useBotEffectInternal = () => {
isChatWindowOpen,
isBotTyping,
isScrolling,
timeoutId,
hasFlowStarted,
setIsChatWindowOpen,
setTextAreaDisabled,
Expand All @@ -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();
Expand Down Expand Up @@ -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);
Expand Down Expand Up @@ -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) {
Expand Down Expand Up @@ -293,6 +281,4 @@ export const useBotEffectInternal = () => {
goToPath("start");
}
}, [hasFlowStarted, settings.general?.flowStartTrigger]);

return { timeoutId }
};
11 changes: 9 additions & 2 deletions src/hooks/internal/useChatWindowInternal.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,8 @@ export const useChatWindowInternal = () => {
viewportHeight,
setViewportHeight,
viewportWidth,
setViewportWidth
setViewportWidth,
setUnreadCount
} = useBotStatesContext();

// handles rcb events
Expand All @@ -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;
});
}, []);

/**
Expand Down

0 comments on commit 400afb0

Please sign in to comment.