use-single-run-form-params.ts 1.7 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364
  1. import type { MutableRefObject } from 'react'
  2. import type { InputVar, Variable } from '@/app/components/workflow/types'
  3. import { useCallback, useMemo } from 'react'
  4. import type { DocExtractorNodeType } from './types'
  5. import { useTranslation } from 'react-i18next'
  6. import { InputVarType } from '@/app/components/workflow/types'
  7. const i18nPrefix = 'workflow.nodes.docExtractor'
  8. type Params = {
  9. id: string,
  10. payload: DocExtractorNodeType,
  11. runInputData: Record<string, any>
  12. runInputDataRef: MutableRefObject<Record<string, any>>
  13. getInputVars: (textList: string[]) => InputVar[]
  14. setRunInputData: (data: Record<string, any>) => void
  15. toVarInputs: (variables: Variable[]) => InputVar[]
  16. }
  17. const useSingleRunFormParams = ({
  18. payload,
  19. runInputData,
  20. setRunInputData,
  21. }: Params) => {
  22. const { t } = useTranslation()
  23. const files = runInputData.files
  24. const setFiles = useCallback((newFiles: []) => {
  25. setRunInputData({
  26. ...runInputData,
  27. files: newFiles,
  28. })
  29. }, [runInputData, setRunInputData])
  30. const forms = useMemo(() => {
  31. return [
  32. {
  33. inputs: [{
  34. label: t(`${i18nPrefix}.inputVar`)!,
  35. variable: 'files',
  36. type: InputVarType.multiFiles,
  37. required: true,
  38. }],
  39. values: { files },
  40. onChange: (keyValue: Record<string, any>) => setFiles(keyValue.files),
  41. },
  42. ]
  43. }, [files, setFiles, t])
  44. const getDependentVars = () => {
  45. return [payload.variable_selector]
  46. }
  47. const getDependentVar = (variable: string) => {
  48. if(variable === 'files')
  49. return payload.variable_selector
  50. }
  51. return {
  52. forms,
  53. getDependentVars,
  54. getDependentVar,
  55. }
  56. }
  57. export default useSingleRunFormParams