node.tsx 1.8 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253
  1. import { type FC, useEffect, useRef, useState } from 'react'
  2. import React from 'react'
  3. import type { KnowledgeRetrievalNodeType } from './types'
  4. import { Folder } from '@/app/components/base/icons/src/vender/solid/files'
  5. import type { NodeProps } from '@/app/components/workflow/types'
  6. import { fetchDatasets } from '@/service/datasets'
  7. import type { DataSet } from '@/models/datasets'
  8. const Node: FC<NodeProps<KnowledgeRetrievalNodeType>> = ({
  9. data,
  10. }) => {
  11. const [selectedDatasets, setSelectedDatasets] = useState<DataSet[]>([])
  12. const updateTime = useRef(0)
  13. useEffect(() => {
  14. (async () => {
  15. updateTime.current = updateTime.current + 1
  16. const currUpdateTime = updateTime.current
  17. if (data.dataset_ids?.length > 0) {
  18. const { data: dataSetsWithDetail } = await fetchDatasets({ url: '/datasets', params: { page: 1, ids: data.dataset_ids } })
  19. // avoid old data overwrite new data
  20. if (currUpdateTime < updateTime.current)
  21. return
  22. setSelectedDatasets(dataSetsWithDetail)
  23. }
  24. else {
  25. setSelectedDatasets([])
  26. }
  27. })()
  28. }, [data.dataset_ids])
  29. if (!selectedDatasets.length)
  30. return null
  31. return (
  32. <div className='mb-1 px-3 py-1'>
  33. <div className='space-y-0.5'>
  34. {selectedDatasets.map(({ id, name }) => (
  35. <div key={id} className='flex items-center h-[26px] bg-workflow-block-parma-bg rounded-md px-1 text-xs font-normal text-gray-700'>
  36. <div className='mr-1 shrink-0 p-1 bg-[#F5F8FF] rounded-md border-[0.5px] border-[#E0EAFF]'>
  37. <Folder className='w-3 h-3 text-[#444CE7]' />
  38. </div>
  39. <div className='grow w-0 text-text-secondary system-xs-regular truncate'>
  40. {name}
  41. </div>
  42. </div>
  43. ))}
  44. </div>
  45. </div>
  46. )
  47. }
  48. export default React.memo(Node)