diff --git a/web/app/components/workflow/panel/index.tsx b/web/app/components/workflow/panel/index.tsx index 775191ecee..75fd5b1509 100644 --- a/web/app/components/workflow/panel/index.tsx +++ b/web/app/components/workflow/panel/index.tsx @@ -13,6 +13,39 @@ export type PanelProps = { right?: React.ReactNode } } + +const useResizeObserver = ( + callback: (width: number) => void, + dependencies: React.DependencyList, +) => { + const elementRef = useRef(null) + + useEffect(() => { + const element = elementRef.current + if (!element) return + + const resizeObserver = new ResizeObserver((entries) => { + for (const entry of entries) { + const { inlineSize } = entry.borderBoxSize[0] + + const width = inlineSize > 0 ? inlineSize : element.getBoundingClientRect().width + callback(width) + } + }) + + resizeObserver.observe(element) + + const initialWidth = element.getBoundingClientRect().width + callback(initialWidth) + + return () => { + resizeObserver.disconnect() + } + }, dependencies) + + return elementRef +} + const Panel: FC = ({ components, }) => { @@ -20,44 +53,21 @@ const Panel: FC = ({ const selectedNode = nodes.find(node => node.data.selected) const showEnvPanel = useStore(s => s.showEnvPanel) const isRestoring = useStore(s => s.isRestoring) + const showWorkflowVersionHistoryPanel = useStore(s => s.showWorkflowVersionHistoryPanel) - const rightPanelRef = useRef(null) const setRightPanelWidth = useStore(s => s.setRightPanelWidth) + const setOtherPanelWidth = useStore(s => s.setOtherPanelWidth) - // get right panel width - useEffect(() => { - if (rightPanelRef.current) { - const resizeRightPanelObserver = new ResizeObserver((entries) => { - for (const entry of entries) { - const { inlineSize } = entry.borderBoxSize[0] - setRightPanelWidth(inlineSize) - } - }) - resizeRightPanelObserver.observe(rightPanelRef.current) - return () => { - resizeRightPanelObserver.disconnect() - } - } - }, [setRightPanelWidth]) + const rightPanelRef = useResizeObserver( + setRightPanelWidth, + [setRightPanelWidth, selectedNode, showEnvPanel, showWorkflowVersionHistoryPanel], + ) - const otherPanelRef = useRef(null) - const setOtherPanelWidth = useStore(s => s.setOtherPanelWidth) + const otherPanelRef = useResizeObserver( + setOtherPanelWidth, + [setOtherPanelWidth, showEnvPanel, showWorkflowVersionHistoryPanel], + ) - // get other panel width - useEffect(() => { - if (otherPanelRef.current) { - const resizeOtherPanelObserver = new ResizeObserver((entries) => { - for (const entry of entries) { - const { inlineSize } = entry.borderBoxSize[0] - setOtherPanelWidth(inlineSize) - } - }) - resizeOtherPanelObserver.observe(otherPanelRef.current) - return () => { - resizeOtherPanelObserver.disconnect() - } - } - }, [setOtherPanelWidth]) return (
= ({ className={cn('absolute bottom-1 right-0 top-14 z-10 flex outline-none')} key={`${isRestoring}`} > - { - components?.left - } - { - !!selectedNode && ( - - ) - } + {components?.left} + {!!selectedNode && }
- { - components?.right - } - { - showEnvPanel && ( - - ) - } + {components?.right} + {showEnvPanel && }
)