2023-09-14 00:37:40 +08:00
|
|
|
/* eslint-disable @typescript-eslint/ban-ts-comment */
|
2023-05-26 23:25:17 +08:00
|
|
|
import React, { useEffect } from 'react';
|
2023-07-08 04:46:36 +08:00
|
|
|
import { PopupContainer, PopupContents } from './styles';
|
2023-05-25 21:21:02 +08:00
|
|
|
import { GET_WELCOME_MESSAGE, WelcomeMsgsResponse } from './queries';
|
|
|
|
import { useQuery } from '@apollo/client';
|
|
|
|
import PopupContent from './popup-content/component';
|
2023-07-08 04:46:36 +08:00
|
|
|
import { layoutSelect } from '../../../layout/context';
|
|
|
|
import { Layout } from '../../../layout/layoutTypes';
|
2023-09-01 22:24:33 +08:00
|
|
|
import { ChatCommands } from '/imports/ui/core/enums/chat';
|
2023-05-25 21:21:02 +08:00
|
|
|
|
|
|
|
interface ChatPopupProps {
|
2023-09-14 00:37:40 +08:00
|
|
|
welcomeMessage: string | null;
|
|
|
|
welcomeMsgForModerators: string | null;
|
2023-05-25 21:21:02 +08:00
|
|
|
}
|
|
|
|
|
2023-07-08 04:46:36 +08:00
|
|
|
const WELCOME_MSG_KEY = 'welcomeMsg';
|
|
|
|
const WELCOME_MSG_FOR_MODERATORS_KEY = 'welcomeMsgForModerators';
|
2023-08-15 04:52:35 +08:00
|
|
|
// @ts-ignore - temporary, while meteor exists in the project
|
2024-03-07 01:28:18 +08:00
|
|
|
const CHAT_CONFIG = window.meetingClientSettings.public.chat;
|
2023-07-08 04:46:36 +08:00
|
|
|
const PUBLIC_GROUP_CHAT_KEY = CHAT_CONFIG.public_group_id;
|
|
|
|
|
|
|
|
const setWelcomeMsgsOnSession = (key: string, value: boolean) => {
|
|
|
|
sessionStorage.setItem(key, String(value));
|
|
|
|
};
|
|
|
|
|
2023-09-14 00:37:40 +08:00
|
|
|
const isBoolean = (v: unknown): boolean => {
|
2023-07-08 04:46:36 +08:00
|
|
|
if (v === 'true') {
|
|
|
|
return true;
|
2023-09-14 00:37:40 +08:00
|
|
|
} if (v === 'false') {
|
2023-07-08 04:46:36 +08:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
// if v is not difined it shouldn't be considered on comparation, so it returns true
|
|
|
|
return true;
|
2023-09-14 00:37:40 +08:00
|
|
|
};
|
2023-07-08 04:46:36 +08:00
|
|
|
|
2023-05-25 21:21:02 +08:00
|
|
|
const ChatPopup: React.FC<ChatPopupProps> = ({
|
|
|
|
welcomeMessage,
|
|
|
|
welcomeMsgForModerators,
|
|
|
|
}) => {
|
2023-07-08 04:46:36 +08:00
|
|
|
const [showWelcomeMessage, setShowWelcomeMessage] = React.useState(
|
2023-09-14 00:37:40 +08:00
|
|
|
welcomeMessage && isBoolean(sessionStorage.getItem(WELCOME_MSG_KEY)),
|
2023-07-08 04:46:36 +08:00
|
|
|
);
|
|
|
|
const [showWelcomeMessageForModerators, setShowWelcomeMessageForModerators] = React.useState(
|
2023-09-14 00:37:40 +08:00
|
|
|
welcomeMsgForModerators && isBoolean(sessionStorage.getItem(WELCOME_MSG_FOR_MODERATORS_KEY)),
|
2023-07-08 04:46:36 +08:00
|
|
|
);
|
2023-05-26 23:25:17 +08:00
|
|
|
useEffect(() => {
|
2023-07-08 04:46:36 +08:00
|
|
|
const eventCallback = () => {
|
|
|
|
if (welcomeMessage) {
|
|
|
|
setShowWelcomeMessage(true);
|
|
|
|
setWelcomeMsgsOnSession(WELCOME_MSG_KEY, true);
|
|
|
|
}
|
|
|
|
if (welcomeMsgForModerators) {
|
|
|
|
setShowWelcomeMessageForModerators(true);
|
|
|
|
setWelcomeMsgsOnSession(WELCOME_MSG_FOR_MODERATORS_KEY, true);
|
|
|
|
}
|
|
|
|
};
|
2023-09-01 22:24:33 +08:00
|
|
|
window.addEventListener(ChatCommands.RESTORE_WELCOME_MESSAGES, eventCallback);
|
2023-07-08 04:46:36 +08:00
|
|
|
|
|
|
|
return () => {
|
2023-09-14 00:37:40 +08:00
|
|
|
// eslint-disable-next-line no-restricted-globals
|
2023-09-01 22:24:33 +08:00
|
|
|
removeEventListener(ChatCommands.RESTORE_WELCOME_MESSAGES, eventCallback);
|
2023-09-14 00:37:40 +08:00
|
|
|
};
|
2023-05-26 23:25:17 +08:00
|
|
|
}, []);
|
|
|
|
if (!showWelcomeMessage && !showWelcomeMessageForModerators) return null;
|
2023-05-25 21:21:02 +08:00
|
|
|
return (
|
|
|
|
<PopupContainer>
|
2023-07-08 04:46:36 +08:00
|
|
|
<PopupContents>
|
|
|
|
{showWelcomeMessage && welcomeMessage && (
|
|
|
|
<PopupContent
|
|
|
|
message={welcomeMessage}
|
|
|
|
closePopup={() => {
|
|
|
|
setShowWelcomeMessage(false);
|
|
|
|
setWelcomeMsgsOnSession(WELCOME_MSG_KEY, false);
|
|
|
|
}}
|
2023-05-25 21:21:02 +08:00
|
|
|
/>
|
2023-07-08 04:46:36 +08:00
|
|
|
)}
|
|
|
|
{showWelcomeMessageForModerators && welcomeMsgForModerators && (
|
|
|
|
<PopupContent
|
|
|
|
message={welcomeMsgForModerators}
|
|
|
|
closePopup={() => {
|
|
|
|
setShowWelcomeMessageForModerators(false);
|
|
|
|
setWelcomeMsgsOnSession(WELCOME_MSG_FOR_MODERATORS_KEY, false);
|
|
|
|
}}
|
|
|
|
/>
|
|
|
|
)}
|
|
|
|
</PopupContents>
|
|
|
|
|
2023-05-25 21:21:02 +08:00
|
|
|
</PopupContainer>
|
|
|
|
);
|
|
|
|
};
|
|
|
|
|
|
|
|
const ChatPopupContainer: React.FC = () => {
|
|
|
|
const {
|
2023-07-08 04:46:36 +08:00
|
|
|
data: welcomeData,
|
|
|
|
loading: welcomeLoading,
|
|
|
|
error: welcomeError,
|
|
|
|
} = useQuery<WelcomeMsgsResponse>(GET_WELCOME_MESSAGE);
|
|
|
|
const idChatOpen = layoutSelect((i: Layout) => i.idChatOpen);
|
|
|
|
if (idChatOpen !== PUBLIC_GROUP_CHAT_KEY) return null;
|
|
|
|
|
|
|
|
if (welcomeLoading) return null;
|
|
|
|
if (welcomeError) return <div>{JSON.stringify(welcomeError)}</div>;
|
|
|
|
if (!welcomeData) return null;
|
2023-05-25 21:21:02 +08:00
|
|
|
|
|
|
|
return (
|
|
|
|
<ChatPopup
|
|
|
|
welcomeMessage={welcomeData.user_welcomeMsgs[0]?.welcomeMsg}
|
|
|
|
welcomeMsgForModerators={welcomeData.user_welcomeMsgs[0]?.welcomeMsgForModerators}
|
|
|
|
/>
|
|
|
|
);
|
|
|
|
};
|
|
|
|
|
2023-09-14 00:37:40 +08:00
|
|
|
export default ChatPopupContainer;
|