-
Notifications
You must be signed in to change notification settings - Fork 11k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: introduce new categories field for UI Action Buttons (#33066)
Co-authored-by: Tasso Evangelista <[email protected]>
- Loading branch information
Showing
6 changed files
with
143 additions
and
7 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
--- | ||
'@rocket.chat/ui-kit': minor | ||
'@rocket.chat/i18n': minor | ||
'@rocket.chat/meteor': minor | ||
--- | ||
|
||
Introduces new property `category` for Rocket.Chat Apps to register UI action buttons. This property is used to group buttons in the UI. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
89 changes: 89 additions & 0 deletions
89
apps/meteor/client/components/message/toolbar/MessageToolbarStarsActionMenu.tsx
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,89 @@ | ||
import { useUniqueId } from '@rocket.chat/fuselage-hooks'; | ||
import { GenericMenu, type GenericMenuItemProps } from '@rocket.chat/ui-client'; | ||
import { useTranslation } from '@rocket.chat/ui-contexts'; | ||
import type { MouseEvent, ReactElement } from 'react'; | ||
import React from 'react'; | ||
|
||
import type { MessageActionConditionProps, MessageActionConfig } from '../../../../app/ui-utils/client/lib/MessageAction'; | ||
|
||
type MessageActionConfigOption = Omit<MessageActionConfig, 'condition' | 'context' | 'order' | 'action'> & { | ||
action: (e?: MouseEvent<HTMLElement>) => void; | ||
}; | ||
|
||
type MessageActionSection = { | ||
id: string; | ||
title: string; | ||
items: GenericMenuItemProps[]; | ||
}; | ||
|
||
type MessageActionMenuProps = { | ||
onChangeMenuVisibility: (visible: boolean) => void; | ||
options: MessageActionConfigOption[]; | ||
context: MessageActionConditionProps; | ||
isMessageEncrypted: boolean; | ||
}; | ||
|
||
const MessageToolbarStarsActionMenu = ({ | ||
options, | ||
onChangeMenuVisibility, | ||
context, | ||
isMessageEncrypted, | ||
}: MessageActionMenuProps): ReactElement => { | ||
const t = useTranslation(); | ||
const id = useUniqueId(); | ||
|
||
const groupOptions = options.reduce((acc, option) => { | ||
const transformedOption = { | ||
variant: option.color === 'alert' ? 'danger' : '', | ||
id: option.id, | ||
icon: option.icon, | ||
content: t(option.label), | ||
onClick: option.action, | ||
type: option.type, | ||
...(option.disabled && { disabled: option?.disabled?.(context) }), | ||
...(option.disabled && | ||
option?.disabled?.(context) && { tooltip: t('Action_not_available_encrypted_content', { action: t(option.label) }) }), | ||
}; | ||
|
||
const group = option.type || ''; | ||
let section = acc.find((section: { id: string }) => section.id === group); | ||
|
||
if (!section) { | ||
section = { id: group, title: '', items: [] }; | ||
acc.push(section); | ||
} | ||
|
||
// Add option to the appropriate section | ||
section.items.push(transformedOption); | ||
|
||
// Handle the "apps" section if message is encrypted | ||
if (group === 'apps' && isMessageEncrypted) { | ||
section.items = [ | ||
{ | ||
content: t('Unavailable'), | ||
id, | ||
disabled: true, | ||
gap: false, | ||
tooltip: t('Action_not_available_encrypted_content', { action: t('Apps') }), | ||
}, | ||
]; | ||
} | ||
|
||
return acc; | ||
}, [] as MessageActionSection[]); | ||
|
||
return ( | ||
<GenericMenu | ||
onOpenChange={onChangeMenuVisibility} | ||
detached | ||
icon='stars' | ||
title={t('AI_Actions')} | ||
data-qa-id='menu' | ||
data-qa-type='message-action-menu' | ||
sections={groupOptions} | ||
placement='bottom-end' | ||
/> | ||
); | ||
}; | ||
|
||
export default MessageToolbarStarsActionMenu; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,24 @@ | ||
import { MessageActionContext } from '@rocket.chat/apps-engine/definition/ui'; | ||
import type { IUIActionButton } from '@rocket.chat/apps-engine/definition/ui'; | ||
import { useCallback } from 'react'; | ||
|
||
const DEFAULT_CATEGORY = 'default'; | ||
|
||
export const useFilterActionsByContextAndCategory = (context: string | undefined, category = 'default') => { | ||
return useCallback( | ||
(action: IUIActionButton) => { | ||
if (!context) { | ||
return true; | ||
} | ||
|
||
const actionCategory = action?.category ?? DEFAULT_CATEGORY; | ||
const messageActionContext = action.when?.messageActionContext || Object.values(MessageActionContext); | ||
const isContextMatch = messageActionContext.includes(context as MessageActionContext); | ||
|
||
const isCategoryMatch = category === DEFAULT_CATEGORY ? actionCategory === DEFAULT_CATEGORY : actionCategory === category; | ||
|
||
return isContextMatch && isCategoryMatch; | ||
}, | ||
[context, category], | ||
); | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters