mirror of
https://github.com/langgenius/dify.git
synced 2026-04-28 11:56:55 +08:00
refactor: simplify type definitions in form components and update related configurations
This commit is contained in:
parent
0099f2296d
commit
6024dbe98d
@ -3,15 +3,15 @@ import { type BaseConfiguration, BaseFieldType } from './types'
|
|||||||
import { withForm } from '../..'
|
import { withForm } from '../..'
|
||||||
import { useStore } from '@tanstack/react-form'
|
import { useStore } from '@tanstack/react-form'
|
||||||
|
|
||||||
type BaseFieldProps<T> = {
|
type BaseFieldProps = {
|
||||||
initialData?: T
|
initialData?: Record<string, any>
|
||||||
config: BaseConfiguration<T>
|
config: BaseConfiguration
|
||||||
}
|
}
|
||||||
|
|
||||||
const BaseField = <T,>({
|
const BaseField = ({
|
||||||
initialData,
|
initialData,
|
||||||
config,
|
config,
|
||||||
}: BaseFieldProps<T>) => withForm({
|
}: BaseFieldProps) => withForm({
|
||||||
defaultValues: initialData,
|
defaultValues: initialData,
|
||||||
render: function Render({
|
render: function Render({
|
||||||
form,
|
form,
|
||||||
|
|||||||
@ -4,14 +4,14 @@ import BaseField from './field'
|
|||||||
import type { BaseFormProps } from './types'
|
import type { BaseFormProps } from './types'
|
||||||
import { generateZodSchema } from './utils'
|
import { generateZodSchema } from './utils'
|
||||||
|
|
||||||
const BaseForm = <T,>({
|
const BaseForm = ({
|
||||||
initialData,
|
initialData,
|
||||||
configurations,
|
configurations,
|
||||||
onSubmit,
|
onSubmit,
|
||||||
CustomActions,
|
CustomActions,
|
||||||
}: BaseFormProps<T>) => {
|
}: BaseFormProps) => {
|
||||||
const schema = useMemo(() => {
|
const schema = useMemo(() => {
|
||||||
const schema = generateZodSchema<T>(configurations)
|
const schema = generateZodSchema(configurations)
|
||||||
return schema
|
return schema
|
||||||
}, [configurations])
|
}, [configurations])
|
||||||
|
|
||||||
@ -44,7 +44,7 @@ const BaseForm = <T,>({
|
|||||||
>
|
>
|
||||||
<div className='flex flex-col gap-4 px-4 py-2'>
|
<div className='flex flex-col gap-4 px-4 py-2'>
|
||||||
{configurations.map((config, index) => {
|
{configurations.map((config, index) => {
|
||||||
const FieldComponent = BaseField<T>({
|
const FieldComponent = BaseField({
|
||||||
initialData,
|
initialData,
|
||||||
config,
|
config,
|
||||||
})
|
})
|
||||||
|
|||||||
@ -1,4 +1,3 @@
|
|||||||
import type { DeepKeys } from '@tanstack/react-form'
|
|
||||||
import type { FormType } from '../..'
|
import type { FormType } from '../..'
|
||||||
import type { Option } from '../../../select/pure'
|
import type { Option } from '../../../select/pure'
|
||||||
|
|
||||||
@ -9,8 +8,8 @@ export enum BaseFieldType {
|
|||||||
select = 'select',
|
select = 'select',
|
||||||
}
|
}
|
||||||
|
|
||||||
export type ShowCondition<T> = {
|
export type ShowCondition = {
|
||||||
variable: DeepKeys<T>
|
variable: string
|
||||||
value: any
|
value: any
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -29,21 +28,21 @@ export type SelectConfiguration = {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
export type BaseConfiguration<T> = {
|
export type BaseConfiguration = {
|
||||||
label: string
|
label: string
|
||||||
variable: DeepKeys<T> // Variable name
|
variable: string // Variable name
|
||||||
maxLength?: number // Max length for text input
|
maxLength?: number // Max length for text input
|
||||||
placeholder?: string
|
placeholder?: string
|
||||||
required: boolean
|
required: boolean
|
||||||
showOptional?: boolean // show optional label
|
showOptional?: boolean // show optional label
|
||||||
showConditions: ShowCondition<T>[] // Show this field only when all conditions are met
|
showConditions: ShowCondition[] // Show this field only when all conditions are met
|
||||||
type: BaseFieldType
|
type: BaseFieldType
|
||||||
tooltip?: string // Tooltip for this field
|
tooltip?: string // Tooltip for this field
|
||||||
} & NumberConfiguration & Partial<SelectConfiguration>
|
} & NumberConfiguration & Partial<SelectConfiguration>
|
||||||
|
|
||||||
export type BaseFormProps<T> = {
|
export type BaseFormProps = {
|
||||||
initialData?: T
|
initialData?: Record<string, any>
|
||||||
configurations: BaseConfiguration<T>[]
|
configurations: BaseConfiguration[]
|
||||||
CustomActions?: (form: FormType) => React.ReactNode
|
CustomActions?: (form: FormType) => React.ReactNode
|
||||||
onSubmit: (value: T) => void
|
onSubmit: (value: Record<string, any>) => void
|
||||||
}
|
}
|
||||||
|
|||||||
@ -2,7 +2,7 @@ import type { ZodNumber, ZodSchema, ZodString } from 'zod'
|
|||||||
import { z } from 'zod'
|
import { z } from 'zod'
|
||||||
import { type BaseConfiguration, BaseFieldType } from './types'
|
import { type BaseConfiguration, BaseFieldType } from './types'
|
||||||
|
|
||||||
export const generateZodSchema = <T>(fields: BaseConfiguration<T>[]) => {
|
export const generateZodSchema = (fields: BaseConfiguration[]) => {
|
||||||
const shape: Record<string, ZodSchema> = {}
|
const shape: Record<string, ZodSchema> = {}
|
||||||
|
|
||||||
fields.forEach((field) => {
|
fields.forEach((field) => {
|
||||||
|
|||||||
@ -3,15 +3,15 @@ import { type InputFieldConfiguration, InputFieldType } from './types'
|
|||||||
import { withForm } from '../..'
|
import { withForm } from '../..'
|
||||||
import { useStore } from '@tanstack/react-form'
|
import { useStore } from '@tanstack/react-form'
|
||||||
|
|
||||||
type InputFieldProps<T> = {
|
type InputFieldProps = {
|
||||||
initialData?: T
|
initialData?: Record<string, any>
|
||||||
config: InputFieldConfiguration<T>
|
config: InputFieldConfiguration
|
||||||
}
|
}
|
||||||
|
|
||||||
const InputField = <T,>({
|
const InputField = ({
|
||||||
initialData,
|
initialData,
|
||||||
config,
|
config,
|
||||||
}: InputFieldProps<T>) => withForm({
|
}: InputFieldProps) => withForm({
|
||||||
defaultValues: initialData,
|
defaultValues: initialData,
|
||||||
render: function Render({
|
render: function Render({
|
||||||
form,
|
form,
|
||||||
|
|||||||
@ -23,17 +23,17 @@ export type NumberSliderConfiguration = {
|
|||||||
min?: number
|
min?: number
|
||||||
}
|
}
|
||||||
|
|
||||||
export type InputFieldConfiguration<T> = {
|
export type InputFieldConfiguration = {
|
||||||
label: string
|
label: string
|
||||||
variable: DeepKeys<T> // Variable name
|
variable: string // Variable name
|
||||||
maxLength?: number // Max length for text input
|
maxLength?: number // Max length for text input
|
||||||
placeholder?: string
|
placeholder?: string
|
||||||
required: boolean
|
required: boolean
|
||||||
showOptional?: boolean // show optional label
|
showOptional?: boolean // show optional label
|
||||||
showConditions: ShowCondition<T>[] // Show this field only when all conditions are met
|
showConditions: ShowCondition[] // Show this field only when all conditions are met
|
||||||
type: InputFieldType
|
type: InputFieldType
|
||||||
tooltip?: string // Tooltip for this field
|
tooltip?: string // Tooltip for this field
|
||||||
listeners?: FieldListeners<T, DeepKeys<T>> // Listener for this field
|
listeners?: FieldListeners<Record<string, any>, DeepKeys<Record<string, any>>> // Listener for this field
|
||||||
} & NumberConfiguration & Partial<InputTypeSelectConfiguration>
|
} & NumberConfiguration & Partial<InputTypeSelectConfiguration>
|
||||||
& Partial<NumberSliderConfiguration>
|
& Partial<NumberSliderConfiguration>
|
||||||
& Partial<SelectConfiguration>
|
& Partial<SelectConfiguration>
|
||||||
|
|||||||
@ -3,7 +3,7 @@ import { z } from 'zod'
|
|||||||
import { type InputFieldConfiguration, InputFieldType } from './types'
|
import { type InputFieldConfiguration, InputFieldType } from './types'
|
||||||
import { SupportedFileTypes, TransferMethod } from '@/app/components/rag-pipeline/components/input-field/editor/form/schema'
|
import { SupportedFileTypes, TransferMethod } from '@/app/components/rag-pipeline/components/input-field/editor/form/schema'
|
||||||
|
|
||||||
export const generateZodSchema = <T>(fields: InputFieldConfiguration<T>[]) => {
|
export const generateZodSchema = (fields: InputFieldConfiguration[]) => {
|
||||||
const shape: Record<string, ZodSchema> = {}
|
const shape: Record<string, ZodSchema> = {}
|
||||||
|
|
||||||
fields.forEach((field) => {
|
fields.forEach((field) => {
|
||||||
|
|||||||
@ -3,15 +3,15 @@ import { type InputFieldConfiguration, InputFieldType } from './types'
|
|||||||
import { withForm } from '../..'
|
import { withForm } from '../..'
|
||||||
import { useStore } from '@tanstack/react-form'
|
import { useStore } from '@tanstack/react-form'
|
||||||
|
|
||||||
type InputFieldProps<T> = {
|
type InputFieldProps = {
|
||||||
initialData?: T
|
initialData?: Record<string, any>
|
||||||
config: InputFieldConfiguration<T>
|
config: InputFieldConfiguration
|
||||||
}
|
}
|
||||||
|
|
||||||
const NodePanelField = <T,>({
|
const NodePanelField = ({
|
||||||
initialData,
|
initialData,
|
||||||
config,
|
config,
|
||||||
}: InputFieldProps<T>) => withForm({
|
}: InputFieldProps) => withForm({
|
||||||
defaultValues: initialData,
|
defaultValues: initialData,
|
||||||
render: function Render({
|
render: function Render({
|
||||||
form,
|
form,
|
||||||
|
|||||||
@ -24,17 +24,17 @@ export type NumberSliderConfiguration = {
|
|||||||
min?: number
|
min?: number
|
||||||
}
|
}
|
||||||
|
|
||||||
export type InputFieldConfiguration<T> = {
|
export type InputFieldConfiguration = {
|
||||||
label: string
|
label: string
|
||||||
variable: DeepKeys<T> // Variable name
|
variable: string // Variable name
|
||||||
maxLength?: number // Max length for text input
|
maxLength?: number // Max length for text input
|
||||||
placeholder?: string
|
placeholder?: string
|
||||||
required: boolean
|
required: boolean
|
||||||
showOptional?: boolean // show optional label
|
showOptional?: boolean // show optional label
|
||||||
showConditions: ShowCondition<T>[] // Show this field only when all conditions are met
|
showConditions: ShowCondition[] // Show this field only when all conditions are met
|
||||||
type: InputFieldType
|
type: InputFieldType
|
||||||
tooltip?: string // Tooltip for this field
|
tooltip?: string // Tooltip for this field
|
||||||
listeners?: FieldListeners<T, DeepKeys<T>> // Listener for this field
|
listeners?: FieldListeners<Record<string, any>, DeepKeys<Record<string, any>>> // Listener for this field
|
||||||
} & NumberConfiguration & Partial<InputTypeSelectConfiguration>
|
} & NumberConfiguration & Partial<InputTypeSelectConfiguration>
|
||||||
& Partial<NumberSliderConfiguration>
|
& Partial<NumberSliderConfiguration>
|
||||||
& Partial<SelectConfiguration>
|
& Partial<SelectConfiguration>
|
||||||
|
|||||||
@ -10,6 +10,7 @@ import InputTypeSelectField from './components/field/input-type-select'
|
|||||||
import FileTypesField from './components/field/file-types'
|
import FileTypesField from './components/field/file-types'
|
||||||
import UploadMethodField from './components/field/upload-method'
|
import UploadMethodField from './components/field/upload-method'
|
||||||
import NumberSliderField from './components/field/number-slider'
|
import NumberSliderField from './components/field/number-slider'
|
||||||
|
import VariableOrConstantInputField from './components/field/variable-selector'
|
||||||
|
|
||||||
export const { fieldContext, useFieldContext, formContext, useFormContext }
|
export const { fieldContext, useFieldContext, formContext, useFormContext }
|
||||||
= createFormHookContexts()
|
= createFormHookContexts()
|
||||||
@ -26,6 +27,7 @@ export const { useAppForm, withForm } = createFormHook({
|
|||||||
FileTypesField,
|
FileTypesField,
|
||||||
UploadMethodField,
|
UploadMethodField,
|
||||||
NumberSliderField,
|
NumberSliderField,
|
||||||
|
VariableOrConstantInputField,
|
||||||
},
|
},
|
||||||
formComponents: {
|
formComponents: {
|
||||||
Actions,
|
Actions,
|
||||||
|
|||||||
@ -3,7 +3,6 @@ import { useAppForm } from '@/app/components/base/form'
|
|||||||
import BaseField from '@/app/components/base/form/form-scenarios/base/field'
|
import BaseField from '@/app/components/base/form/form-scenarios/base/field'
|
||||||
import type { BaseConfiguration } from '@/app/components/base/form/form-scenarios/base/types'
|
import type { BaseConfiguration } from '@/app/components/base/form/form-scenarios/base/types'
|
||||||
import { ArrowDownRoundFill } from '@/app/components/base/icons/src/vender/solid/general'
|
import { ArrowDownRoundFill } from '@/app/components/base/icons/src/vender/solid/general'
|
||||||
import type { CrawlOptions } from '@/models/datasets'
|
|
||||||
import cn from '@/utils/classnames'
|
import cn from '@/utils/classnames'
|
||||||
import { RiPlayLargeLine } from '@remixicon/react'
|
import { RiPlayLargeLine } from '@remixicon/react'
|
||||||
import { useBoolean } from 'ahooks'
|
import { useBoolean } from 'ahooks'
|
||||||
@ -14,17 +13,13 @@ import type { ZodSchema } from 'zod'
|
|||||||
|
|
||||||
const I18N_PREFIX = 'datasetCreation.stepOne.website'
|
const I18N_PREFIX = 'datasetCreation.stepOne.website'
|
||||||
|
|
||||||
export type FormData = {
|
|
||||||
url: string
|
|
||||||
} & CrawlOptions
|
|
||||||
|
|
||||||
type OptionsProps = {
|
type OptionsProps = {
|
||||||
initialData: FormData
|
initialData: Record<string, any>
|
||||||
configurations: BaseConfiguration<FormData>[]
|
configurations: BaseConfiguration[]
|
||||||
isRunning: boolean
|
isRunning: boolean
|
||||||
controlFoldOptions?: number
|
controlFoldOptions?: number
|
||||||
schema: ZodSchema
|
schema: ZodSchema
|
||||||
onSubmit: (data: FormData) => void
|
onSubmit: (data: Record<string, any>) => void
|
||||||
}
|
}
|
||||||
|
|
||||||
const Options = ({
|
const Options = ({
|
||||||
@ -104,7 +99,7 @@ const Options = ({
|
|||||||
{!fold && (
|
{!fold && (
|
||||||
<div className='flex flex-col gap-3 border-t border-divider-subtle px-4 py-3'>
|
<div className='flex flex-col gap-3 border-t border-divider-subtle px-4 py-3'>
|
||||||
{configurations.map((config, index) => {
|
{configurations.map((config, index) => {
|
||||||
const FieldComponent = BaseField<FormData>({
|
const FieldComponent = BaseField({
|
||||||
initialData,
|
initialData,
|
||||||
config,
|
config,
|
||||||
})
|
})
|
||||||
|
|||||||
@ -1,7 +1,6 @@
|
|||||||
import type { BaseConfiguration } from '@/app/components/base/form/form-scenarios/base/types'
|
import type { BaseConfiguration } from '@/app/components/base/form/form-scenarios/base/types'
|
||||||
import { BaseFieldType } from '@/app/components/base/form/form-scenarios/base/types'
|
import { BaseFieldType } from '@/app/components/base/form/form-scenarios/base/types'
|
||||||
import { useTranslation } from 'react-i18next'
|
import { useTranslation } from 'react-i18next'
|
||||||
import type { FormData } from '../base/options'
|
|
||||||
import { z } from 'zod'
|
import { z } from 'zod'
|
||||||
|
|
||||||
const ERROR_I18N_PREFIX = 'common.errorMsg'
|
const ERROR_I18N_PREFIX = 'common.errorMsg'
|
||||||
@ -9,7 +8,7 @@ const I18N_PREFIX = 'datasetCreation.stepOne.website'
|
|||||||
|
|
||||||
export const useConfigurations = () => {
|
export const useConfigurations = () => {
|
||||||
const { t } = useTranslation()
|
const { t } = useTranslation()
|
||||||
const configurations: BaseConfiguration<FormData>[] = [
|
const configurations: BaseConfiguration[] = [
|
||||||
{
|
{
|
||||||
type: BaseFieldType.textInput,
|
type: BaseFieldType.textInput,
|
||||||
variable: 'url',
|
variable: 'url',
|
||||||
|
|||||||
@ -1,7 +1,6 @@
|
|||||||
import type { BaseConfiguration } from '@/app/components/base/form/form-scenarios/base/types'
|
import type { BaseConfiguration } from '@/app/components/base/form/form-scenarios/base/types'
|
||||||
import { BaseFieldType } from '@/app/components/base/form/form-scenarios/base/types'
|
import { BaseFieldType } from '@/app/components/base/form/form-scenarios/base/types'
|
||||||
import { useTranslation } from 'react-i18next'
|
import { useTranslation } from 'react-i18next'
|
||||||
import type { FormData } from '../base/options'
|
|
||||||
import { z } from 'zod'
|
import { z } from 'zod'
|
||||||
|
|
||||||
const ERROR_I18N_PREFIX = 'common.errorMsg'
|
const ERROR_I18N_PREFIX = 'common.errorMsg'
|
||||||
@ -9,7 +8,7 @@ const I18N_PREFIX = 'datasetCreation.stepOne.website'
|
|||||||
|
|
||||||
export const useConfigurations = () => {
|
export const useConfigurations = () => {
|
||||||
const { t } = useTranslation()
|
const { t } = useTranslation()
|
||||||
const configurations: BaseConfiguration<FormData>[] = [
|
const configurations: BaseConfiguration[] = [
|
||||||
{
|
{
|
||||||
type: BaseFieldType.textInput,
|
type: BaseFieldType.textInput,
|
||||||
variable: 'url',
|
variable: 'url',
|
||||||
|
|||||||
@ -1,7 +1,6 @@
|
|||||||
import type { BaseConfiguration } from '@/app/components/base/form/form-scenarios/base/types'
|
import type { BaseConfiguration } from '@/app/components/base/form/form-scenarios/base/types'
|
||||||
import { BaseFieldType } from '@/app/components/base/form/form-scenarios/base/types'
|
import { BaseFieldType } from '@/app/components/base/form/form-scenarios/base/types'
|
||||||
import { useTranslation } from 'react-i18next'
|
import { useTranslation } from 'react-i18next'
|
||||||
import type { FormData } from '../base/options'
|
|
||||||
import { z } from 'zod'
|
import { z } from 'zod'
|
||||||
|
|
||||||
const ERROR_I18N_PREFIX = 'common.errorMsg'
|
const ERROR_I18N_PREFIX = 'common.errorMsg'
|
||||||
@ -9,7 +8,7 @@ const I18N_PREFIX = 'datasetCreation.stepOne.website'
|
|||||||
|
|
||||||
export const useConfigurations = () => {
|
export const useConfigurations = () => {
|
||||||
const { t } = useTranslation()
|
const { t } = useTranslation()
|
||||||
const configurations: BaseConfiguration<FormData>[] = [
|
const configurations: BaseConfiguration[] = [
|
||||||
{
|
{
|
||||||
type: BaseFieldType.textInput,
|
type: BaseFieldType.textInput,
|
||||||
variable: 'url',
|
variable: 'url',
|
||||||
|
|||||||
@ -1,3 +1,4 @@
|
|||||||
|
import type { InputVarType } from '@/app/components/workflow/types'
|
||||||
import type { DSLImportMode, DSLImportStatus } from './app'
|
import type { DSLImportMode, DSLImportStatus } from './app'
|
||||||
import type { ChunkingMode, IconInfo } from './datasets'
|
import type { ChunkingMode, IconInfo } from './datasets'
|
||||||
import type { Dependency } from '@/app/components/plugins/types'
|
import type { Dependency } from '@/app/components/plugins/types'
|
||||||
@ -69,3 +70,17 @@ export type ImportPipelineDSLResponse = {
|
|||||||
error: string
|
error: string
|
||||||
leaked_dependencies: Dependency[]
|
leaked_dependencies: Dependency[]
|
||||||
}
|
}
|
||||||
|
|
||||||
|
export type Variables = {
|
||||||
|
type: InputVarType
|
||||||
|
label: string
|
||||||
|
description: string
|
||||||
|
variable: string
|
||||||
|
max_length: number
|
||||||
|
required: boolean
|
||||||
|
options?: string[]
|
||||||
|
}
|
||||||
|
|
||||||
|
export type PipelineProcessingParamsResponse = {
|
||||||
|
variables: Variables[]
|
||||||
|
}
|
||||||
|
|||||||
@ -6,6 +6,7 @@ import type {
|
|||||||
ExportPipelineDSLResponse,
|
ExportPipelineDSLResponse,
|
||||||
ImportPipelineDSLRequest,
|
ImportPipelineDSLRequest,
|
||||||
ImportPipelineDSLResponse,
|
ImportPipelineDSLResponse,
|
||||||
|
PipelineProcessingParamsResponse,
|
||||||
PipelineTemplateByIdResponse,
|
PipelineTemplateByIdResponse,
|
||||||
PipelineTemplateListParams,
|
PipelineTemplateListParams,
|
||||||
PipelineTemplateListResponse,
|
PipelineTemplateListResponse,
|
||||||
@ -85,3 +86,12 @@ export const useImportPipelineDSL = (
|
|||||||
...mutationOptions,
|
...mutationOptions,
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
|
|
||||||
|
export const usePipelineProcessingParams = (pipelineId: string) => {
|
||||||
|
return useQuery<PipelineProcessingParamsResponse>({
|
||||||
|
queryKey: [NAME_SPACE, 'pipeline-processing-params', pipelineId],
|
||||||
|
queryFn: () => {
|
||||||
|
return get<PipelineProcessingParamsResponse>(`/rag/pipeline/${pipelineId}/workflows/processing/parameters`)
|
||||||
|
},
|
||||||
|
})
|
||||||
|
}
|
||||||
|
|||||||
Loading…
Reference in New Issue
Block a user