-
Notifications
You must be signed in to change notification settings - Fork 298
Expand file tree
/
Copy pathUnreadMessagesNotification.tsx
More file actions
78 lines (73 loc) · 2.44 KB
/
Copy pathUnreadMessagesNotification.tsx
File metadata and controls
78 lines (73 loc) · 2.44 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
import React from 'react';
import { useChannel, useChatContext, useTranslationContext } from '../../context';
import { useMessagePaginator } from '../../hooks';
import { Button } from '../Button';
import { IconArrowUp, IconXmark } from '../Icons';
import clsx from 'clsx';
import { useThreadContext } from '../Threads';
import type { UnreadSnapshotState } from 'stream-chat';
import { useStateStore } from '../../store';
export type UnreadMessagesNotificationProps = {
/**
* Configuration parameter to determine the message page size, when jumping to the first unread message.
*/
queryMessageLimit?: number;
/**
* Configuration parameter to determine, whether the unread count is to be shown on the component. Enabled by default.
*/
showCount?: boolean;
// todo: maybe remove?
unreadCount?: number;
};
const unreadStateSnapshotSelector = (state: UnreadSnapshotState) => ({
unreadCount: state.unreadCount,
});
export const UnreadMessagesNotification = ({
queryMessageLimit,
showCount = true,
}: UnreadMessagesNotificationProps) => {
// todo: move into a hook dedicated to unread count from the snapshot
const channel = useChannel();
const { client } = useChatContext('UnreadMessagesNotification');
const thread = useThreadContext();
const messagePaginator = useMessagePaginator();
const { unreadCount } = useStateStore(
messagePaginator.unreadStateSnapshot,
unreadStateSnapshotSelector,
);
const { t } = useTranslationContext('UnreadMessagesNotification');
return (
<div
className={clsx('str-chat__unread-messages-notification', {
'str-chat__unread-messages-notification--with-count': unreadCount && showCount,
})}
data-testid='unread-messages-notification'
>
<Button
appearance='outline'
onClick={() =>
messagePaginator.jumpToTheFirstUnreadMessage({
pageSize: queryMessageLimit,
})
}
variant='secondary'
>
<IconArrowUp />
{unreadCount && showCount
? t('{{count}} unread', { count: unreadCount })
: t('Unread messages')}
</Button>
<Button
appearance='outline'
aria-label={t('aria/Mark messages as read')}
onClick={() => {
messagePaginator.clearUnreadSnapshot();
client.messageDeliveryReporter.throttledMarkRead(thread ?? channel);
}}
variant='secondary'
>
<IconXmark />
</Button>
</div>
);
};