refactor: Refactor useDatasourceIcon hook and enhance dataset node rendering with AppIcon component
parent
5d7a533ada
commit
0284e7556e
@ -1,31 +1,23 @@
|
|||||||
import { useCallback, useMemo, useState } from 'react'
|
import { useMemo } from 'react'
|
||||||
import type { DataSourceNodeType } from '@/app/components/workflow/nodes/data-source/types'
|
import type { DataSourceNodeType } from '@/app/components/workflow/nodes/data-source/types'
|
||||||
import type { DataSourceItem } from '@/app/components/workflow/block-selector/types'
|
|
||||||
import { basePath } from '@/utils/var'
|
import { basePath } from '@/utils/var'
|
||||||
import { useDataSourceList } from '@/service/use-pipeline'
|
import { useDataSourceList } from '@/service/use-pipeline'
|
||||||
import { useDatasetDetailContextWithSelector } from '@/context/dataset-detail'
|
|
||||||
import type { ToolWithProvider } from '@/app/components/workflow/types'
|
|
||||||
import { transformDataSourceToTool } from '@/app/components/workflow/block-selector/utils'
|
import { transformDataSourceToTool } from '@/app/components/workflow/block-selector/utils'
|
||||||
|
|
||||||
export const useDatasourceIcon = (data: DataSourceNodeType) => {
|
export const useDatasourceIcon = (data: DataSourceNodeType) => {
|
||||||
const pipelineId = useDatasetDetailContextWithSelector(s => s.dataset?.pipeline_id)
|
const { data: dataSourceListData, isSuccess } = useDataSourceList(true)
|
||||||
const [dataSourceList, setDataSourceList] = useState<ToolWithProvider[]>([])
|
|
||||||
|
|
||||||
const handleUpdateDataSourceList = useCallback((dataSourceList: DataSourceItem[]) => {
|
const datasourceIcon = useMemo(() => {
|
||||||
|
if (!isSuccess) return
|
||||||
|
const dataSourceList = [...(dataSourceListData || [])]
|
||||||
dataSourceList.forEach((item) => {
|
dataSourceList.forEach((item) => {
|
||||||
const icon = item.declaration.identity.icon
|
const icon = item.declaration.identity.icon
|
||||||
if (typeof icon == 'string' && !icon.includes(basePath))
|
if (typeof icon == 'string' && !icon.includes(basePath))
|
||||||
item.declaration.identity.icon = `${basePath}${icon}`
|
item.declaration.identity.icon = `${basePath}${icon}`
|
||||||
})
|
})
|
||||||
const formattedDataSourceList = dataSourceList.map(item => transformDataSourceToTool(item))
|
const formattedDataSourceList = dataSourceList.map(item => transformDataSourceToTool(item))
|
||||||
setDataSourceList!(formattedDataSourceList)
|
return formattedDataSourceList?.find(toolWithProvider => toolWithProvider.plugin_id === data.plugin_id)?.icon
|
||||||
}, [])
|
}, [data.plugin_id, dataSourceListData, isSuccess])
|
||||||
|
|
||||||
useDataSourceList(!!pipelineId, handleUpdateDataSourceList)
|
|
||||||
|
|
||||||
const datasourceIcon = useMemo(() => {
|
|
||||||
return dataSourceList?.find(toolWithProvider => toolWithProvider.plugin_id === data.plugin_id)?.icon
|
|
||||||
}, [data, dataSourceList])
|
|
||||||
|
|
||||||
return datasourceIcon
|
return datasourceIcon
|
||||||
}
|
}
|
||||||
|
|||||||
Loading…
Reference in New Issue