2024-04-06 06:30:58 +08:00
|
|
|
import { useEffect } from 'react';
|
|
|
|
import { isEqual } from 'radash';
|
|
|
|
import { defineMessages, useIntl } from 'react-intl';
|
|
|
|
import { UNREAD_CHATS_SUBSCRIPTION, UnreadChatsSubscriptionResponse } from './queries';
|
|
|
|
import usePreviousValue from '/imports/ui/hooks/usePreviousValue';
|
|
|
|
import { addAlert } from './service';
|
2024-06-04 21:40:54 +08:00
|
|
|
import useDeduplicatedSubscription from '../../core/hooks/useDeduplicatedSubscription';
|
2024-04-06 06:30:58 +08:00
|
|
|
|
|
|
|
const intlMessages = defineMessages({
|
|
|
|
newMsgAria: {
|
|
|
|
id: 'app.navBar.toggleUserList.newMsgAria',
|
|
|
|
description: 'label for new message screen reader alert',
|
|
|
|
},
|
|
|
|
publicChatName: {
|
|
|
|
id: 'app.chat.titlePublic',
|
|
|
|
description: 'label for public chat name',
|
|
|
|
},
|
|
|
|
});
|
|
|
|
|
|
|
|
const ScreenReaderAlertAdapter = () => {
|
2024-06-04 21:40:54 +08:00
|
|
|
const { data, error, loading } = useDeduplicatedSubscription<UnreadChatsSubscriptionResponse>(
|
|
|
|
UNREAD_CHATS_SUBSCRIPTION,
|
|
|
|
);
|
2024-04-06 06:30:58 +08:00
|
|
|
const previousData = usePreviousValue(data);
|
|
|
|
const intl = useIntl();
|
|
|
|
|
|
|
|
useEffect(() => {
|
|
|
|
if (!loading && !error && data && !isEqual(data, previousData)) {
|
|
|
|
const { chat: chats } = data;
|
|
|
|
const { chat: previousChats } = previousData ?? {};
|
|
|
|
|
|
|
|
chats.forEach((chat) => {
|
|
|
|
const previousChat = previousChats && previousChats.find((c) => c.chatId === chat.chatId);
|
|
|
|
|
|
|
|
if (!previousChat || chat.totalUnread > previousChat.totalUnread) {
|
|
|
|
const name = chat.participant?.name ?? intl.formatMessage(intlMessages.publicChatName);
|
|
|
|
addAlert(`${intl.formatMessage(intlMessages.newMsgAria, { 0: name })}`);
|
|
|
|
}
|
|
|
|
});
|
|
|
|
}
|
|
|
|
}, [data, previousData, error, loading]);
|
|
|
|
|
|
|
|
return null;
|
|
|
|
};
|
|
|
|
|
|
|
|
export default ScreenReaderAlertAdapter;
|