2021-05-28 01:46:27 +08:00
|
|
|
import React, { useContext } from 'react';
|
|
|
|
import PropTypes from 'prop-types';
|
2022-01-20 07:02:57 +08:00
|
|
|
import logger from '/imports/startup/client/logger';
|
2022-01-20 08:43:42 +08:00
|
|
|
import Auth from '/imports/ui/services/auth';
|
2018-08-10 01:02:18 +08:00
|
|
|
import ChatAlert from './component';
|
2021-08-05 19:03:24 +08:00
|
|
|
import LayoutContext from '../../layout/context';
|
2021-05-18 04:25:07 +08:00
|
|
|
import { PANELS } from '../../layout/enums';
|
2021-04-07 03:58:11 +08:00
|
|
|
import { UsersContext } from '/imports/ui/components/components-data/users-context/context';
|
2021-04-10 04:35:05 +08:00
|
|
|
import { ChatContext } from '/imports/ui/components/components-data/chat-context/context';
|
|
|
|
import { GroupChatContext } from '/imports/ui/components/components-data/group-chat-context/context';
|
|
|
|
import userListService from '/imports/ui/components/user-list/service';
|
2021-05-28 01:46:27 +08:00
|
|
|
import UnreadMessages from '/imports/ui/services/unread-messages';
|
|
|
|
|
|
|
|
const PUBLIC_CHAT_ID = Meteor.settings.public.chat.public_group_id;
|
|
|
|
|
|
|
|
const propTypes = {
|
|
|
|
audioAlertEnabled: PropTypes.bool.isRequired,
|
|
|
|
pushAlertEnabled: PropTypes.bool.isRequired,
|
|
|
|
};
|
2017-03-17 03:57:45 +08:00
|
|
|
|
2022-01-17 13:01:45 +08:00
|
|
|
// custom hook for getting previous value
|
|
|
|
function usePrevious(value) {
|
|
|
|
const ref = React.useRef();
|
|
|
|
React.useEffect(() => {
|
|
|
|
ref.current = value;
|
|
|
|
});
|
|
|
|
return ref.current;
|
|
|
|
}
|
|
|
|
|
2021-04-07 03:58:11 +08:00
|
|
|
const ChatAlertContainer = (props) => {
|
2021-08-05 19:03:24 +08:00
|
|
|
const layoutContext = useContext(LayoutContext);
|
|
|
|
const { layoutContextState, layoutContextDispatch } = layoutContext;
|
|
|
|
const { idChatOpen, input } = layoutContextState;
|
2021-05-19 22:51:31 +08:00
|
|
|
const { sidebarContent } = input;
|
|
|
|
const { sidebarContentPanel } = sidebarContent;
|
2021-05-28 01:46:27 +08:00
|
|
|
const { audioAlertEnabled, pushAlertEnabled } = props;
|
2021-05-18 04:25:07 +08:00
|
|
|
|
|
|
|
let idChat = idChatOpen;
|
|
|
|
if (sidebarContentPanel !== PANELS.CHAT) idChat = '';
|
|
|
|
|
2021-04-07 03:58:11 +08:00
|
|
|
const usingUsersContext = useContext(UsersContext);
|
2021-04-10 04:35:05 +08:00
|
|
|
const usingChatContext = useContext(ChatContext);
|
|
|
|
const usingGroupChatContext = useContext(GroupChatContext);
|
|
|
|
|
2021-04-07 03:58:11 +08:00
|
|
|
const { users } = usingUsersContext;
|
2021-04-10 04:35:05 +08:00
|
|
|
const { chats: groupChatsMessages } = usingChatContext;
|
|
|
|
const { groupChat: groupChats } = usingGroupChatContext;
|
|
|
|
|
|
|
|
const activeChats = userListService.getActiveChats({ groupChatsMessages, groupChats, users });
|
|
|
|
|
2021-05-28 01:46:27 +08:00
|
|
|
// audio alerts
|
|
|
|
const unreadMessagesCountByChat = audioAlertEnabled
|
2021-05-28 03:11:36 +08:00
|
|
|
? activeChats.map((chat) => ({
|
2021-05-28 02:12:49 +08:00
|
|
|
chatId: chat.chatId, unreadCounter: chat.unreadCounter,
|
2021-05-28 03:11:36 +08:00
|
|
|
}))
|
2021-05-28 02:12:49 +08:00
|
|
|
: null;
|
2021-05-28 01:46:27 +08:00
|
|
|
|
|
|
|
// push alerts
|
|
|
|
const unreadMessagesByChat = pushAlertEnabled
|
2021-05-28 03:11:36 +08:00
|
|
|
? activeChats.filter(
|
|
|
|
(chat) => chat.unreadCounter > 0 && chat.chatId !== idChat,
|
|
|
|
).map((chat) => {
|
2021-05-28 02:12:49 +08:00
|
|
|
const chatId = (chat.chatId === 'public') ? PUBLIC_CHAT_ID : chat.chatId;
|
|
|
|
return UnreadMessages.getUnreadMessages(chatId, groupChatsMessages);
|
2021-05-28 03:11:36 +08:00
|
|
|
})
|
2021-05-28 02:12:49 +08:00
|
|
|
: null;
|
2021-05-28 01:46:27 +08:00
|
|
|
|
2022-01-17 13:01:45 +08:00
|
|
|
const chatsTracker = {};
|
|
|
|
|
|
|
|
if (usingChatContext.chats) {
|
|
|
|
const chatsActive = Object.entries(usingChatContext.chats);
|
|
|
|
chatsActive.forEach((c) => {
|
2022-01-20 07:02:57 +08:00
|
|
|
try {
|
|
|
|
if (c[0] === idChat || (c[0] === 'MAIN-PUBLIC-GROUP-CHAT' && idChat === 'public')) {
|
|
|
|
chatsTracker[c[0]] = {};
|
2022-01-20 08:43:42 +08:00
|
|
|
chatsTracker[c[0]].lastSender = users[Auth.meetingID][c[1]?.lastSender]?.name;
|
2022-01-20 07:02:57 +08:00
|
|
|
if (c[1]?.posJoinMessages || c[1]?.messageGroups) {
|
|
|
|
const m = Object.entries(c[1]?.posJoinMessages || c[1]?.messageGroups);
|
|
|
|
chatsTracker[c[0]].count = m?.length;
|
|
|
|
if (m[m.length - 1]) {
|
|
|
|
chatsTracker[c[0]].content = m[m.length - 1][1]?.message;
|
|
|
|
}
|
2022-01-17 21:51:05 +08:00
|
|
|
}
|
2022-01-17 13:01:45 +08:00
|
|
|
}
|
2022-01-20 07:02:57 +08:00
|
|
|
} catch (e) {
|
|
|
|
logger.error({
|
|
|
|
logCode: 'chat_alert_component_error',
|
|
|
|
}, 'Error : ', e.error);
|
2022-01-17 13:01:45 +08:00
|
|
|
}
|
|
|
|
});
|
|
|
|
|
|
|
|
const prevTracker = usePrevious(chatsTracker);
|
|
|
|
|
|
|
|
if (prevTracker) {
|
|
|
|
const keys = Object.keys(prevTracker);
|
|
|
|
keys.forEach((key) => {
|
2022-01-17 21:51:05 +08:00
|
|
|
if (chatsTracker[key]?.count > prevTracker[key]?.count) {
|
2022-01-17 13:01:45 +08:00
|
|
|
chatsTracker[key].shouldNotify = true;
|
|
|
|
}
|
|
|
|
});
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-04-07 03:58:11 +08:00
|
|
|
return (
|
2021-05-18 04:25:07 +08:00
|
|
|
<ChatAlert
|
|
|
|
{...props}
|
2022-01-17 13:01:45 +08:00
|
|
|
chatsTracker={chatsTracker}
|
2021-08-05 19:03:24 +08:00
|
|
|
layoutContextDispatch={layoutContextDispatch}
|
2021-05-28 01:46:27 +08:00
|
|
|
unreadMessagesCountByChat={unreadMessagesCountByChat}
|
|
|
|
unreadMessagesByChat={unreadMessagesByChat}
|
2021-05-18 04:25:07 +08:00
|
|
|
idChatOpen={idChat}
|
|
|
|
/>
|
2021-04-07 03:58:11 +08:00
|
|
|
);
|
|
|
|
};
|
2017-03-17 03:57:45 +08:00
|
|
|
|
2021-05-28 01:46:27 +08:00
|
|
|
ChatAlertContainer.propTypes = propTypes;
|
2020-03-06 07:14:22 +08:00
|
|
|
|
2021-05-28 01:46:27 +08:00
|
|
|
export default ChatAlertContainer;
|