-
Notifications
You must be signed in to change notification settings - Fork 283
/
Copy pathMessageOptions.tsx
118 lines (103 loc) · 4.21 KB
/
MessageOptions.tsx
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
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
import React from 'react';
import {
ActionsIcon as DefaultActionsIcon,
ReactionIcon as DefaultReactionIcon,
ThreadIcon as DefaultThreadIcon,
} from './icons';
import { MESSAGE_ACTIONS, showMessageActionsBox } from './utils';
import { MessageActions } from '../MessageActions';
import { MessageContextValue, useMessageContext } from '../../context/MessageContext';
import type { DefaultStreamChatGenerics, IconProps } from '../../types/types';
import { useTranslationContext } from '../../context';
export type MessageOptionsProps<
StreamChatGenerics extends DefaultStreamChatGenerics = DefaultStreamChatGenerics
> = Partial<Pick<MessageContextValue<StreamChatGenerics>, 'handleOpenThread'>> & {
/* Custom component rendering the icon used in message actions button. This button invokes the message actions menu. */
ActionsIcon?: React.ComponentType<IconProps>;
/* If true, show the `ThreadIcon` and enable navigation into a `Thread` component. */
displayReplies?: boolean;
/* React mutable ref that can be placed on the message root `div` of MessageActions component */
messageWrapperRef?: React.RefObject<HTMLDivElement>;
/* Custom component rendering the icon used in a button invoking reactions selector for a given message. */
ReactionIcon?: React.ComponentType<IconProps>;
/* Theme string to be added to CSS class names. */
theme?: string;
/* Custom component rendering the icon used in a message options button opening thread */
ThreadIcon?: React.ComponentType<IconProps>;
};
const UnMemoizedMessageOptions = <
StreamChatGenerics extends DefaultStreamChatGenerics = DefaultStreamChatGenerics
>(
props: MessageOptionsProps<StreamChatGenerics>,
) => {
const {
ActionsIcon = DefaultActionsIcon,
displayReplies = true,
handleOpenThread: propHandleOpenThread,
messageWrapperRef,
ReactionIcon = DefaultReactionIcon,
theme = 'simple',
ThreadIcon = DefaultThreadIcon,
} = props;
const {
customMessageActions,
getMessageActions,
handleOpenThread: contextHandleOpenThread,
initialMessage,
message,
onReactionListClick,
showDetailedReactions,
threadList,
} = useMessageContext<StreamChatGenerics>('MessageOptions');
const { t } = useTranslationContext('MessageOptions');
const handleOpenThread = propHandleOpenThread || contextHandleOpenThread;
const messageActions = getMessageActions();
const showActionsBox =
showMessageActionsBox(messageActions, threadList) || !!customMessageActions;
const shouldShowReactions = messageActions.indexOf(MESSAGE_ACTIONS.react) > -1;
const shouldShowReplies =
messageActions.indexOf(MESSAGE_ACTIONS.reply) > -1 && displayReplies && !threadList;
if (
!message.type ||
message.type === 'error' ||
message.type === 'system' ||
message.type === 'ephemeral' ||
message.status === 'failed' ||
message.status === 'sending' ||
initialMessage
) {
return null;
}
const rootClassName = `str-chat__message-${theme}__actions str-chat__message-options`;
return (
<div className={rootClassName} data-testid='message-options'>
{showActionsBox && (
<MessageActions ActionsIcon={ActionsIcon} messageWrapperRef={messageWrapperRef} />
)}
{shouldShowReplies && (
<button
aria-label={t('aria/Open Thread')}
className={`str-chat__message-${theme}__actions__action str-chat__message-${theme}__actions__action--thread str-chat__message-reply-in-thread-button`}
data-testid='thread-action'
onClick={handleOpenThread}
>
<ThreadIcon className='str-chat__message-action-icon' />
</button>
)}
{shouldShowReactions && (
<button
aria-expanded={showDetailedReactions}
aria-label={t('aria/Open Reaction Selector')}
className={`str-chat__message-${theme}__actions__action str-chat__message-${theme}__actions__action--reactions str-chat__message-reactions-button`}
data-testid='message-reaction-action'
onClick={onReactionListClick}
>
<ReactionIcon className='str-chat__message-action-icon' />
</button>
)}
</div>
);
};
export const MessageOptions = React.memo(
UnMemoizedMessageOptions,
) as typeof UnMemoizedMessageOptions;