utils.ts 1.8 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758
  1. import type { InputForm } from './type'
  2. import { InputVarType } from '@/app/components/workflow/types'
  3. import { getProcessedFiles } from '@/app/components/base/file-uploader/utils'
  4. export const processOpeningStatement = (openingStatement: string, inputs: Record<string, any>, inputsForm: InputForm[]) => {
  5. if (!openingStatement)
  6. return openingStatement
  7. return openingStatement.replace(/\{\{([^}]+)\}\}/g, (match, key) => {
  8. const name = inputs[key]
  9. if (name) { // has set value
  10. return name
  11. }
  12. const valueObj = inputsForm.find(v => v.variable === key)
  13. return valueObj ? `{{${valueObj.label}}}` : match
  14. })
  15. }
  16. export const processInputFileFromServer = (fileItem: Record<string, any>) => {
  17. return {
  18. type: fileItem.type,
  19. transfer_method: fileItem.transfer_method,
  20. url: fileItem.remote_url,
  21. upload_file_id: fileItem.related_id,
  22. }
  23. }
  24. export const getProcessedInputs = (inputs: Record<string, any>, inputsForm: InputForm[]) => {
  25. const processedInputs = { ...inputs }
  26. inputsForm.forEach((item) => {
  27. const inputValue = inputs[item.variable]
  28. // set boolean type default value
  29. if(item.type === InputVarType.checkbox) {
  30. processedInputs[item.variable] = !!inputValue
  31. return
  32. }
  33. if (!inputValue)
  34. return
  35. if (item.type === InputVarType.singleFile) {
  36. if ('transfer_method' in inputValue)
  37. processedInputs[item.variable] = processInputFileFromServer(inputValue)
  38. else
  39. processedInputs[item.variable] = getProcessedFiles([inputValue])[0]
  40. }
  41. else if (item.type === InputVarType.multiFiles) {
  42. if ('transfer_method' in inputValue[0])
  43. processedInputs[item.variable] = inputValue.map(processInputFileFromServer)
  44. else
  45. processedInputs[item.variable] = getProcessedFiles(inputValue)
  46. }
  47. })
  48. return processedInputs
  49. }