You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

index.tsx 4.7KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156
  1. import type { FC } from 'react'
  2. import { memo, useCallback, useEffect, useRef } from 'react'
  3. import type { VersionHistoryPanelProps } from '@/app/components/workflow/panel/version-history-panel'
  4. import { useShallow } from 'zustand/react/shallow'
  5. import { useStore as useReactflow } from 'reactflow'
  6. import { Panel as NodePanel } from '../nodes'
  7. import { useStore } from '../store'
  8. import EnvPanel from './env-panel'
  9. import cn from '@/utils/classnames'
  10. import dynamic from 'next/dynamic'
  11. const VersionHistoryPanel = dynamic(() => import('@/app/components/workflow/panel/version-history-panel'), {
  12. ssr: false,
  13. })
  14. export type PanelProps = {
  15. components?: {
  16. left?: React.ReactNode
  17. right?: React.ReactNode
  18. }
  19. versionHistoryPanelProps?: VersionHistoryPanelProps
  20. }
  21. /**
  22. * Reference MDN standard implementation:https://developer.mozilla.org/zh-CN/docs/Web/API/ResizeObserverEntry/borderBoxSize
  23. */
  24. const getEntryWidth = (entry: ResizeObserverEntry, element: HTMLElement): number => {
  25. if (entry.borderBoxSize?.length > 0)
  26. return entry.borderBoxSize[0].inlineSize
  27. if (entry.contentRect.width > 0)
  28. return entry.contentRect.width
  29. return element.getBoundingClientRect().width
  30. }
  31. const useResizeObserver = (
  32. callback: (width: number) => void,
  33. dependencies: React.DependencyList = [],
  34. ) => {
  35. const elementRef = useRef<HTMLDivElement>(null)
  36. const stableCallback = useCallback(callback, [callback])
  37. useEffect(() => {
  38. const element = elementRef.current
  39. if (!element) return
  40. const resizeObserver = new ResizeObserver((entries) => {
  41. for (const entry of entries) {
  42. const width = getEntryWidth(entry, element)
  43. stableCallback(width)
  44. }
  45. })
  46. resizeObserver.observe(element)
  47. const initialWidth = element.getBoundingClientRect().width
  48. stableCallback(initialWidth)
  49. return () => {
  50. resizeObserver.disconnect()
  51. }
  52. }, [stableCallback, ...dependencies])
  53. return elementRef
  54. }
  55. const Panel: FC<PanelProps> = ({
  56. components,
  57. versionHistoryPanelProps,
  58. }) => {
  59. const selectedNode = useReactflow(useShallow((s) => {
  60. const nodes = s.getNodes()
  61. const currentNode = nodes.find(node => node.data.selected)
  62. if (currentNode) {
  63. return {
  64. id: currentNode.id,
  65. type: currentNode.type,
  66. data: currentNode.data,
  67. }
  68. }
  69. }))
  70. const showEnvPanel = useStore(s => s.showEnvPanel)
  71. const isRestoring = useStore(s => s.isRestoring)
  72. const showWorkflowVersionHistoryPanel = useStore(s => s.showWorkflowVersionHistoryPanel)
  73. // widths used for adaptive layout
  74. const workflowCanvasWidth = useStore(s => s.workflowCanvasWidth)
  75. const previewPanelWidth = useStore(s => s.previewPanelWidth)
  76. const setPreviewPanelWidth = useStore(s => s.setPreviewPanelWidth)
  77. // When a node is selected and the NodePanel appears, if the current width
  78. // of preview/otherPanel is too large, it may result in the total width of
  79. // the two panels exceeding the workflowCanvasWidth, causing the NodePanel
  80. // to be pushed out. Here we check and, if necessary, reduce the previewPanelWidth
  81. // to "workflowCanvasWidth - 400 (minimum NodePanel width) - 400 (minimum canvas space)",
  82. // while still ensuring that previewPanelWidth ≥ 400.
  83. useEffect(() => {
  84. if (!selectedNode || !workflowCanvasWidth)
  85. return
  86. const reservedCanvasWidth = 400 // Reserve the minimum visible width for the canvas
  87. const minNodePanelWidth = 400
  88. const maxAllowed = Math.max(workflowCanvasWidth - reservedCanvasWidth - minNodePanelWidth, 400)
  89. if (previewPanelWidth > maxAllowed)
  90. setPreviewPanelWidth(maxAllowed)
  91. }, [selectedNode, workflowCanvasWidth, previewPanelWidth, setPreviewPanelWidth])
  92. const setRightPanelWidth = useStore(s => s.setRightPanelWidth)
  93. const setOtherPanelWidth = useStore(s => s.setOtherPanelWidth)
  94. const rightPanelRef = useResizeObserver(
  95. setRightPanelWidth,
  96. [setRightPanelWidth, selectedNode, showEnvPanel, showWorkflowVersionHistoryPanel],
  97. )
  98. const otherPanelRef = useResizeObserver(
  99. setOtherPanelWidth,
  100. [setOtherPanelWidth, showEnvPanel, showWorkflowVersionHistoryPanel],
  101. )
  102. return (
  103. <div
  104. ref={rightPanelRef}
  105. tabIndex={-1}
  106. className={cn('absolute bottom-1 right-0 top-14 z-10 flex outline-none')}
  107. key={`${isRestoring}`}
  108. >
  109. {components?.left}
  110. {!!selectedNode && <NodePanel {...selectedNode} />}
  111. <div
  112. className="relative"
  113. ref={otherPanelRef}
  114. >
  115. {
  116. components?.right
  117. }
  118. {
  119. showWorkflowVersionHistoryPanel && (
  120. <VersionHistoryPanel {...versionHistoryPanelProps} />
  121. )
  122. }
  123. {
  124. showEnvPanel && (
  125. <EnvPanel />
  126. )
  127. }
  128. </div>
  129. </div>
  130. )
  131. }
  132. export default memo(Panel)