207 lines
6.5 KiB
TypeScript
Raw Normal View History

2025-04-22 16:46:33 +08:00
import type { FC } from 'react'
2025-06-04 11:39:04 +08:00
import {
useCallback,
useMemo,
useState,
} from 'react'
2025-05-23 10:46:28 +08:00
import { useTranslation } from 'react-i18next'
2025-04-22 16:46:33 +08:00
import { memo } from 'react'
2025-06-04 11:39:04 +08:00
import { useBoolean } from 'ahooks'
2025-04-22 16:46:33 +08:00
import type { DataSourceNodeType } from './types'
2025-06-05 16:51:01 +08:00
import { DataSourceClassification } from './types'
2025-04-22 16:46:33 +08:00
import type { NodePanelProps } from '@/app/components/workflow/types'
2025-06-04 11:39:04 +08:00
import {
BoxGroupField,
Group,
} from '@/app/components/workflow/nodes/_base/components/layout'
2025-05-23 10:46:28 +08:00
import OutputVars, { VarItem } from '@/app/components/workflow/nodes/_base/components/output-vars'
2025-05-23 11:50:36 +08:00
import TagInput from '@/app/components/base/tag-input'
2025-05-27 17:42:02 +08:00
import { useNodesReadOnly } from '@/app/components/workflow/hooks'
2025-05-23 11:50:36 +08:00
import { useConfig } from './hooks/use-config'
2025-06-05 16:51:01 +08:00
import {
COMMON_OUTPUT,
FILE_OUTPUT,
WEBSITE_OUTPUT,
} from './constants'
2025-06-04 11:39:04 +08:00
import { useStore } from '@/app/components/workflow/store'
import Button from '@/app/components/base/button'
import ConfigCredential from './components/config-credential'
import InputVarList from '@/app/components/workflow/nodes/tool/components/input-var-list'
import { toolParametersToFormSchemas } from '@/app/components/tools/utils/to-form-schema'
import type { Var } from '@/app/components/workflow/types'
import { VarType } from '@/app/components/workflow/types'
import { useToastContext } from '@/app/components/base/toast'
import { useUpdateDataSourceCredentials } from '@/service/use-pipeline'
2025-05-23 10:46:28 +08:00
2025-05-23 11:50:36 +08:00
const Panel: FC<NodePanelProps<DataSourceNodeType>> = ({ id, data }) => {
2025-05-23 10:46:28 +08:00
const { t } = useTranslation()
2025-06-04 11:39:04 +08:00
const { notify } = useToastContext()
2025-05-27 17:42:02 +08:00
const { nodesReadOnly } = useNodesReadOnly()
2025-06-04 11:39:04 +08:00
const dataSourceList = useStore(s => s.dataSourceList)
2025-05-23 11:50:36 +08:00
const {
provider_type,
2025-06-05 16:51:01 +08:00
plugin_id,
2025-05-23 11:50:36 +08:00
fileExtensions = [],
2025-06-04 11:39:04 +08:00
datasource_parameters,
2025-05-23 11:50:36 +08:00
} = data
2025-05-27 17:42:02 +08:00
const {
handleFileExtensionsChange,
2025-06-04 11:39:04 +08:00
handleParametersChange,
2025-05-27 17:42:02 +08:00
} = useConfig(id)
2025-06-05 16:51:01 +08:00
const isLocalFile = provider_type === DataSourceClassification.file
const isWebsiteCrawl = provider_type === DataSourceClassification.website
const currentDataSource = dataSourceList?.find(ds => ds.plugin_id === plugin_id)
2025-06-04 11:39:04 +08:00
const isAuthorized = !!currentDataSource?.is_authorized
const [showAuthModal, {
setTrue: openAuthModal,
setFalse: hideAuthModal,
}] = useBoolean(false)
const currentDataSourceItem: any = currentDataSource?.tools.find(tool => tool.name === data.datasource_name)
const formSchemas = useMemo(() => {
return currentDataSourceItem ? toolParametersToFormSchemas(currentDataSourceItem.parameters) : []
}, [currentDataSourceItem])
const [currVarIndex, setCurrVarIndex] = useState(-1)
const currVarType = formSchemas[currVarIndex]?._type
const handleOnVarOpen = useCallback((index: number) => {
setCurrVarIndex(index)
}, [])
const filterVar = useCallback((varPayload: Var) => {
if (currVarType)
return varPayload.type === currVarType
return varPayload.type !== VarType.arrayFile
}, [currVarType])
const { mutateAsync } = useUpdateDataSourceCredentials()
const handleAuth = useCallback(async (value: any) => {
await mutateAsync({
2025-06-04 18:09:03 +08:00
provider: currentDataSource?.provider || '',
pluginId: currentDataSource?.plugin_id || '',
2025-06-04 11:39:04 +08:00
credentials: value,
})
notify({
type: 'success',
message: t('common.api.actionSuccess'),
})
hideAuthModal()
2025-06-04 18:09:03 +08:00
}, [currentDataSource, mutateAsync, notify, t, hideAuthModal])
2025-04-22 16:46:33 +08:00
return (
2025-05-23 10:46:28 +08:00
<div >
2025-06-04 11:39:04 +08:00
{
2025-06-05 15:07:01 +08:00
!isAuthorized && !showAuthModal && !isLocalFile && currentDataSource && (
2025-06-04 11:39:04 +08:00
<Group>
<Button
variant='primary'
className='w-full'
onClick={openAuthModal}
disabled={nodesReadOnly}
>
{t('workflow.nodes.tool.authorize')}
</Button>
</Group>
)
}
{
2025-06-04 15:48:29 +08:00
isAuthorized && !isLocalFile && (
2025-06-05 15:07:01 +08:00
<BoxGroupField
boxGroupProps={{
boxProps: { withBorderBottom: true },
}}
fieldProps={{
fieldTitleProps: {
title: t('workflow.nodes.tool.inputVars'),
},
supportCollapse: true,
2025-06-04 11:39:04 +08:00
}}
>
<InputVarList
readOnly={nodesReadOnly}
nodeId={id}
schema={formSchemas as any}
filterVar={filterVar}
value={datasource_parameters}
onChange={handleParametersChange}
isSupportConstantValue
onOpen={handleOnVarOpen}
/>
2025-06-05 15:07:01 +08:00
</BoxGroupField>
2025-06-04 11:39:04 +08:00
)
}
2025-05-23 11:50:36 +08:00
{
2025-05-23 16:27:19 +08:00
isLocalFile && (
<BoxGroupField
boxGroupProps={{
boxProps: { withBorderBottom: true },
}}
fieldProps={{
fieldTitleProps: {
2025-05-23 14:25:38 +08:00
title: t('workflow.nodes.dataSource.supportedFileFormats'),
2025-05-23 16:27:19 +08:00
},
}}
>
<div className='rounded-lg bg-components-input-bg-normal p-1 pt-0'>
<TagInput
items={fileExtensions}
onChange={handleFileExtensionsChange}
placeholder={t('workflow.nodes.dataSource.supportedFileFormatsPlaceholder')}
inputClassName='bg-transparent'
2025-05-29 11:03:22 +08:00
disableAdd={nodesReadOnly}
disableRemove={nodesReadOnly}
2025-05-23 16:27:19 +08:00
/>
</div>
</BoxGroupField>
2025-05-23 11:50:36 +08:00
)
}
2025-05-23 10:46:28 +08:00
<OutputVars>
2025-05-23 16:27:19 +08:00
{
2025-06-05 16:51:01 +08:00
COMMON_OUTPUT.map(item => (
<VarItem
name={item.name}
type={item.type}
description={item.description}
/>
))
}
{
isLocalFile && FILE_OUTPUT.map(item => (
2025-05-23 16:27:19 +08:00
<VarItem
2025-06-05 16:51:01 +08:00
name={item.name}
type={item.type}
description={item.description}
subItems={item.subItems.map(item => ({
2025-05-26 15:57:34 +08:00
name: item.name,
type: item.type,
description: item.description,
}))}
2025-05-23 16:27:19 +08:00
/>
2025-06-05 16:51:01 +08:00
))
}
{
isWebsiteCrawl && WEBSITE_OUTPUT.map(item => (
<VarItem
name={item.name}
type={item.type}
description={item.description}
/>
))
2025-05-23 16:27:19 +08:00
}
2025-05-23 10:46:28 +08:00
</OutputVars>
2025-06-04 11:39:04 +08:00
{
2025-06-04 15:48:29 +08:00
showAuthModal && !isLocalFile && (
2025-06-04 11:39:04 +08:00
<ConfigCredential
dataSourceItem={currentDataSource!}
onCancel={hideAuthModal}
onSaved={handleAuth}
isHideRemoveBtn
/>
)
}
2025-04-22 16:46:33 +08:00
</div>
)
}
export default memo(Panel)