-
Notifications
You must be signed in to change notification settings - Fork 10.9k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
9 changed files
with
203 additions
and
206 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
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
This file was deleted.
Oops, something went wrong.
78 changes: 78 additions & 0 deletions
78
apps/meteor/client/hooks/useMessageActionAppsActionButtons.ts
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,78 @@ | ||
import { type IUIActionButton, MessageActionContext as AppsEngineMessageActionContext } from '@rocket.chat/apps-engine/definition/ui'; | ||
import type { IMessage } from '@rocket.chat/core-typings'; | ||
import { useToastMessageDispatch } from '@rocket.chat/ui-contexts'; | ||
import type { UseQueryResult } from '@tanstack/react-query'; | ||
import { useMemo } from 'react'; | ||
import { useTranslation } from 'react-i18next'; | ||
|
||
import { useAppActionButtons, getIdForActionButton } from './useAppActionButtons'; | ||
import { useApplyButtonFilters } from './useApplyButtonFilters'; | ||
import { UiKitTriggerTimeoutError } from '../../app/ui-message/client/UiKitTriggerTimeoutError'; | ||
import type { MessageActionContext, MessageActionConfig } from '../../app/ui-utils/client/lib/MessageAction'; | ||
import { Utilities } from '../../ee/lib/misc/Utilities'; | ||
import { useUiKitActionManager } from '../uikit/hooks/useUiKitActionManager'; | ||
|
||
const filterActionsByContext = (context: string | undefined, action: IUIActionButton) => { | ||
if (!context) { | ||
return true; | ||
} | ||
|
||
const messageActionContext = action.when?.messageActionContext || Object.values(AppsEngineMessageActionContext); | ||
const isContextMatch = messageActionContext.includes(context as AppsEngineMessageActionContext); | ||
|
||
return isContextMatch; | ||
}; | ||
|
||
export const useMessageActionAppsActionButtons = (message: IMessage, context?: MessageActionContext, category?: string) => { | ||
const result = useAppActionButtons('messageAction'); | ||
const actionManager = useUiKitActionManager(); | ||
const applyButtonFilters = useApplyButtonFilters(category); | ||
const dispatchToastMessage = useToastMessageDispatch(); | ||
const { t } = useTranslation(); | ||
const data = useMemo( | ||
() => | ||
result.data | ||
?.filter((action) => filterActionsByContext(context, action)) | ||
.filter((action) => applyButtonFilters(action)) | ||
.map((action) => { | ||
const item: MessageActionConfig = { | ||
icon: undefined as any, | ||
id: getIdForActionButton(action), | ||
label: Utilities.getI18nKeyForApp(action.labelI18n, action.appId), | ||
order: 7, | ||
type: 'apps', | ||
variant: action.variant, | ||
group: 'menu', | ||
action: () => { | ||
void actionManager | ||
.emitInteraction(action.appId, { | ||
type: 'actionButton', | ||
rid: message.rid, | ||
tmid: message.tmid, | ||
mid: message._id, | ||
actionId: action.actionId, | ||
payload: { context: action.context }, | ||
}) | ||
.catch(async (reason) => { | ||
if (reason instanceof UiKitTriggerTimeoutError) { | ||
dispatchToastMessage({ | ||
type: 'error', | ||
message: t('UIKit_Interaction_Timeout'), | ||
}); | ||
return; | ||
} | ||
|
||
return reason; | ||
}); | ||
}, | ||
}; | ||
|
||
return item; | ||
}), | ||
[actionManager, applyButtonFilters, context, dispatchToastMessage, message._id, message.rid, message.tmid, result.data, t], | ||
); | ||
return { | ||
...result, | ||
data, | ||
} as UseQueryResult<MessageActionConfig[]>; | ||
}; |
62 changes: 62 additions & 0 deletions
62
apps/meteor/client/hooks/useMessageboxAppsActionButtons.ts
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,62 @@ | ||
import { useToastMessageDispatch } from '@rocket.chat/ui-contexts'; | ||
import type { UseQueryResult } from '@tanstack/react-query'; | ||
import { useMemo } from 'react'; | ||
import { useTranslation } from 'react-i18next'; | ||
|
||
import { useAppActionButtons, getIdForActionButton } from './useAppActionButtons'; | ||
import { useApplyButtonFilters } from './useApplyButtonFilters'; | ||
import { UiKitTriggerTimeoutError } from '../../app/ui-message/client/UiKitTriggerTimeoutError'; | ||
import type { MessageBoxAction } from '../../app/ui-utils/client/lib/messageBox'; | ||
import { Utilities } from '../../ee/lib/misc/Utilities'; | ||
import { useUiKitActionManager } from '../uikit/hooks/useUiKitActionManager'; | ||
|
||
export const useMessageboxAppsActionButtons = (): UseQueryResult<MessageBoxAction[]> => { | ||
const result = useAppActionButtons('messageBoxAction'); | ||
const actionManager = useUiKitActionManager(); | ||
const dispatchToastMessage = useToastMessageDispatch(); | ||
const { t } = useTranslation(); | ||
|
||
const applyButtonFilters = useApplyButtonFilters(); | ||
|
||
const data = useMemo( | ||
() => | ||
result.data | ||
?.filter((action) => { | ||
return applyButtonFilters(action); | ||
}) | ||
.map((action) => { | ||
const item: Omit<MessageBoxAction, 'icon'> = { | ||
id: getIdForActionButton(action), | ||
label: Utilities.getI18nKeyForApp(action.labelI18n, action.appId), | ||
action: (params) => { | ||
void actionManager | ||
.emitInteraction(action.appId, { | ||
type: 'actionButton', | ||
rid: params.rid, | ||
tmid: params.tmid, | ||
actionId: action.actionId, | ||
payload: { context: action.context, message: params.chat.composer?.text ?? '' }, | ||
}) | ||
.catch(async (reason) => { | ||
if (reason instanceof UiKitTriggerTimeoutError) { | ||
dispatchToastMessage({ | ||
type: 'error', | ||
message: t('UIKit_Interaction_Timeout'), | ||
}); | ||
return; | ||
} | ||
|
||
return reason; | ||
}); | ||
}, | ||
}; | ||
|
||
return item; | ||
}), | ||
[actionManager, applyButtonFilters, dispatchToastMessage, result.data, t], | ||
); | ||
return { | ||
...result, | ||
data, | ||
} as UseQueryResult<MessageBoxAction[]>; | ||
}; |
Oops, something went wrong.