user-input.tsx 1.5 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556
  1. import {
  2. memo,
  3. } from 'react'
  4. import { useNodes } from 'reactflow'
  5. import FormItem from '../../nodes/_base/components/before-run-form/form-item'
  6. import { BlockEnum } from '../../types'
  7. import {
  8. useStore,
  9. useWorkflowStore,
  10. } from '../../store'
  11. import type { StartNodeType } from '../../nodes/start/types'
  12. import cn from '@/utils/classnames'
  13. const UserInput = () => {
  14. const workflowStore = useWorkflowStore()
  15. const inputs = useStore(s => s.inputs)
  16. const nodes = useNodes<StartNodeType>()
  17. const startNode = nodes.find(node => node.data.type === BlockEnum.Start)
  18. const variables = startNode?.data.variables || []
  19. const handleValueChange = (variable: string, v: string) => {
  20. const {
  21. inputs,
  22. setInputs,
  23. } = workflowStore.getState()
  24. setInputs({
  25. ...inputs,
  26. [variable]: v,
  27. })
  28. }
  29. if (!variables.length)
  30. return null
  31. return (
  32. <div className={cn('sticky top-0 bg-components-panel-on-panel-item-bg rounded-xl border-[0.5px] border-components-panel-border-subtle shadow-xs z-[1]')}>
  33. <div className='px-4 pt-3 pb-4'>
  34. {variables.map((variable, index) => (
  35. <div
  36. key={variable.variable}
  37. className='mb-4 last-of-type:mb-0'
  38. >
  39. <FormItem
  40. autoFocus={index === 0}
  41. payload={variable}
  42. value={inputs[variable.variable]}
  43. onChange={v => handleValueChange(variable.variable, v)}
  44. />
  45. </div>
  46. ))}
  47. </div>
  48. </div>
  49. )
  50. }
  51. export default memo(UserInput)