component.tsx 3.5 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192
  1. import type { FC } from 'react'
  2. import { useState } from 'react'
  3. import { useTranslation } from 'react-i18next'
  4. import {
  5. RiMoreFill,
  6. } from '@remixicon/react'
  7. import { useSelectOrDelete, useTrigger } from '../../hooks'
  8. import { UPDATE_HISTORY_EVENT_EMITTER } from '../../constants'
  9. import type { RoleName } from './index'
  10. import { DELETE_HISTORY_BLOCK_COMMAND } from './index'
  11. import { MessageClockCircle } from '@/app/components/base/icons/src/vender/solid/general'
  12. import {
  13. PortalToFollowElem,
  14. PortalToFollowElemContent,
  15. PortalToFollowElemTrigger,
  16. } from '@/app/components/base/portal-to-follow-elem'
  17. import { useEventEmitterContextContext } from '@/context/event-emitter'
  18. type HistoryBlockComponentProps = {
  19. nodeKey: string
  20. roleName?: RoleName
  21. onEditRole: () => void
  22. }
  23. const HistoryBlockComponent: FC<HistoryBlockComponentProps> = ({
  24. nodeKey,
  25. roleName = { user: '', assistant: '' },
  26. onEditRole,
  27. }) => {
  28. const { t } = useTranslation()
  29. const [ref, isSelected] = useSelectOrDelete(nodeKey, DELETE_HISTORY_BLOCK_COMMAND)
  30. const [triggerRef, open, setOpen] = useTrigger()
  31. const { eventEmitter } = useEventEmitterContextContext()
  32. const [localRoleName, setLocalRoleName] = useState<RoleName>(roleName)
  33. eventEmitter?.useSubscription((v: any) => {
  34. if (v?.type === UPDATE_HISTORY_EVENT_EMITTER)
  35. setLocalRoleName(v.payload)
  36. })
  37. return (
  38. <div className={`
  39. group inline-flex items-center pl-1 pr-0.5 h-6 border border-transparent text-[#DD2590] rounded-[5px] hover:bg-[#FCE7F6]
  40. ${open ? 'bg-[#FCE7F6]' : 'bg-[#FDF2FA]'}
  41. ${isSelected && '!border-[#F670C7]'}
  42. `} ref={ref}>
  43. <MessageClockCircle className='mr-1 w-[14px] h-[14px]' />
  44. <div className='mr-1 text-xs font-medium'>{t('common.promptEditor.history.item.title')}</div>
  45. <PortalToFollowElem
  46. open={open}
  47. onOpenChange={setOpen}
  48. placement='top-end'
  49. offset={{
  50. mainAxis: 4,
  51. alignmentAxis: -148,
  52. }}
  53. >
  54. <PortalToFollowElemTrigger ref={triggerRef}>
  55. <div className={`
  56. flex items-center justify-center w-[18px] h-[18px] rounded cursor-pointer
  57. ${open ? 'bg-[#DD2590] text-white' : 'bg-white/50 group-hover:bg-white group-hover:shadow-xs'}
  58. `}>
  59. <RiMoreFill className='w-3 h-3' />
  60. </div>
  61. </PortalToFollowElemTrigger>
  62. <PortalToFollowElemContent style={{ zIndex: 100 }}>
  63. <div className='w-[360px] bg-white rounded-xl shadow-lg'>
  64. <div className='p-4'>
  65. <div className='mb-2 text-xs font-medium text-gray-500'>{t('common.promptEditor.history.modal.title')}</div>
  66. <div className='flex items-center text-sm text-gray-700'>
  67. <div className='mr-1 w-20 text-xs font-semibold'>{localRoleName?.user}</div>
  68. {t('common.promptEditor.history.modal.user')}
  69. </div>
  70. <div className='flex items-center text-sm text-gray-700'>
  71. <div className='mr-1 w-20 text-xs font-semibold'>{localRoleName?.assistant}</div>
  72. {t('common.promptEditor.history.modal.assistant')}
  73. </div>
  74. </div>
  75. <div
  76. className='px-4 py-3 text-xs text-[#155EEF] border-t border-black/5 rounded-b-xl cursor-pointer'
  77. onClick={onEditRole}
  78. >
  79. {t('common.promptEditor.history.modal.edit')}
  80. </div>
  81. </div>
  82. </PortalToFollowElemContent>
  83. </PortalToFollowElem>
  84. </div>
  85. )
  86. }
  87. export default HistoryBlockComponent