import React, { PropsWithChildren, useRef, useState } from 'react'; import { useTranslation } from 'react-i18next'; import TencentCloudChat from '@tencentcloud/chat'; import './styles/index.scss'; import { Plugins, PluginsProps } from '../Plugins'; import { Icon, IconTypes } from '../Icon'; import { useTUIChatStateContext, useTUIMessageContext } from '../../context'; import { useMessagePluginElement, useMessageHandler } from './hooks'; import { MESSAGE_FLOW, MESSAGE_STATUS } from '../../constants'; enum PluginsNameEnum { quote = 'quote', forward = 'forward', copy = 'copy', delete = 'delete', resend = 'resend', revoke = 'revoke', } export type MessagePluginConfigProps = { [propsName in PluginsNameEnum]?: { isShow?: boolean; relateMessageType?: TencentCloudChat.TYPES[], }; }; export interface MessagePluginsProps extends PluginsProps { config?: MessagePluginConfigProps } export function MessagePlugins ( props:PropsWithChildren, ):React.ReactElement { const { plugins: propsPlugins, showNumber: propsShowNumber, MoreIcon: propsMoreIcon, config: propsPluginConfig, } = props; const { t } = useTranslation(); const [className, setClassName] = useState(''); const [popStyle, setPopStyle] = useState({}); const pluginsRef = useRef(null); const { message, plugin: contextPlugin } = useTUIMessageContext('MessagePlugins'); const { messageListRef } = useTUIChatStateContext('MessageBubbleWithContext'); const { handleDelMessage, handleRevokeMessage, handleReplyMessage, handleCopyMessage, handleResendMessage, handleForWardMessage, } = useMessageHandler({ message }); const pluginConfig = { quote: { isShow: true, ...propsPluginConfig?.quote, ...contextPlugin?.config?.quote, }, forward: { isShow: true, ...propsPluginConfig?.forward, ...contextPlugin?.config?.forward, }, copy: { isShow: true, relateMessageType: [TencentCloudChat.TYPES.MSG_TEXT], ...propsPluginConfig?.copy, ...contextPlugin?.config?.copy, }, delete: { isShow: true, ...propsPluginConfig?.delete, ...contextPlugin?.config?.delete, }, revoke: { isShow: true, ...propsPluginConfig?.revoke, ...contextPlugin?.config?.revoke, }, resend: { isShow: true, ...propsPluginConfig?.resend, ...contextPlugin?.config?.resend, }, }; const handleVisible = (data) => { if (data.x && data.y) { setPopStyle({ position: 'fixed', left: `${!data.left ? (data.x - data.width) : data.x}px`, top: `${!data.top ? (data.y - data.height) : data.y}px`, }); } }; const RevokeElement = useMessagePluginElement({ children: (
{t('TUIChat.Recall')}
), handle: (e) => { pluginsRef.current.closeMore(); handleRevokeMessage(e); }, message, isShow: pluginConfig.revoke.isShow && (message?.status === MESSAGE_STATUS.SUCCESS && message.flow === MESSAGE_FLOW.OUT), relateMessageType: pluginConfig.revoke.relateMessageType, }); const DeleteElement = useMessagePluginElement({ children: (
{t('TUIChat.Delete')}
), handle: (e) => { pluginsRef.current.closeMore(); handleDelMessage(e); }, message, isShow: pluginConfig.delete.isShow && message?.status === MESSAGE_STATUS.SUCCESS, relateMessageType: pluginConfig.delete.relateMessageType, }); const ReplyElement = useMessagePluginElement({ children: (
{t('TUIChat.Reference')}
), handle: (e) => { pluginsRef.current.closeMore(); handleReplyMessage(e); }, message, isShow: pluginConfig.quote.isShow && message?.status === MESSAGE_STATUS.SUCCESS, relateMessageType: pluginConfig.quote.relateMessageType, }); const CopyElement = useMessagePluginElement({ children: (
{t('TUIChat.Copy')}
), handle: (e) => { pluginsRef.current.closeMore(); handleCopyMessage(e); }, message, isShow: pluginConfig.copy.isShow && message?.status === MESSAGE_STATUS.SUCCESS, relateMessageType: pluginConfig.copy.relateMessageType, }); const ResendElement = useMessagePluginElement({ children: (
{t('TUIChat.Resend')}
), handle: (e) => { pluginsRef.current.closeMore(); handleResendMessage(e); }, message, isShow: pluginConfig.resend.isShow && message?.status !== MESSAGE_STATUS.SUCCESS, relateMessageType: pluginConfig.resend.relateMessageType, }); const ForWardElement = useMessagePluginElement({ children: (
{t('TUIChat.Forward')}
), handle: (e) => { pluginsRef.current.closeMore(); handleForWardMessage(e); }, message, isShow: pluginConfig.forward.isShow && message?.status === MESSAGE_STATUS.SUCCESS, relateMessageType: pluginConfig.forward.relateMessageType, }); const defaultPlugins = [ RevokeElement, ReplyElement, ForWardElement, DeleteElement, ResendElement, CopyElement, ]; const plugins = (propsPlugins || contextPlugin?.plugins || defaultPlugins).filter((item) => item); const MoreIcon = propsMoreIcon || contextPlugin?.MoreIcon || ; const showNumber = propsShowNumber || contextPlugin?.showNumber || 0; return message?.status !== MESSAGE_STATUS.UNSEND && ( ); }