MessageActions.js 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140
  1. /*
  2. Copyright (C) 2025 QuantumNous
  3. This program is free software: you can redistribute it and/or modify
  4. it under the terms of the GNU Affero General Public License as
  5. published by the Free Software Foundation, either version 3 of the
  6. License, or (at your option) any later version.
  7. This program is distributed in the hope that it will be useful,
  8. but WITHOUT ANY WARRANTY; without even the implied warranty of
  9. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  10. GNU Affero General Public License for more details.
  11. You should have received a copy of the GNU Affero General Public License
  12. along with this program. If not, see <https://www.gnu.org/licenses/>.
  13. For commercial licensing, please contact support@quantumnous.com
  14. */
  15. import React from 'react';
  16. import {
  17. Button,
  18. Tooltip,
  19. } from '@douyinfe/semi-ui';
  20. import {
  21. RefreshCw,
  22. Copy,
  23. Trash2,
  24. UserCheck,
  25. Edit,
  26. } from 'lucide-react';
  27. import { useTranslation } from 'react-i18next';
  28. const MessageActions = ({
  29. message,
  30. styleState,
  31. onMessageReset,
  32. onMessageCopy,
  33. onMessageDelete,
  34. onRoleToggle,
  35. onMessageEdit,
  36. isAnyMessageGenerating = false,
  37. isEditing = false
  38. }) => {
  39. const { t } = useTranslation();
  40. const isLoading = message.status === 'loading' || message.status === 'incomplete';
  41. const shouldDisableActions = isAnyMessageGenerating || isEditing;
  42. const canToggleRole = message.role === 'assistant' || message.role === 'system';
  43. const canEdit = !isLoading && message.content && typeof onMessageEdit === 'function' && !isEditing;
  44. return (
  45. <div className="flex items-center gap-0.5">
  46. {!isLoading && (
  47. <Tooltip content={shouldDisableActions ? t('操作暂时被禁用') : t('重试')} position="top">
  48. <Button
  49. theme="borderless"
  50. type="tertiary"
  51. size="small"
  52. icon={<RefreshCw size={styleState.isMobile ? 12 : 14} />}
  53. onClick={() => !shouldDisableActions && onMessageReset(message)}
  54. disabled={shouldDisableActions}
  55. className={`!rounded-full ${shouldDisableActions ? '!text-gray-300 !cursor-not-allowed' : '!text-gray-400 hover:!text-blue-600 hover:!bg-blue-50'} ${styleState.isMobile ? '!w-6 !h-6' : '!w-7 !h-7'} !p-0 transition-all`}
  56. aria-label={t('重试')}
  57. />
  58. </Tooltip>
  59. )}
  60. {message.content && (
  61. <Tooltip content={t('复制')} position="top">
  62. <Button
  63. theme="borderless"
  64. type="tertiary"
  65. size="small"
  66. icon={<Copy size={styleState.isMobile ? 12 : 14} />}
  67. onClick={() => onMessageCopy(message)}
  68. className={`!rounded-full !text-gray-400 hover:!text-green-600 hover:!bg-green-50 ${styleState.isMobile ? '!w-6 !h-6' : '!w-7 !h-7'} !p-0 transition-all`}
  69. aria-label={t('复制')}
  70. />
  71. </Tooltip>
  72. )}
  73. {canEdit && (
  74. <Tooltip content={shouldDisableActions ? t('操作暂时被禁用') : t('编辑')} position="top">
  75. <Button
  76. theme="borderless"
  77. type="tertiary"
  78. size="small"
  79. icon={<Edit size={styleState.isMobile ? 12 : 14} />}
  80. onClick={() => !shouldDisableActions && onMessageEdit(message)}
  81. disabled={shouldDisableActions}
  82. className={`!rounded-full ${shouldDisableActions ? '!text-gray-300 !cursor-not-allowed' : '!text-gray-400 hover:!text-yellow-600 hover:!bg-yellow-50'} ${styleState.isMobile ? '!w-6 !h-6' : '!w-7 !h-7'} !p-0 transition-all`}
  83. aria-label={t('编辑')}
  84. />
  85. </Tooltip>
  86. )}
  87. {canToggleRole && !isLoading && (
  88. <Tooltip
  89. content={
  90. shouldDisableActions
  91. ? t('操作暂时被禁用')
  92. : message.role === 'assistant'
  93. ? t('切换为System角色')
  94. : t('切换为Assistant角色')
  95. }
  96. position="top"
  97. >
  98. <Button
  99. theme="borderless"
  100. type="tertiary"
  101. size="small"
  102. icon={<UserCheck size={styleState.isMobile ? 12 : 14} />}
  103. onClick={() => !shouldDisableActions && onRoleToggle && onRoleToggle(message)}
  104. disabled={shouldDisableActions}
  105. className={`!rounded-full ${shouldDisableActions ? '!text-gray-300 !cursor-not-allowed' : message.role === 'system' ? '!text-purple-500 hover:!text-purple-700 hover:!bg-purple-50' : '!text-gray-400 hover:!text-purple-600 hover:!bg-purple-50'} ${styleState.isMobile ? '!w-6 !h-6' : '!w-7 !h-7'} !p-0 transition-all`}
  106. aria-label={message.role === 'assistant' ? t('切换为System角色') : t('切换为Assistant角色')}
  107. />
  108. </Tooltip>
  109. )}
  110. {!isLoading && (
  111. <Tooltip content={shouldDisableActions ? t('操作暂时被禁用') : t('删除')} position="top">
  112. <Button
  113. theme="borderless"
  114. type="tertiary"
  115. size="small"
  116. icon={<Trash2 size={styleState.isMobile ? 12 : 14} />}
  117. onClick={() => !shouldDisableActions && onMessageDelete(message)}
  118. disabled={shouldDisableActions}
  119. className={`!rounded-full ${shouldDisableActions ? '!text-gray-300 !cursor-not-allowed' : '!text-gray-400 hover:!text-red-600 hover:!bg-red-50'} ${styleState.isMobile ? '!w-6 !h-6' : '!w-7 !h-7'} !p-0 transition-all`}
  120. aria-label={t('删除')}
  121. />
  122. </Tooltip>
  123. )}
  124. </div>
  125. );
  126. };
  127. export default MessageActions;