-
Notifications
You must be signed in to change notification settings - Fork 10k
/
MessageActionMenu.tsx
69 lines (62 loc) · 2.06 KB
/
MessageActionMenu.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
import { MessageToolboxItem, Option } from '@rocket.chat/fuselage';
import { useTranslation } from '@rocket.chat/ui-contexts';
import React, { FC, useState, Fragment, useRef, ComponentProps } from 'react';
import { MessageActionConfig } from '../../../../../../app/ui-utils/client/lib/MessageAction';
import { ToolboxDropdown } from './ToolboxDropdown';
type MessageActionConfigOption = Omit<MessageActionConfig, 'condition' | 'context' | 'order'>;
export const MessageActionMenu: FC<{
options: MessageActionConfig[];
}> = ({ options, ...rest }) => {
const ref = useRef(null);
const t = useTranslation();
const [visible, setVisible] = useState(false);
const groupOptions = options
.map(({ color, ...option }) => ({
...option,
...(color === 'alert' && { variant: 'danger' as const }),
}))
.reduce((acc, option) => {
const group = option.variant ? option.variant : '';
acc[group] = acc[group] || [];
acc[group].push(option);
return acc;
}, {} as { [key: string]: MessageActionConfigOption[] }) as {
[key: string]: MessageActionConfigOption[];
};
const messagesContainer = document.querySelector('.messages-container') || document.body;
return (
<MessageToolboxItem
ref={ref}
icon='kebab'
onClick={(e): void => {
e.stopPropagation();
setVisible(!visible);
}}
data-qa-id='menu'
data-qa-type='message-action-menu'
title={t('More')}
>
{visible && (
<ToolboxDropdown reference={ref} container={messagesContainer} {...rest}>
{Object.entries(groupOptions).map(([, options], index, arr) => (
<Fragment key={index}>
{options.map((option) => (
<Option
variant={option.variant}
key={option.id}
id={option.id}
icon={option.icon as ComponentProps<typeof Option>['icon']}
label={t(option.label)}
onClick={option.action as any}
data-qa-type='message-action'
data-qa-id={option.id}
/>
))}
{index !== arr.length - 1 && <Option.Divider />}
</Fragment>
))}
</ToolboxDropdown>
)}
</MessageToolboxItem>
);
};