Nelze vybrat více než 25 témat Téma musí začínat písmenem nebo číslem, může obsahovat pomlčky („-“) a může být dlouhé až 35 znaků.

use-input-fields.ts 2.1KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354
  1. import { useMemo } from 'react'
  2. import type { BaseConfiguration } from '@/app/components/base/form/form-scenarios/base/types'
  3. import { type RAGPipelineVariables, VAR_TYPE_MAP } from '@/models/pipeline'
  4. import { BaseFieldType } from '@/app/components/base/form/form-scenarios/base/types'
  5. export const useInitialData = (variables: RAGPipelineVariables, lastRunInputData?: Record<string, any>) => {
  6. const initialData = useMemo(() => {
  7. return variables.reduce((acc, item) => {
  8. const type = VAR_TYPE_MAP[item.type]
  9. const variableName = item.variable
  10. const defaultValue = lastRunInputData?.[variableName] || item.default_value
  11. if ([BaseFieldType.textInput, BaseFieldType.paragraph, BaseFieldType.select].includes(type))
  12. acc[variableName] = defaultValue ?? ''
  13. if (type === BaseFieldType.numberInput)
  14. acc[variableName] = defaultValue ?? 0
  15. if (type === BaseFieldType.checkbox)
  16. acc[variableName] = defaultValue ?? false
  17. if ([BaseFieldType.file, BaseFieldType.fileList].includes(type))
  18. acc[variableName] = defaultValue ?? []
  19. return acc
  20. }, {} as Record<string, any>)
  21. }, [lastRunInputData, variables])
  22. return initialData
  23. }
  24. export const useConfigurations = (variables: RAGPipelineVariables) => {
  25. const configurations = useMemo(() => {
  26. const configurations: BaseConfiguration[] = []
  27. variables.forEach((item) => {
  28. configurations.push({
  29. type: VAR_TYPE_MAP[item.type],
  30. variable: item.variable,
  31. label: item.label,
  32. required: item.required,
  33. maxLength: item.max_length,
  34. options: item.options?.map(option => ({
  35. label: option,
  36. value: option,
  37. })),
  38. showConditions: [],
  39. placeholder: item.placeholder,
  40. tooltip: item.tooltips,
  41. unit: item.unit,
  42. allowedFileTypes: item.allowed_file_types,
  43. allowedFileExtensions: item.allowed_file_extensions,
  44. allowedFileUploadMethods: item.allowed_file_upload_methods,
  45. })
  46. })
  47. return configurations
  48. }, [variables])
  49. return configurations
  50. }