-
Notifications
You must be signed in to change notification settings - Fork 298
Expand file tree
/
Copy pathMessageRepliesCountButton.tsx
More file actions
71 lines (60 loc) · 2.39 KB
/
Copy pathMessageRepliesCountButton.tsx
File metadata and controls
71 lines (60 loc) · 2.39 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
import type { MouseEventHandler } from 'react';
import type { UserResponse } from 'stream-chat';
import React, { useMemo } from 'react';
import { useTranslationContext } from '../../context/TranslationContext';
import { useChannelStateContext, useComponentContext } from '../../context';
import { AvatarStack as DefaultAvatarStack } from '../Avatar';
import { extractDisplayInfo as defaultExtractDisplayInfo } from '../Avatar/utils';
export type MessageRepliesCountButtonProps = {
/* If supplied, adds custom text to the end of a multiple replies message */
labelPlural?: string;
/* If supplied, adds custom text to the end of a single reply message */
labelSingle?: string;
/* Function to navigate into an existing thread on a message */
onClick?: MouseEventHandler;
/* The amount of replies (i.e., threaded messages) on a message */
reply_count?: number;
thread_participants?: UserResponse[];
};
function UnMemoizedMessageRepliesCountButton(props: MessageRepliesCountButtonProps) {
const {
AvatarStack = DefaultAvatarStack,
extractDisplayInfo = defaultExtractDisplayInfo,
} = useComponentContext(MessageRepliesCountButton.name);
const {
labelPlural,
labelSingle,
onClick,
reply_count: replyCount = 0,
thread_participants: threadParticipants = [],
} = props;
const { channelCapabilities } = useChannelStateContext();
const { t } = useTranslationContext('MessageRepliesCountButton');
const avatarStackDisplayInfo = useMemo(
() => threadParticipants.slice(0, 3).map((user) => extractDisplayInfo({ user })),
[extractDisplayInfo, threadParticipants],
);
if (!replyCount) return null;
let replyCountText = t('replyCount', { count: replyCount });
if (labelPlural && replyCount > 1) {
replyCountText = `${replyCount} ${labelPlural}`;
} else if (labelSingle) {
replyCountText = `1 ${labelSingle}`;
}
return (
<div className='str-chat__message-replies-count-button-wrapper'>
<button
className='str-chat__message-replies-count-button'
data-testid='replies-count-button'
disabled={!channelCapabilities['send-reply']}
onClick={onClick}
>
{replyCountText}
<AvatarStack displayInfo={avatarStackDisplayInfo} size='xs' />
</button>
</div>
);
}
export const MessageRepliesCountButton = React.memo(
UnMemoizedMessageRepliesCountButton,
) as typeof UnMemoizedMessageRepliesCountButton;