-
Notifications
You must be signed in to change notification settings - Fork 297
Expand file tree
/
Copy pathMessageOptions.tsx
More file actions
112 lines (98 loc) · 4.01 KB
/
MessageOptions.tsx
File metadata and controls
112 lines (98 loc) · 4.01 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
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
import clsx from 'clsx';
import React from 'react';
import {
ActionsIcon as DefaultActionsIcon,
ReactionIcon as DefaultReactionIcon,
ThreadIcon as DefaultThreadIcon,
} from './icons';
import { MESSAGE_ACTIONS } from './utils';
import { MessageActions } from '../MessageActions';
import { useDialogIsOpen } from '../Dialog';
import { ReactionSelectorWithButton } from '../Reactions/ReactionSelectorWithButton';
import { useMessageContext, useTranslationContext } from '../../context';
import type { IconProps } from '../../types/types';
import type { MessageContextValue } from '../../context/MessageContext';
export type MessageOptionsProps = Partial<
Pick<MessageContextValue, '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;
/* 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 = (props: MessageOptionsProps) => {
const {
ActionsIcon = DefaultActionsIcon,
displayReplies = true,
handleOpenThread: propHandleOpenThread,
ReactionIcon = DefaultReactionIcon,
theme = 'simple',
ThreadIcon = DefaultThreadIcon,
} = props;
const {
getMessageActions,
handleOpenThread: contextHandleOpenThread,
initialMessage,
message,
threadList,
} = useMessageContext('MessageOptions');
const { t } = useTranslationContext('MessageOptions');
// It is necessary to namespace the dialog IDs because a message with the same ID
// can appear in the main message list as well as in the thread message list.
// Without the namespace, the search for dialog would be performed by the message ID only
// which could return the dialog for a message in another message list (which would not be rendered).
const dialogIdNamespace = threadList ? '-thread-' : '';
const messageActionsDialogIsOpen = useDialogIsOpen(
`message-actions${dialogIdNamespace}--${message.id}`,
);
const reactionSelectorDialogIsOpen = useDialogIsOpen(
`reaction-selector${dialogIdNamespace}--${message.id}`,
);
const handleOpenThread = propHandleOpenThread || contextHandleOpenThread;
const messageActions = getMessageActions();
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;
}
return (
<div
className={clsx(`str-chat__message-${theme}__actions str-chat__message-options`, {
'str-chat__message-options--active':
messageActionsDialogIsOpen || reactionSelectorDialogIsOpen,
})}
data-testid='message-options'
>
<MessageActions ActionsIcon={ActionsIcon} />
{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 && <ReactionSelectorWithButton ReactionIcon={ReactionIcon} />}
</div>
);
};
export const MessageOptions = React.memo(
UnMemoizedMessageOptions,
) as typeof UnMemoizedMessageOptions;