You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

hooks.tsx 1.5KB

12345678910111213141516171819202122232425262728293031
  1. import { useCallback, useMemo, useState } from 'react'
  2. import type { DataSourceNodeType } from '@/app/components/workflow/nodes/data-source/types'
  3. import type { DataSourceItem } from '@/app/components/workflow/block-selector/types'
  4. import { basePath } from '@/utils/var'
  5. import { useDataSourceList } from '@/service/use-pipeline'
  6. import { useDatasetDetailContextWithSelector } from '@/context/dataset-detail'
  7. import type { ToolWithProvider } from '@/app/components/workflow/types'
  8. import { transformDataSourceToTool } from '@/app/components/workflow/block-selector/utils'
  9. export const useDatasourceIcon = (data: DataSourceNodeType) => {
  10. const pipelineId = useDatasetDetailContextWithSelector(s => s.dataset?.pipeline_id)
  11. const [dataSourceList, setDataSourceList] = useState<ToolWithProvider[]>([])
  12. const handleUpdateDataSourceList = useCallback((dataSourceList: DataSourceItem[]) => {
  13. dataSourceList.forEach((item) => {
  14. const icon = item.declaration.identity.icon
  15. if (typeof icon == 'string' && !icon.includes(basePath))
  16. item.declaration.identity.icon = `${basePath}${icon}`
  17. })
  18. const formattedDataSourceList = dataSourceList.map(item => transformDataSourceToTool(item))
  19. setDataSourceList!(formattedDataSourceList)
  20. }, [])
  21. useDataSourceList(!!pipelineId, handleUpdateDataSourceList)
  22. const datasourceIcon = useMemo(() => {
  23. return dataSourceList?.find(toolWithProvider => toolWithProvider.plugin_id === data.plugin_id)?.icon
  24. }, [data, dataSourceList])
  25. return datasourceIcon
  26. }