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-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) => {
|
|
|
|
chatsTracker[c[0]] = {};
|
|
|
|
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;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
});
|
|
|
|
|
|
|
|
const prevTracker = usePrevious(chatsTracker);
|
|
|
|
|
|
|
|
if (prevTracker) {
|
|
|
|
const keys = Object.keys(prevTracker);
|
|
|
|
keys.forEach((key) => {
|
|
|
|
if (chatsTracker[key].count > prevTracker[key].count) {
|
|
|
|
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;
|