Skip to content

Commit

Permalink
fix: Fix null checks
Browse files Browse the repository at this point in the history
  • Loading branch information
tjtanjin committed Sep 29, 2024
1 parent 1dd12ab commit f56b76b
Show file tree
Hide file tree
Showing 13 changed files with 34 additions and 34 deletions.
6 changes: 3 additions & 3 deletions src/components/ChatBotBody/ChatBotBody.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -83,8 +83,8 @@ const ChatBotBody = ({
const toastPromptContainerStyle: CSSProperties = {
bottom: 20,
width: 300,
minWidth: (styles.chatWindowStyle?.width as number || 375) / 2,
maxWidth: (styles.chatWindowStyle?.width as number || 375) - 50,
minWidth: (styles.chatWindowStyle?.width as number ?? 375) / 2,
maxWidth: (styles.chatWindowStyle?.width as number ?? 375) - 50,
...styles.toastPromptContainerStyle
};

Expand Down Expand Up @@ -151,7 +151,7 @@ const ChatBotBody = ({
}
const { scrollTop, clientHeight, scrollHeight } = chatBodyRef.current;
setIsScrolling(
scrollTop + clientHeight < scrollHeight - (settings.chatWindow?.messagePromptOffset || 30)
scrollTop + clientHeight < scrollHeight - (settings.chatWindow?.messagePromptOffset ?? 30)
);

// workaround to ensure user never truly scrolls to bottom by introducing a 1 pixel offset
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -143,7 +143,7 @@ const UserCheckboxes = ({
if (checkboxes.sendOutput) {
sendInChat = checkboxes.sendOutput;
} else {
sendInChat = settings.chatInput?.sendCheckboxOutput || true;
sendInChat = settings.chatInput?.sendCheckboxOutput ?? true;
}
handleSubmitText(userInput, sendInChat);
}}
Expand Down
2 changes: 1 addition & 1 deletion src/components/ChatBotBody/UserOptions/UserOptions.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -110,7 +110,7 @@ const UserOptions = ({
if (options.sendOutput) {
sendInChat = options.sendOutput;
} else {
sendInChat = settings.chatInput?.sendOptionOutput || true;
sendInChat = settings.chatInput?.sendOptionOutput ?? true;
}
handleSubmitText(key, sendInChat);
}}
Expand Down
10 changes: 5 additions & 5 deletions src/components/ChatBotContainer.tsx
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { MouseEvent } from "react";
import { MouseEvent, useMemo } from "react";

import ChatBotHeader from "./ChatBotHeader/ChatBotHeader";
import ChatBotBody from "./ChatBotBody/ChatBotBody";
Expand Down Expand Up @@ -64,15 +64,15 @@ const ChatBotContainer = ({
/**
* Retrieves class name for window state.
*/
const getWindowStateClass = () => {
const windowStateClass = useMemo(() => {
const windowClass = "rcb-chat-bot ";
if (settings.general?.embedded) {
return windowClass + "rcb-window-embedded";
} else if (isChatWindowOpen) {
return windowClass + "rcb-window-open";
}
return windowClass + "rcb-window-close"
}
return windowClass + "rcb-window-close";
}, [settings, isChatWindowOpen]);

/**
* Retrieves styles for chat window.
Expand Down Expand Up @@ -114,7 +114,7 @@ const ChatBotContainer = ({
// if not on mobile, should remove focus
isDesktop ? inputRef.current?.blur() : event?.preventDefault();
}}
className={getWindowStateClass()}
className={windowStateClass}
>
<ChatBotTooltip/>
<ChatBotButton/>
Expand Down
6 changes: 3 additions & 3 deletions src/components/ChatBotTooltip/ChatBotTooltip.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -39,8 +39,8 @@ const ChatBotTooltip = () => {
if (isDesktop) {
let offset;
if (isChatWindowOpen) {
offset = (styles.chatWindowStyle?.width as number || 375) -
(styles.chatButtonStyle?.width as number || 75)
offset = (styles.chatWindowStyle?.width as number ?? 375) -
(styles.chatButtonStyle?.width as number ?? 75)
} else {
offset = 0;
}
Expand Down Expand Up @@ -71,7 +71,7 @@ const ChatBotTooltip = () => {
// styles for tooltip
const tooltipStyle: React.CSSProperties = {
transform: `translateX(-${tooltipOffset}px)`,
right: (styles.chatButtonStyle?.width as number || 75) + 40,
right: (styles.chatButtonStyle?.width as number ?? 75) + 40,
bottom: 30,
backgroundColor: settings.general?.secondaryColor,
color: settings.general?.secondaryColor,
Expand Down
12 changes: 6 additions & 6 deletions src/context/BotStatesContext.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -72,11 +72,11 @@ const BotStatesProvider = ({
settings?: Settings;
}) => {
const [isBotTyping, setIsBotTyping] = useState<boolean>(false);
const [isChatWindowOpen, setIsChatWindowOpen] = useState<boolean>(settings?.chatWindow?.defaultOpen || false);
const [audioToggledOn, setAudioToggledOn] = useState<boolean>(settings?.audio?.defaultToggledOn || false);
const [voiceToggledOn, setVoiceToggledOn] = useState<boolean>(settings?.voice?.defaultToggledOn || false);
const [isChatWindowOpen, setIsChatWindowOpen] = useState<boolean>(settings?.chatWindow?.defaultOpen ?? false);
const [audioToggledOn, setAudioToggledOn] = useState<boolean>(settings?.audio?.defaultToggledOn ?? false);
const [voiceToggledOn, setVoiceToggledOn] = useState<boolean>(settings?.voice?.defaultToggledOn ?? false);
const [notificationsToggledOn, setNotificationsToggledOn] = useState<boolean>(
settings?.notification?.defaultToggledOn || true
settings?.notification?.defaultToggledOn ?? true
);
const [isLoadingChatHistory, setIsLoadingChatHistory] = useState<boolean>(false);
const [isScrolling, setIsScrolling] = useState<boolean>(false);
Expand All @@ -91,9 +91,9 @@ const BotStatesProvider = ({
const [timeoutId, setTimeoutId] = useState<ReturnType<typeof setTimeout> | null>(null);
// tracks view port height and width (for auto-resizing on mobile view)
const [viewportHeight, setViewportHeight] = useState<number>(window.visualViewport?.height as number
|| window.innerHeight);
?? window.innerHeight);
const [viewportWidth, setViewportWidth] = useState<number>(window.visualViewport?.width as number
|| window.innerWidth);
?? window.innerWidth);

return (
<BotStatesContext.Provider value={{
Expand Down
2 changes: 1 addition & 1 deletion src/hooks/internal/useMessagesInternal.ts
Original file line number Diff line number Diff line change
Expand Up @@ -210,7 +210,7 @@ export const useMessagesInternal = () => {

return updatedMessages;
});
return streamMessageMap.current.get(sender) || null;
return streamMessageMap.current.get(sender) ?? null;
},[]);

/**
Expand Down
2 changes: 1 addition & 1 deletion src/hooks/internal/useNotificationsInternal.ts
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ export const useNotificationInternal = () => {
const notificationSound = settings.notification?.sound;
audioContextRef.current = new AudioContext();
const gainNode = audioContextRef.current.createGain();
gainNode.gain.value = settings.notification?.volume || 0.2;
gainNode.gain.value = settings.notification?.volume ?? 0.2;
gainNodeRef.current = gainNode;

let audioSource;
Expand Down
4 changes: 2 additions & 2 deletions src/hooks/internal/useSubmitInputInternal.ts
Original file line number Diff line number Diff line change
Expand Up @@ -78,7 +78,7 @@ export const useSubmitInputInternal = () => {
if (settings?.sensitiveInput?.hideInUserBubble) {
return;
} else if (settings?.sensitiveInput?.maskInUserBubble) {
await injectMessage("*".repeat(settings.sensitiveInput?.asterisksCount as number || 10), "user");
await injectMessage("*".repeat(settings.sensitiveInput?.asterisksCount as number ?? 10), "user");
return;
}
}
Expand Down Expand Up @@ -176,7 +176,7 @@ export const useSubmitInputInternal = () => {
*/
const handleSubmitText = useCallback(async (inputText?: string, sendInChat = true) => {
// if no user input provided, grab from text area
inputText = inputText || inputRef.current?.value as string;
inputText = inputText ?? inputRef.current?.value as string;

// handles user send text event
if (settings.event?.rcbUserSubmitText) {
Expand Down
2 changes: 1 addition & 1 deletion src/hooks/useFlow.ts
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ export const useFlow = () => {
* Retrieves the conversation flow for the chatbot.
*/
const getFlow = () => {
return flowRef.current || {};
return flowRef.current ?? {};
}

return {
Expand Down
2 changes: 1 addition & 1 deletion src/hooks/useToast.ts
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ export const useToast = () => {
const currentToasts = toastsRef.current;
const numToast = currentToasts.length;

if (numToast >= (settings.toast?.maxCount || 3)) {
if (numToast >= (settings.toast?.maxCount ?? 3)) {
if (settings.toast?.forbidOnMax) {
return null;
}
Expand Down
14 changes: 7 additions & 7 deletions src/services/ChatHistoryService.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -141,7 +141,7 @@ const loadChatHistory = (settings: Settings, styles: Styles, chatHistory: string
}
return [...parsedMessages, lineBreakMessage, ...prevMessages];
});
setTextAreaDisabled(settings.chatInput?.disabled || false);
setTextAreaDisabled(settings.chatInput?.disabled ?? false);
}, 500)
} catch {
// remove chat history on error (to address corrupted storage values)
Expand Down Expand Up @@ -240,8 +240,8 @@ const addStyleToOptions = (classList: DOMTokenList, attributes: {[key: string]:
if (classList.contains("rcb-options")) {
attributes["style"] = {
...(attributes["style"] as CSSProperties),
color: styles.botOptionStyle?.color || settings.general?.primaryColor,
borderColor: styles.botOptionStyle?.color || settings.general?.primaryColor,
color: styles.botOptionStyle?.color ?? settings.general?.primaryColor,
borderColor: styles.botOptionStyle?.color ?? settings.general?.primaryColor,
cursor: `url(${settings.general?.actionDisabledIcon}), auto`,
...styles.botOptionStyle
}
Expand All @@ -261,8 +261,8 @@ const addStyleToCheckboxRows = (classList: DOMTokenList, attributes: {[key: stri
if (classList.contains("rcb-checkbox-row-container")) {
attributes["style"] = {
...(attributes["style"] as CSSProperties),
color: styles.botCheckboxRowStyle?.color || settings.general?.primaryColor,
borderColor: styles.botCheckboxRowStyle?.color || settings.general?.primaryColor,
color: styles.botCheckboxRowStyle?.color ?? settings.general?.primaryColor,
borderColor: styles.botCheckboxRowStyle?.color ?? settings.general?.primaryColor,
cursor: `url(${settings.general?.actionDisabledIcon}), auto`,
...styles.botCheckboxRowStyle
}
Expand All @@ -282,8 +282,8 @@ const addStyleToCheckboxNextButton = (classList: DOMTokenList, attributes: {[key
if (classList.contains("rcb-checkbox-next-button")) {
attributes["style"] = {
...(attributes["style"] as CSSProperties),
color: styles.botCheckboxNextStyle?.color || settings.general?.primaryColor,
borderColor: styles.botCheckboxNextStyle?.color || settings.general?.primaryColor,
color: styles.botCheckboxNextStyle?.color ?? settings.general?.primaryColor,
borderColor: styles.botCheckboxNextStyle?.color ?? settings.general?.primaryColor,
cursor: `url(${settings.general?.actionDisabledIcon}), auto`,
...styles.botCheckboxNextStyle
}
Expand Down
4 changes: 2 additions & 2 deletions src/utils/displayChecker.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,8 @@ export const isChatBotVisible = (element: HTMLDivElement) => {
}

const rect = element.getBoundingClientRect();
const windowHeight = (window.innerHeight || document.documentElement.clientHeight);
const windowWidth = (window.innerWidth || document.documentElement.clientWidth);
const windowHeight = (window.innerHeight ?? document.documentElement.clientHeight);
const windowWidth = (window.innerWidth ?? document.documentElement.clientWidth);

return (
rect.top >= 0 &&
Expand Down

0 comments on commit f56b76b

Please sign in to comment.