2021-05-28 01:46:27 +08:00
|
|
|
import React, { useContext } from 'react';
|
|
|
|
import PropTypes from 'prop-types';
|
2018-08-10 01:02:18 +08:00
|
|
|
import ChatAlert from './component';
|
2021-05-22 01:52:08 +08:00
|
|
|
import { NLayoutContext } from '../../layout/context/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
|
|
|
|
2021-04-07 03:58:11 +08:00
|
|
|
const ChatAlertContainer = (props) => {
|
2021-05-22 01:52:08 +08:00
|
|
|
const newLayoutContext = useContext(NLayoutContext);
|
2021-05-28 01:46:27 +08:00
|
|
|
const { newLayoutContextState, newLayoutContextDispatch } = newLayoutContext;
|
2021-05-19 22:51:31 +08:00
|
|
|
const { idChatOpen, input } = newLayoutContextState;
|
|
|
|
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
|
|
|
|
? JSON.stringify(activeChats.map(chat => {
|
|
|
|
return {chatId: chat.chatId, unreadCounter: chat.unreadCounter};
|
|
|
|
}))
|
|
|
|
: null;
|
|
|
|
|
|
|
|
// push alerts
|
|
|
|
const unreadMessagesByChat = pushAlertEnabled
|
|
|
|
? JSON.stringify(activeChats.filter((chat) => chat.unreadCounter > 0).map(chat => {
|
|
|
|
const chatId = (chat.chatId === 'public') ? PUBLIC_CHAT_ID : chat.chatId;
|
|
|
|
return UnreadMessages.getUnreadMessages(chatId, groupChatsMessages);
|
|
|
|
}))
|
|
|
|
: null;
|
|
|
|
|
2021-04-07 03:58:11 +08:00
|
|
|
return (
|
2021-05-18 04:25:07 +08:00
|
|
|
<ChatAlert
|
|
|
|
{...props}
|
2021-05-28 01:46:27 +08:00
|
|
|
newLayoutContextDispatch={newLayoutContextDispatch}
|
|
|
|
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;
|