mirror of
https://github.com/langgenius/dify.git
synced 2025-07-16 05:35:09 +00:00
68 lines
2.5 KiB
TypeScript
68 lines
2.5 KiB
TypeScript
import { useMemo } from 'react'
|
|
import { BaseFieldType } from '@/app/components/base/form/form-scenarios/base/types'
|
|
import { usePublishedPipelineProcessingParams } from '@/service/use-pipeline'
|
|
import { PipelineInputVarType } from '@/models/pipeline'
|
|
import { useDatasetDetailContextWithSelector } from '@/context/dataset-detail'
|
|
|
|
const VAR_TYPE_MAP: Record<PipelineInputVarType, BaseFieldType> = {
|
|
[PipelineInputVarType.textInput]: BaseFieldType.textInput,
|
|
[PipelineInputVarType.paragraph]: BaseFieldType.paragraph,
|
|
[PipelineInputVarType.select]: BaseFieldType.select,
|
|
[PipelineInputVarType.singleFile]: BaseFieldType.file,
|
|
[PipelineInputVarType.multiFiles]: BaseFieldType.fileList,
|
|
[PipelineInputVarType.number]: BaseFieldType.numberInput,
|
|
[PipelineInputVarType.checkbox]: BaseFieldType.checkbox,
|
|
}
|
|
|
|
export const useConfigurations = (datasourceNodeId: string) => {
|
|
const pipelineId = useDatasetDetailContextWithSelector(state => state.dataset?.pipeline_id)
|
|
const { data: paramsConfig } = usePublishedPipelineProcessingParams({
|
|
pipeline_id: pipelineId!,
|
|
node_id: datasourceNodeId,
|
|
})
|
|
|
|
const initialData = useMemo(() => {
|
|
const variables = paramsConfig?.variables || []
|
|
return variables.reduce((acc, item) => {
|
|
const type = VAR_TYPE_MAP[item.type]
|
|
if ([BaseFieldType.textInput, BaseFieldType.paragraph, BaseFieldType.select].includes(type))
|
|
acc[item.variable] = item.default_value ?? ''
|
|
if (type === BaseFieldType.numberInput)
|
|
acc[item.variable] = item.default_value ?? 0
|
|
if (type === BaseFieldType.checkbox)
|
|
acc[item.variable] = true
|
|
if ([BaseFieldType.file, BaseFieldType.fileList].includes(type))
|
|
acc[item.variable] = []
|
|
return acc
|
|
}, {} as Record<string, any>)
|
|
}, [paramsConfig])
|
|
|
|
const configurations = useMemo(() => {
|
|
const variables = paramsConfig?.variables || []
|
|
const configs = variables.map(item => ({
|
|
type: VAR_TYPE_MAP[item.type],
|
|
variable: item.variable,
|
|
label: item.label,
|
|
required: item.required,
|
|
maxLength: item.max_length,
|
|
options: item.options?.map(option => ({
|
|
label: option,
|
|
value: option,
|
|
})),
|
|
showConditions: [],
|
|
placeholder: item.placeholder,
|
|
tooltip: item.tooltips,
|
|
unit: item.unit,
|
|
allowedFileTypes: item.allowed_file_types,
|
|
allowedFileExtensions: item.allowed_file_extensions,
|
|
allowedFileUploadMethods: item.allowed_file_upload_methods,
|
|
}))
|
|
return configs
|
|
}, [paramsConfig])
|
|
|
|
return {
|
|
initialData,
|
|
configurations,
|
|
}
|
|
}
|