features.tsx 1.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960
  1. import {
  2. memo,
  3. useCallback,
  4. } from 'react'
  5. import { useNodes } from 'reactflow'
  6. import { useStore } from './store'
  7. import {
  8. useIsChatMode,
  9. useNodesReadOnly,
  10. useNodesSyncDraft,
  11. } from './hooks'
  12. import { type CommonNodeType, type InputVar, InputVarType, type Node } from './types'
  13. import useConfig from './nodes/start/use-config'
  14. import type { StartNodeType } from './nodes/start/types'
  15. import type { PromptVariable } from '@/models/debug'
  16. import NewFeaturePanel from '@/app/components/base/features/new-feature-panel'
  17. const Features = () => {
  18. const setShowFeaturesPanel = useStore(s => s.setShowFeaturesPanel)
  19. const isChatMode = useIsChatMode()
  20. const { nodesReadOnly } = useNodesReadOnly()
  21. const { handleSyncWorkflowDraft } = useNodesSyncDraft()
  22. const nodes = useNodes<CommonNodeType>()
  23. const startNode = nodes.find(node => node.data.type === 'start')
  24. const { id, data } = startNode as Node<StartNodeType>
  25. const { handleAddVariable } = useConfig(id, data)
  26. const handleAddOpeningStatementVariable = (variables: PromptVariable[]) => {
  27. const newVariable = variables[0]
  28. const startNodeVariable: InputVar = {
  29. variable: newVariable.key,
  30. label: newVariable.name,
  31. type: InputVarType.textInput,
  32. max_length: newVariable.max_length,
  33. required: newVariable.required || false,
  34. options: [],
  35. }
  36. handleAddVariable(startNodeVariable)
  37. }
  38. const handleFeaturesChange = useCallback(() => {
  39. handleSyncWorkflowDraft()
  40. setShowFeaturesPanel(true)
  41. }, [handleSyncWorkflowDraft, setShowFeaturesPanel])
  42. return (
  43. <NewFeaturePanel
  44. show
  45. isChatMode={isChatMode}
  46. disabled={nodesReadOnly}
  47. onChange={handleFeaturesChange}
  48. onClose={() => setShowFeaturesPanel(false)}
  49. onAutoAddPromptVariable={handleAddOpeningStatementVariable}
  50. workflowVariables={data.variables}
  51. />
  52. )
  53. }
  54. export default memo(Features)