user-input.tsx 2.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081
  1. import {
  2. memo,
  3. useState,
  4. } from 'react'
  5. import { useTranslation } from 'react-i18next'
  6. import { useNodes } from 'reactflow'
  7. import FormItem from '../../nodes/_base/components/before-run-form/form-item'
  8. import { BlockEnum } from '../../types'
  9. import {
  10. useStore,
  11. useWorkflowStore,
  12. } from '../../store'
  13. import type { StartNodeType } from '../../nodes/start/types'
  14. import { ChevronDown } from '@/app/components/base/icons/src/vender/line/arrows'
  15. const UserInput = () => {
  16. const { t } = useTranslation()
  17. const workflowStore = useWorkflowStore()
  18. const [expanded, setExpanded] = useState(true)
  19. const inputs = useStore(s => s.inputs)
  20. const nodes = useNodes<StartNodeType>()
  21. const startNode = nodes.find(node => node.data.type === BlockEnum.Start)
  22. const variables = startNode?.data.variables || []
  23. const handleValueChange = (variable: string, v: string) => {
  24. workflowStore.getState().setInputs({
  25. ...inputs,
  26. [variable]: v,
  27. })
  28. }
  29. if (!variables.length)
  30. return null
  31. return (
  32. <div
  33. className={`
  34. relative rounded-xl border z-[1]
  35. ${!expanded ? 'bg-indigo-25 border-indigo-100 shadow-none' : 'bg-white shadow-xs border-transparent'}
  36. `}
  37. >
  38. <div
  39. className={`
  40. flex items-center px-2 pt-4 h-[18px] text-[13px] font-semibold cursor-pointer
  41. ${!expanded ? 'text-indigo-800' : 'text-gray-800'}
  42. `}
  43. onClick={() => setExpanded(!expanded)}
  44. >
  45. <ChevronDown
  46. className={`mr-1 w-3 h-3 ${!expanded ? '-rotate-90 text-indigo-600' : 'text-gray-300'}`}
  47. />
  48. {t('workflow.panel.userInputField').toLocaleUpperCase()}
  49. </div>
  50. <div className='px-2 pt-1 pb-3'>
  51. {
  52. expanded && (
  53. <div className='py-2 text-[13px] text-gray-900'>
  54. {
  55. variables.map((variable, index) => (
  56. <div
  57. key={variable.variable}
  58. className='mb-2 last-of-type:mb-0'
  59. >
  60. <FormItem
  61. autoFocus={index === 0}
  62. payload={variable}
  63. value={inputs[variable.variable]}
  64. onChange={v => handleValueChange(variable.variable, v)}
  65. />
  66. </div>
  67. ))
  68. }
  69. </div>
  70. )
  71. }
  72. </div>
  73. </div>
  74. )
  75. }
  76. export default memo(UserInput)