2024-03-27 21:22:20 +08:00
|
|
|
import React, { useCallback, useEffect, useRef } from 'react';
|
2024-02-24 02:14:12 +08:00
|
|
|
import { isEqual } from 'radash';
|
|
|
|
import { defineMessages, useIntl } from 'react-intl';
|
|
|
|
import { layoutSelect, layoutSelectInput, layoutDispatch } from '/imports/ui/components/layout/context';
|
|
|
|
import { Input, Layout } from '/imports/ui/components/layout/layoutTypes';
|
|
|
|
import { PANELS } from '/imports/ui/components/layout/enums';
|
2024-03-09 04:48:44 +08:00
|
|
|
import usePreviousValue from '/imports/ui/hooks/usePreviousValue';
|
2024-02-24 02:14:12 +08:00
|
|
|
import { stripTags, unescapeHtml } from '/imports/utils/string-utils';
|
|
|
|
import { ChatMessageType } from '/imports/ui/core/enums/chat';
|
|
|
|
import {
|
|
|
|
CHAT_MESSAGE_PRIVATE_STREAM,
|
|
|
|
CHAT_MESSAGE_PUBLIC_STREAM,
|
|
|
|
PublicMessageStreamResponse,
|
|
|
|
PrivateMessageStreamResponse,
|
|
|
|
Message,
|
|
|
|
} from './queries';
|
|
|
|
import ChatPushAlert from './push-alert/component';
|
|
|
|
import Styled from './styles';
|
2024-02-27 20:07:42 +08:00
|
|
|
import Service from './service';
|
2024-06-04 21:40:54 +08:00
|
|
|
import useDeduplicatedSubscription from '/imports/ui/core/hooks/useDeduplicatedSubscription';
|
2024-08-29 21:09:53 +08:00
|
|
|
import useSettings from '/imports/ui/services/settings/hooks/useSettings';
|
|
|
|
import { SETTINGS } from '/imports/ui/services/settings/enums';
|
2024-02-24 02:14:12 +08:00
|
|
|
|
|
|
|
const intlMessages = defineMessages({
|
|
|
|
appToastChatPublic: {
|
|
|
|
id: 'app.toast.chat.public',
|
|
|
|
description: 'when entry various message',
|
|
|
|
},
|
|
|
|
appToastChatPrivate: {
|
|
|
|
id: 'app.toast.chat.private',
|
|
|
|
description: 'when entry various message',
|
|
|
|
},
|
|
|
|
appToastChatSystem: {
|
|
|
|
id: 'app.toast.chat.system',
|
|
|
|
description: 'system for use',
|
|
|
|
},
|
|
|
|
publicChatClear: {
|
|
|
|
id: 'app.chat.clearPublicChatMessage',
|
|
|
|
description: 'message of when clear the public chat',
|
|
|
|
},
|
|
|
|
publicChatMsg: {
|
|
|
|
id: 'app.toast.chat.public',
|
|
|
|
description: 'public chat toast message title',
|
|
|
|
},
|
|
|
|
privateChatMsg: {
|
|
|
|
id: 'app.toast.chat.private',
|
|
|
|
description: 'private chat toast message title',
|
|
|
|
},
|
|
|
|
pollResults: {
|
|
|
|
id: 'app.toast.chat.poll',
|
|
|
|
description: 'chat toast message for polls',
|
|
|
|
},
|
|
|
|
pollResultsClick: {
|
|
|
|
id: 'app.toast.chat.pollClick',
|
|
|
|
description: 'chat toast click message for polls',
|
|
|
|
},
|
|
|
|
});
|
|
|
|
|
|
|
|
const ALERT_DURATION = 4000; // 4 seconds
|
|
|
|
|
2024-08-29 21:09:53 +08:00
|
|
|
interface ChatAlertGraphqlProps {
|
2024-02-24 02:14:12 +08:00
|
|
|
idChatOpen: string;
|
|
|
|
layoutContextDispatch: () => void;
|
|
|
|
publicUnreadMessages: Array<Message> | null;
|
|
|
|
privateUnreadMessages: Array<Message> | null;
|
2024-08-29 21:09:53 +08:00
|
|
|
audioAlertEnabled: boolean;
|
|
|
|
pushAlertEnabled: boolean;
|
2024-02-24 02:14:12 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
const ChatAlertGraphql: React.FC<ChatAlertGraphqlProps> = (props) => {
|
|
|
|
const {
|
|
|
|
audioAlertEnabled,
|
|
|
|
idChatOpen,
|
|
|
|
layoutContextDispatch,
|
|
|
|
pushAlertEnabled,
|
|
|
|
publicUnreadMessages,
|
|
|
|
privateUnreadMessages,
|
|
|
|
} = props;
|
|
|
|
const intl = useIntl();
|
2024-02-27 20:07:42 +08:00
|
|
|
const history = useRef(new Set<string>());
|
2024-02-24 02:14:12 +08:00
|
|
|
const prevPublicUnreadMessages = usePreviousValue(publicUnreadMessages);
|
|
|
|
const prevPrivateUnreadMessages = usePreviousValue(privateUnreadMessages);
|
|
|
|
const publicMessagesDidChange = !isEqual(prevPublicUnreadMessages, publicUnreadMessages);
|
|
|
|
const privateMessagesDidChange = !isEqual(prevPrivateUnreadMessages, privateUnreadMessages);
|
2024-03-27 21:22:20 +08:00
|
|
|
const shouldRenderPublicChatAlerts = publicMessagesDidChange
|
|
|
|
&& !!publicUnreadMessages
|
|
|
|
&& publicUnreadMessages.length > 0;
|
|
|
|
const shouldRenderPrivateChatAlerts = privateMessagesDidChange
|
|
|
|
&& !!privateUnreadMessages
|
|
|
|
&& privateUnreadMessages.length > 0;
|
2024-02-27 20:07:42 +08:00
|
|
|
const shouldPlayAudioAlert = useCallback(
|
2024-03-27 21:22:20 +08:00
|
|
|
(m: Message) => (m.chatId !== idChatOpen || document.hidden) && !history.current.has(m.messageId),
|
2024-02-27 20:07:42 +08:00
|
|
|
[idChatOpen, history.current],
|
|
|
|
);
|
2024-02-24 02:14:12 +08:00
|
|
|
|
2024-05-29 21:26:11 +08:00
|
|
|
const CHAT_CONFIG = window.meetingClientSettings.public.chat;
|
|
|
|
const PUBLIC_CHAT_ID = CHAT_CONFIG.public_id;
|
|
|
|
const PUBLIC_GROUP_CHAT_ID = CHAT_CONFIG.public_group_id;
|
|
|
|
|
2024-03-27 21:22:20 +08:00
|
|
|
useEffect(() => {
|
|
|
|
if (shouldRenderPublicChatAlerts) {
|
|
|
|
publicUnreadMessages.forEach((m) => {
|
|
|
|
history.current.add(m.messageId);
|
|
|
|
});
|
|
|
|
}
|
|
|
|
if (shouldRenderPrivateChatAlerts) {
|
|
|
|
privateUnreadMessages.forEach((m) => {
|
|
|
|
history.current.add(m.messageId);
|
|
|
|
});
|
|
|
|
}
|
|
|
|
});
|
|
|
|
|
2024-02-27 20:07:42 +08:00
|
|
|
let playAudioAlert = false;
|
2024-02-24 02:14:12 +08:00
|
|
|
|
2024-02-27 20:07:42 +08:00
|
|
|
if (shouldRenderPublicChatAlerts) {
|
|
|
|
playAudioAlert = publicUnreadMessages.some(shouldPlayAudioAlert);
|
|
|
|
}
|
|
|
|
if (shouldRenderPrivateChatAlerts && !playAudioAlert) {
|
|
|
|
playAudioAlert = privateUnreadMessages.some(shouldPlayAudioAlert);
|
|
|
|
}
|
2024-02-24 02:14:12 +08:00
|
|
|
|
2024-02-27 20:07:42 +08:00
|
|
|
if (audioAlertEnabled && playAudioAlert) {
|
|
|
|
Service.playAlertSound();
|
|
|
|
}
|
2024-02-24 02:14:12 +08:00
|
|
|
|
|
|
|
const mapTextContent = (msg: Message) => {
|
|
|
|
if (msg.messageType === ChatMessageType.CHAT_CLEAR) {
|
|
|
|
return intl.formatMessage(intlMessages.publicChatClear);
|
|
|
|
}
|
|
|
|
|
|
|
|
return unescapeHtml(stripTags(msg.message));
|
|
|
|
};
|
|
|
|
|
|
|
|
const createMessage = (msg: Message) => (
|
|
|
|
<Styled.PushMessageContent>
|
|
|
|
<Styled.UserNameMessage>{msg.senderName}</Styled.UserNameMessage>
|
|
|
|
<Styled.ContentMessage>
|
|
|
|
{mapTextContent(msg)}
|
|
|
|
</Styled.ContentMessage>
|
|
|
|
</Styled.PushMessageContent>
|
|
|
|
);
|
|
|
|
|
|
|
|
const createPollMessage = () => (
|
|
|
|
<Styled.PushMessageContent>
|
|
|
|
<Styled.UserNameMessage>
|
|
|
|
{intl.formatMessage(intlMessages.pollResults)}
|
|
|
|
</Styled.UserNameMessage>
|
|
|
|
<Styled.ContentMessagePoll>
|
|
|
|
{intl.formatMessage(intlMessages.pollResultsClick)}
|
|
|
|
</Styled.ContentMessagePoll>
|
|
|
|
</Styled.PushMessageContent>
|
|
|
|
);
|
|
|
|
|
2024-02-27 20:07:42 +08:00
|
|
|
const renderToast = (message: Message) => {
|
|
|
|
if (history.current.has(message.messageId)) return null;
|
|
|
|
if (message.chatId === idChatOpen) return null;
|
2024-02-24 02:14:12 +08:00
|
|
|
|
2024-02-27 20:07:42 +08:00
|
|
|
const messageChatId = message.chatId === PUBLIC_GROUP_CHAT_ID ? PUBLIC_CHAT_ID : message.chatId;
|
|
|
|
const isPollResult = message.messageType === ChatMessageType.POLL;
|
|
|
|
let content;
|
2024-02-24 02:14:12 +08:00
|
|
|
|
2024-02-27 20:07:42 +08:00
|
|
|
if (isPollResult) {
|
|
|
|
content = createPollMessage();
|
|
|
|
} else {
|
|
|
|
content = createMessage(message);
|
|
|
|
}
|
|
|
|
|
|
|
|
return (
|
|
|
|
<ChatPushAlert
|
|
|
|
key={messageChatId}
|
|
|
|
chatId={messageChatId}
|
|
|
|
content={content}
|
|
|
|
title={
|
|
|
|
messageChatId === PUBLIC_CHAT_ID
|
|
|
|
? <span>{intl.formatMessage(intlMessages.appToastChatPublic)}</span>
|
|
|
|
: <span>{intl.formatMessage(intlMessages.appToastChatPrivate)}</span>
|
2024-02-24 02:14:12 +08:00
|
|
|
}
|
2024-02-27 20:07:42 +08:00
|
|
|
alertDuration={ALERT_DURATION}
|
|
|
|
layoutContextDispatch={layoutContextDispatch}
|
|
|
|
/>
|
|
|
|
);
|
|
|
|
};
|
2024-02-24 02:14:12 +08:00
|
|
|
|
2024-02-27 20:07:42 +08:00
|
|
|
return pushAlertEnabled
|
|
|
|
? [
|
|
|
|
shouldRenderPublicChatAlerts && publicUnreadMessages.map(renderToast),
|
|
|
|
shouldRenderPrivateChatAlerts && privateUnreadMessages.map(renderToast),
|
2024-02-24 02:14:12 +08:00
|
|
|
]
|
|
|
|
: null;
|
|
|
|
};
|
|
|
|
|
2024-08-29 21:09:53 +08:00
|
|
|
const ChatAlertContainerGraphql: React.FC = () => {
|
2024-02-24 02:14:12 +08:00
|
|
|
const cursor = useRef(new Date());
|
2024-06-04 21:40:54 +08:00
|
|
|
const { data: publicMessages } = useDeduplicatedSubscription<PublicMessageStreamResponse>(
|
2024-02-24 02:14:12 +08:00
|
|
|
CHAT_MESSAGE_PUBLIC_STREAM,
|
|
|
|
{ variables: { createdAt: cursor.current.toISOString() } },
|
|
|
|
);
|
2024-06-04 21:40:54 +08:00
|
|
|
const { data: privateMessages } = useDeduplicatedSubscription<PrivateMessageStreamResponse>(
|
2024-02-24 02:14:12 +08:00
|
|
|
CHAT_MESSAGE_PRIVATE_STREAM,
|
|
|
|
{ variables: { createdAt: cursor.current.toISOString() } },
|
|
|
|
);
|
|
|
|
|
2024-08-29 21:09:53 +08:00
|
|
|
const {
|
|
|
|
chatAudioAlerts,
|
|
|
|
chatPushAlerts,
|
|
|
|
} = useSettings(SETTINGS.APPLICATION) as {
|
|
|
|
chatAudioAlerts: boolean;
|
|
|
|
chatPushAlerts: boolean;
|
|
|
|
};
|
|
|
|
|
2024-02-24 02:14:12 +08:00
|
|
|
const idChatOpen = layoutSelect((i: Layout) => i.idChatOpen);
|
|
|
|
const sidebarContent = layoutSelectInput((i: Input) => i.sidebarContent);
|
|
|
|
const { sidebarContentPanel } = sidebarContent;
|
|
|
|
const layoutContextDispatch = layoutDispatch();
|
|
|
|
|
2024-08-29 21:09:53 +08:00
|
|
|
if (!(chatAudioAlerts || chatPushAlerts)) return null;
|
2024-02-24 02:14:12 +08:00
|
|
|
|
|
|
|
const idChat = sidebarContentPanel === PANELS.CHAT ? idChatOpen : '';
|
|
|
|
|
|
|
|
if (!publicMessages && !privateMessages) return null;
|
|
|
|
|
|
|
|
return (
|
|
|
|
<ChatAlertGraphql
|
2024-08-29 21:09:53 +08:00
|
|
|
audioAlertEnabled={chatAudioAlerts}
|
2024-02-24 02:14:12 +08:00
|
|
|
idChatOpen={idChat}
|
|
|
|
layoutContextDispatch={layoutContextDispatch}
|
2024-08-29 21:09:53 +08:00
|
|
|
pushAlertEnabled={chatPushAlerts}
|
2024-02-24 02:14:12 +08:00
|
|
|
publicUnreadMessages={publicMessages?.chat_message_public_stream ?? null}
|
|
|
|
privateUnreadMessages={privateMessages?.chat_message_private_stream ?? null}
|
|
|
|
/>
|
|
|
|
);
|
|
|
|
};
|
|
|
|
|
|
|
|
export default ChatAlertContainerGraphql;
|