component.tsx 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104
  1. import type { FC } from 'react'
  2. import { useState } from 'react'
  3. import { useTranslation } from 'react-i18next'
  4. import {
  5. RiAddLine,
  6. } from '@remixicon/react'
  7. import { useSelectOrDelete, useTrigger } from '../../hooks'
  8. import { UPDATE_DATASETS_EVENT_EMITTER } from '../../constants'
  9. import type { Dataset } from './index'
  10. import { DELETE_CONTEXT_BLOCK_COMMAND } from './index'
  11. import { File05, Folder } from '@/app/components/base/icons/src/vender/solid/files'
  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 ContextBlockComponentProps = {
  19. nodeKey: string
  20. datasets?: Dataset[]
  21. onAddContext: () => void
  22. canNotAddContext?: boolean
  23. }
  24. const ContextBlockComponent: FC<ContextBlockComponentProps> = ({
  25. nodeKey,
  26. datasets = [],
  27. onAddContext,
  28. canNotAddContext,
  29. }) => {
  30. const { t } = useTranslation()
  31. const [ref, isSelected] = useSelectOrDelete(nodeKey, DELETE_CONTEXT_BLOCK_COMMAND)
  32. const [triggerRef, open, setOpen] = useTrigger()
  33. const { eventEmitter } = useEventEmitterContextContext()
  34. const [localDatasets, setLocalDatasets] = useState<Dataset[]>(datasets)
  35. eventEmitter?.useSubscription((v: any) => {
  36. if (v?.type === UPDATE_DATASETS_EVENT_EMITTER)
  37. setLocalDatasets(v.payload)
  38. })
  39. return (
  40. <div className={`
  41. group inline-flex items-center pl-1 pr-0.5 h-6 border border-transparent bg-[#F4F3FF] text-[#6938EF] rounded-[5px] hover:bg-[#EBE9FE]
  42. ${open ? 'bg-[#EBE9FE]' : 'bg-[#F4F3FF]'}
  43. ${isSelected && '!border-[#9B8AFB]'}
  44. `} ref={ref}>
  45. <File05 className='mr-1 w-[14px] h-[14px]' />
  46. <div className='mr-1 text-xs font-medium'>{t('common.promptEditor.context.item.title')}</div>
  47. {!canNotAddContext && (
  48. <PortalToFollowElem
  49. open={open}
  50. onOpenChange={setOpen}
  51. placement='bottom-end'
  52. offset={{
  53. mainAxis: 3,
  54. alignmentAxis: -147,
  55. }}
  56. >
  57. <PortalToFollowElemTrigger ref={triggerRef}>
  58. <div className={`
  59. flex items-center justify-center w-[18px] h-[18px] text-[11px] font-semibold rounded cursor-pointer
  60. ${open ? 'bg-[#6938EF] text-white' : 'bg-white/50 group-hover:bg-white group-hover:shadow-xs'}
  61. `}>{localDatasets.length}</div>
  62. </PortalToFollowElemTrigger>
  63. <PortalToFollowElemContent style={{ zIndex: 100 }}>
  64. <div className='w-[360px] bg-white rounded-xl shadow-lg'>
  65. <div className='p-4'>
  66. <div className='mb-2 text-xs font-medium text-gray-500'>
  67. {t('common.promptEditor.context.modal.title', { num: localDatasets.length })}
  68. </div>
  69. <div className='max-h-[270px] overflow-y-auto'>
  70. {
  71. localDatasets.map(dataset => (
  72. <div key={dataset.id} className='flex items-center h-8'>
  73. <div className='flex items-center justify-center shrink-0 mr-2 w-6 h-6 bg-[#F5F8FF] rounded-md border-[0.5px] border-[#EAECF5]'>
  74. <Folder className='w-4 h-4 text-[#444CE7]' />
  75. </div>
  76. <div className='text-sm text-gray-800 truncate' title=''>{dataset.name}</div>
  77. </div>
  78. ))
  79. }
  80. </div>
  81. <div className='flex items-center h-8 text-[#155EEF] cursor-pointer' onClick={onAddContext}>
  82. <div className='shrink-0 flex justify-center items-center mr-2 w-6 h-6 rounded-md border-[0.5px] border-gray-100'>
  83. <RiAddLine className='w-[14px] h-[14px]' />
  84. </div>
  85. <div className='text-[13px] font-medium' title=''>{t('common.promptEditor.context.modal.add')}</div>
  86. </div>
  87. </div>
  88. <div className='px-4 py-3 text-xs text-gray-500 bg-gray-50 border-t-[0.5px] border-gray-50 rounded-b-xl'>
  89. {t('common.promptEditor.context.modal.footer')}
  90. </div>
  91. </div>
  92. </PortalToFollowElemContent>
  93. </PortalToFollowElem>
  94. )}
  95. </div>
  96. )
  97. }
  98. export default ContextBlockComponent