node-contextmenu.tsx 1.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051
  1. import {
  2. memo,
  3. useEffect,
  4. useRef,
  5. } from 'react'
  6. import { useClickAway } from 'ahooks'
  7. import { useNodes } from 'reactflow'
  8. import PanelOperatorPopup from './nodes/_base/components/panel-operator/panel-operator-popup'
  9. import type { Node } from './types'
  10. import { useStore } from './store'
  11. import { usePanelInteractions } from './hooks'
  12. const NodeContextmenu = () => {
  13. const ref = useRef(null)
  14. const nodes = useNodes()
  15. const { handleNodeContextmenuCancel, handlePaneContextmenuCancel } = usePanelInteractions()
  16. const nodeMenu = useStore(s => s.nodeMenu)
  17. const currentNode = nodes.find(node => node.id === nodeMenu?.nodeId) as Node
  18. useEffect(() => {
  19. if (nodeMenu)
  20. handlePaneContextmenuCancel()
  21. }, [nodeMenu, handlePaneContextmenuCancel])
  22. useClickAway(() => {
  23. handleNodeContextmenuCancel()
  24. }, ref)
  25. if (!nodeMenu || !currentNode)
  26. return null
  27. return (
  28. <div
  29. className='absolute z-[9]'
  30. style={{
  31. left: nodeMenu.left,
  32. top: nodeMenu.top,
  33. }}
  34. ref={ref}
  35. >
  36. <PanelOperatorPopup
  37. id={currentNode.id}
  38. data={currentNode.data}
  39. onClosePopup={() => handleNodeContextmenuCancel()}
  40. showHelpLink
  41. />
  42. </div>
  43. )
  44. }
  45. export default memo(NodeContextmenu)