import * as React from 'react'; import { SELECTION_STATE, useVisualizationController, useVisualizationState, Node, } from '@patternfly/react-topology'; import { PipelineRunKind } from '~/types'; import SidePanel from '../../SidePanel/SidePanel'; import TaskRunPanel from './sidepanels/TaskRunPanel'; import { isTaskNode } from './visualization/utils/pipelinerun-graph-utils'; type Props = { scrollIntoView?: (node: Node) => void; pipelineRun?: PipelineRunKind; }; const PipelineRunSidePanel: React.FC> = ({ scrollIntoView, pipelineRun, }) => { const [[selectedId], setSelectedIds] = useVisualizationState(SELECTION_STATE, []); const controller = useVisualizationController(); const taskNode = React.useMemo(() => { if (selectedId) { const element = controller.getElementById(selectedId); if (isTaskNode(element)) { return element; } } return null; }, [controller, selectedId]); const panel = taskNode ? ( setSelectedIds([])} taskNode={taskNode} pipelineRun={pipelineRun} /> ) : null; const isExpanded = !!panel; const onExpand = React.useCallback(() => { if (isExpanded && taskNode) { scrollIntoView?.(taskNode); } }, [isExpanded, scrollIntoView, taskNode]); React.useEffect(() => { if (isExpanded && taskNode) { scrollIntoView?.(taskNode); } // only run when the node changes // eslint-disable-next-line react-hooks/exhaustive-deps }, [taskNode]); return ( {panel} ); }; export default PipelineRunSidePanel;