refactor: add DatasourceIcon component and update related hooks and options
parent
049a6de4b3
commit
754a1d1197
@ -0,0 +1,40 @@
|
||||
import type { FC } from 'react'
|
||||
import { memo } from 'react'
|
||||
import cn from '@/utils/classnames'
|
||||
|
||||
type DatasourceIconProps = {
|
||||
size?: string
|
||||
className?: string
|
||||
iconUrl: string
|
||||
}
|
||||
|
||||
const ICON_CONTAINER_CLASSNAME_SIZE_MAP: Record<string, string> = {
|
||||
xs: 'w-4 h-4 rounded-[5px] shadow-xs',
|
||||
sm: 'w-5 h-5 rounded-md shadow-xs',
|
||||
md: 'w-6 h-6 rounded-lg shadow-md',
|
||||
}
|
||||
|
||||
const DatasourceIcon: FC<DatasourceIconProps> = ({
|
||||
size = 'sm',
|
||||
className,
|
||||
iconUrl,
|
||||
}) => {
|
||||
return (
|
||||
<div className={
|
||||
cn(
|
||||
'flex items-center justify-center shadow-none',
|
||||
ICON_CONTAINER_CLASSNAME_SIZE_MAP[size],
|
||||
className,
|
||||
)}
|
||||
>
|
||||
<div
|
||||
className='h-full w-full shrink-0 rounded-md bg-cover bg-center'
|
||||
style={{
|
||||
backgroundImage: `url(${iconUrl})`,
|
||||
}}
|
||||
/>
|
||||
</div>
|
||||
)
|
||||
}
|
||||
|
||||
export default memo(DatasourceIcon)
|
||||
@ -0,0 +1,31 @@
|
||||
import { useCallback, useMemo, useState } from 'react'
|
||||
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 { 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'
|
||||
|
||||
export const useDatasourceIcon = (data: DataSourceNodeType) => {
|
||||
const pipelineId = useDatasetDetailContextWithSelector(s => s.dataset?.pipeline_id)
|
||||
const [dataSourceList, setDataSourceList] = useState<ToolWithProvider[]>([])
|
||||
|
||||
const handleUpdateDataSourceList = useCallback((dataSourceList: DataSourceItem[]) => {
|
||||
dataSourceList.forEach((item) => {
|
||||
const icon = item.declaration.identity.icon
|
||||
if (typeof icon == 'string' && !icon.includes(basePath))
|
||||
item.declaration.identity.icon = `${basePath}${icon}`
|
||||
})
|
||||
const formattedDataSourceList = dataSourceList.map(item => transformDataSourceToTool(item))
|
||||
setDataSourceList!(formattedDataSourceList)
|
||||
}, [])
|
||||
|
||||
useDataSourceList(!!pipelineId, handleUpdateDataSourceList)
|
||||
|
||||
const datasourceIcon = useMemo(() => {
|
||||
return dataSourceList?.find(toolWithProvider => toolWithProvider.name === data.provider_id)?.icon
|
||||
}, [data, dataSourceList])
|
||||
|
||||
return datasourceIcon
|
||||
}
|
||||
Loading…
Reference in New Issue