2024-04-08 18:51:46 +08:00
|
|
|
|
import type { FC } from 'react'
|
2025-06-27 10:50:33 +08:00
|
|
|
|
import { memo, useCallback, useEffect, useRef } from 'react'
|
2025-05-08 18:27:15 +08:00
|
|
|
|
import type { VersionHistoryPanelProps } from '@/app/components/workflow/panel/version-history-panel'
|
|
|
|
|
import VersionHistoryPanel from '@/app/components/workflow/panel/version-history-panel'
|
2025-07-02 18:07:09 +08:00
|
|
|
|
import { useShallow } from 'zustand/react/shallow'
|
2025-07-02 17:48:23 +08:00
|
|
|
|
import { useStore as useReactflow } from 'reactflow'
|
2024-04-08 18:51:46 +08:00
|
|
|
|
import { Panel as NodePanel } from '../nodes'
|
|
|
|
|
import { useStore } from '../store'
|
2024-07-22 15:29:39 +08:00
|
|
|
|
import EnvPanel from './env-panel'
|
2024-07-09 15:05:40 +08:00
|
|
|
|
import cn from '@/utils/classnames'
|
2024-04-08 18:51:46 +08:00
|
|
|
|
|
2025-04-18 13:59:12 +08:00
|
|
|
|
export type PanelProps = {
|
|
|
|
|
components?: {
|
|
|
|
|
left?: React.ReactNode
|
|
|
|
|
right?: React.ReactNode
|
|
|
|
|
}
|
2025-05-08 18:27:15 +08:00
|
|
|
|
versionHistoryPanelProps?: VersionHistoryPanelProps
|
2025-04-18 13:59:12 +08:00
|
|
|
|
}
|
2025-06-27 10:50:33 +08:00
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Reference MDN standard implementation:https://developer.mozilla.org/zh-CN/docs/Web/API/ResizeObserverEntry/borderBoxSize
|
|
|
|
|
*/
|
|
|
|
|
const getEntryWidth = (entry: ResizeObserverEntry, element: HTMLElement): number => {
|
|
|
|
|
if (entry.borderBoxSize?.length > 0)
|
|
|
|
|
return entry.borderBoxSize[0].inlineSize
|
|
|
|
|
|
|
|
|
|
if (entry.contentRect.width > 0)
|
|
|
|
|
return entry.contentRect.width
|
|
|
|
|
|
|
|
|
|
return element.getBoundingClientRect().width
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
const useResizeObserver = (
|
|
|
|
|
callback: (width: number) => void,
|
|
|
|
|
dependencies: React.DependencyList = [],
|
|
|
|
|
) => {
|
|
|
|
|
const elementRef = useRef<HTMLDivElement>(null)
|
|
|
|
|
|
|
|
|
|
const stableCallback = useCallback(callback, [callback])
|
|
|
|
|
|
|
|
|
|
useEffect(() => {
|
|
|
|
|
const element = elementRef.current
|
|
|
|
|
if (!element) return
|
|
|
|
|
|
|
|
|
|
const resizeObserver = new ResizeObserver((entries) => {
|
|
|
|
|
for (const entry of entries) {
|
|
|
|
|
const width = getEntryWidth(entry, element)
|
|
|
|
|
stableCallback(width)
|
|
|
|
|
}
|
|
|
|
|
})
|
|
|
|
|
|
|
|
|
|
resizeObserver.observe(element)
|
|
|
|
|
|
|
|
|
|
const initialWidth = element.getBoundingClientRect().width
|
|
|
|
|
stableCallback(initialWidth)
|
|
|
|
|
|
|
|
|
|
return () => {
|
|
|
|
|
resizeObserver.disconnect()
|
|
|
|
|
}
|
|
|
|
|
}, [stableCallback, ...dependencies])
|
|
|
|
|
return elementRef
|
|
|
|
|
}
|
|
|
|
|
|
2025-04-18 13:59:12 +08:00
|
|
|
|
const Panel: FC<PanelProps> = ({
|
|
|
|
|
components,
|
2025-05-08 18:27:15 +08:00
|
|
|
|
versionHistoryPanelProps,
|
2025-04-18 13:59:12 +08:00
|
|
|
|
}) => {
|
2025-07-02 17:48:23 +08:00
|
|
|
|
const selectedNode = useReactflow(useShallow((s) => {
|
|
|
|
|
const nodes = s.getNodes()
|
|
|
|
|
const currentNode = nodes.find(node => node.data.selected)
|
|
|
|
|
|
|
|
|
|
if (currentNode) {
|
|
|
|
|
return {
|
|
|
|
|
id: currentNode.id,
|
|
|
|
|
type: currentNode.type,
|
|
|
|
|
data: currentNode.data,
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}))
|
2024-07-22 15:29:39 +08:00
|
|
|
|
const showEnvPanel = useStore(s => s.showEnvPanel)
|
2024-04-24 13:05:33 +08:00
|
|
|
|
const isRestoring = useStore(s => s.isRestoring)
|
2025-05-08 18:27:15 +08:00
|
|
|
|
const showWorkflowVersionHistoryPanel = useStore(s => s.showWorkflowVersionHistoryPanel)
|
2024-04-08 18:51:46 +08:00
|
|
|
|
|
2025-06-24 09:10:30 +08:00
|
|
|
|
const setRightPanelWidth = useStore(s => s.setRightPanelWidth)
|
2025-06-27 10:50:33 +08:00
|
|
|
|
const setOtherPanelWidth = useStore(s => s.setOtherPanelWidth)
|
2025-06-24 09:10:30 +08:00
|
|
|
|
|
2025-06-27 10:50:33 +08:00
|
|
|
|
const rightPanelRef = useResizeObserver(
|
|
|
|
|
setRightPanelWidth,
|
|
|
|
|
[setRightPanelWidth, selectedNode, showEnvPanel, showWorkflowVersionHistoryPanel],
|
|
|
|
|
)
|
2025-06-24 09:10:30 +08:00
|
|
|
|
|
2025-06-27 10:50:33 +08:00
|
|
|
|
const otherPanelRef = useResizeObserver(
|
|
|
|
|
setOtherPanelWidth,
|
|
|
|
|
[setOtherPanelWidth, showEnvPanel, showWorkflowVersionHistoryPanel],
|
|
|
|
|
)
|
2025-06-24 09:10:30 +08:00
|
|
|
|
|
2024-04-08 18:51:46 +08:00
|
|
|
|
return (
|
2024-04-25 14:02:06 +08:00
|
|
|
|
<div
|
2025-06-24 09:10:30 +08:00
|
|
|
|
ref={rightPanelRef}
|
2024-04-25 14:02:06 +08:00
|
|
|
|
tabIndex={-1}
|
2025-06-24 09:10:30 +08:00
|
|
|
|
className={cn('absolute bottom-1 right-0 top-14 z-10 flex outline-none')}
|
2024-04-25 14:02:06 +08:00
|
|
|
|
key={`${isRestoring}`}
|
|
|
|
|
>
|
2025-06-27 10:50:33 +08:00
|
|
|
|
{components?.left}
|
|
|
|
|
{!!selectedNode && <NodePanel {...selectedNode} />}
|
2025-06-24 09:10:30 +08:00
|
|
|
|
<div
|
2025-06-27 10:50:33 +08:00
|
|
|
|
className="relative"
|
2025-06-24 09:10:30 +08:00
|
|
|
|
ref={otherPanelRef}
|
|
|
|
|
>
|
|
|
|
|
{
|
|
|
|
|
components?.right
|
|
|
|
|
}
|
2025-06-26 15:21:24 +08:00
|
|
|
|
{
|
|
|
|
|
showWorkflowVersionHistoryPanel && (
|
|
|
|
|
<VersionHistoryPanel {...versionHistoryPanelProps} />
|
|
|
|
|
)
|
|
|
|
|
}
|
2025-06-24 09:10:30 +08:00
|
|
|
|
{
|
|
|
|
|
showEnvPanel && (
|
|
|
|
|
<EnvPanel />
|
|
|
|
|
)
|
|
|
|
|
}
|
|
|
|
|
</div>
|
2024-04-08 18:51:46 +08:00
|
|
|
|
</div>
|
|
|
|
|
)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
export default memo(Panel)
|