Skip to content

Commit

Permalink
[GEN-1716]: fixed ReactFlow "fit to view" (#1769)
Browse files Browse the repository at this point in the history
Co-authored-by: Alon Braymok <138359965+alonkeyval@users.noreply.github.com>
  • Loading branch information
BenElferink and alonkeyval authored Nov 17, 2024
1 parent a8f6655 commit 293949a
Show file tree
Hide file tree
Showing 7 changed files with 75 additions and 42 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ const MenuContainer = styled.div`
display: flex;
align-items: center;
margin: 20px 0;
padding: 0 16px;
gap: 16px;
`;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ import { ToastList } from '@/components';
import MultiSourceControl from '../multi-source-control';
import { OverviewActionMenuContainer } from '../overview-actions-menu';
import { buildNodesAndEdges, NodeBaseDataFlow } from '@/reuseable-components';
import { useMetrics, useContainerWidth, useNodeDataFlowHandlers, useSourceCRUD, useDestinationCRUD, useInstrumentationRuleCRUD, useActionCRUD } from '@/hooks';
import { useMetrics, useContainerSize, useNodeDataFlowHandlers, useSourceCRUD, useDestinationCRUD, useInstrumentationRuleCRUD, useActionCRUD } from '@/hooks';

const AllDrawers = dynamic(() => import('../all-drawers'), {
ssr: false,
Expand All @@ -17,7 +17,7 @@ const AllModals = dynamic(() => import('../all-modals'), {
});

export const OverviewDataFlowWrapper = styled.div`
width: calc(100% - 64px);
width: 100%;
height: calc(100vh - 176px);
position: relative;
`;
Expand All @@ -31,7 +31,8 @@ export default function OverviewDataFlowContainer() {
const { actions } = useActionCRUD();
const { destinations } = useDestinationCRUD();
const { instrumentationRules } = useInstrumentationRuleCRUD();
const { containerRef, containerWidth } = useContainerWidth();
const { containerRef, containerWidth, containerHeight } = useContainerSize();

const { handleNodeClick } = useNodeDataFlowHandlers({
rules: instrumentationRules,
sources,
Expand All @@ -48,10 +49,11 @@ export default function OverviewDataFlowContainer() {
destinations,
metrics,
containerWidth,
containerHeight,
nodeWidth: NODE_WIDTH,
nodeHeight: NODE_HEIGHT,
});
}, [instrumentationRules, sources, actions, destinations, metrics, containerWidth]);
}, [instrumentationRules, sources, actions, destinations, metrics, containerWidth, containerHeight]);

return (
<OverviewDataFlowWrapper ref={containerRef}>
Expand Down
2 changes: 1 addition & 1 deletion frontend/webapp/hooks/common/index.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
export * from './useContainerWidth';
export * from './useContainerSize';
export * from './useOnClickOutside';
export * from './useKeyDown';
export * from './useTimeAgo';
25 changes: 25 additions & 0 deletions frontend/webapp/hooks/common/useContainerSize.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
import { useEffect, useState, useRef } from 'react';

export function useContainerSize() {
const containerRef = useRef<HTMLDivElement | null>(null);
const [containerWidth, setContainerWidth] = useState<number>(0);
const [containerHeight, setContainerHeight] = useState<number>(0);

useEffect(() => {
const resize = () => {
if (containerRef.current) {
const { width, height } = containerRef.current.getBoundingClientRect();

setContainerWidth(width);
setContainerHeight(height);
}
};

resize();

window.addEventListener('resize', resize);
return () => window.removeEventListener('resize', resize);
}, []);

return { containerRef, containerWidth, containerHeight };
}
23 changes: 0 additions & 23 deletions frontend/webapp/hooks/common/useContainerWidth.ts

This file was deleted.

43 changes: 30 additions & 13 deletions frontend/webapp/reuseable-components/nodes-data-flow/builder.ts
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,7 @@ export const buildNodesAndEdges = ({
destinations,
metrics,
containerWidth,
containerHeight,
nodeWidth,
nodeHeight,
}: {
Expand All @@ -64,6 +65,7 @@ export const buildNodesAndEdges = ({
destinations: ActualDestination[];
metrics?: OverviewMetricsResponse;
containerWidth: number;
containerHeight: number;
nodeWidth: number;
nodeHeight: number;
}) => {
Expand All @@ -77,33 +79,32 @@ export const buildNodesAndEdges = ({
};
}

// Calculate positions for each node
const startX = 0;
const endX = (containerWidth <= 1500 ? 1500 : containerWidth) - nodeWidth;
const startX = 24;
const endX = (containerWidth <= 1500 ? 1500 : containerWidth) - nodeWidth - 40 - startX;
const getY = (idx?: number) => nodeHeight * ((idx || 0) + 1);

const postions = {
rules: {
x: startX,
y: (idx?: number) => nodeHeight * ((idx || 0) + 1),
y: getY,
},
sources: {
x: getValueForRange(containerWidth, [
[0, 1500, endX / 3.5],
[1500, 1600, endX / 4],
[1600, null, endX / 4.5],
[0, 1600, endX / 3.5],
[1600, null, endX / 4],
]),
y: (idx?: number) => nodeHeight * ((idx || 0) + 1),
y: getY,
},
actions: {
x: getValueForRange(containerWidth, [
[0, 1500, endX / 1.55],
[1500, 1600, endX / 1.6],
[1600, null, endX / 1.65],
[0, 1600, endX / 1.55],
[1600, null, endX / 1.6],
]),
y: (idx?: number) => nodeHeight * ((idx || 0) + 1),
y: getY,
},
destinations: {
x: endX,
y: (idx?: number) => nodeHeight * ((idx || 0) + 1),
y: getY,
},
};

Expand Down Expand Up @@ -331,6 +332,22 @@ export const buildNodesAndEdges = ({
});
}

tempNodes['rules'].push(
createNode(
'hidden',
'hidden',
postions['rules']['x'],
containerHeight,
{},
{
width: 1,
height: 1,
opacity: 0,
pointerEvents: 'none',
},
),
);

Object.values(tempNodes).forEach((arr) => nodes.push(...arr));

return { nodes, edges };
Expand Down
13 changes: 12 additions & 1 deletion frontend/webapp/reuseable-components/nodes-data-flow/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,18 @@ export function NodeBaseDataFlow({ nodes, edges, onNodeClick, nodeWidth }: NodeB
<FlowWrapper>
<ReactFlow nodes={nodes} nodeTypes={nodeTypes} edges={edges} edgeTypes={edgeTypes} onNodeClick={onNodeClick} zoomOnScroll={false} fitView={false}>
<ControllerWrapper>
<Controls position='bottom-left' orientation='horizontal' showZoom showFitView showInteractive={false} />
<Controls
position='bottom-left'
orientation='horizontal'
showInteractive={false}
showZoom
showFitView
fitViewOptions={{
duration: 300,
padding: 0.02,
includeHiddenNodes: true,
}}
/>
</ControllerWrapper>
</ReactFlow>
</FlowWrapper>
Expand Down

0 comments on commit 293949a

Please sign in to comment.