Skip to content

Commit

Permalink
frontend Map: Remember last used zoom mode
Browse files Browse the repository at this point in the history
Signed-off-by: Oleksandr Dubenko <[email protected]>
  • Loading branch information
sniok committed Dec 10, 2024
1 parent 66167b7 commit 859c864
Showing 1 changed file with 8 additions and 2 deletions.
10 changes: 8 additions & 2 deletions frontend/src/components/resourceMap/useGraphViewport.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import { getNodesBounds, getViewportForBounds, Node, useReactFlow, useStore } from '@xyflow/react';
import { useCallback, useMemo } from 'react';
import { useLocalStorageState } from '../globalSearch/useLocalStorageState';
import { maxZoom, minZoom, viewportPaddingPx } from './graphConstants';

/**
Expand All @@ -16,6 +17,7 @@ type zoomMode = '100%' | 'fit';

/** Helper hook to deal with viewport zooming */
export const useGraphViewport = () => {
const [zoomMode, setZoomMode] = useLocalStorageState<zoomMode>('map-zoom-mode', '100%');
const reactFlowWidth = useStore(it => it.width);
const reactFlowHeight = useStore(it => it.height);
const aspectRatio = useStore(it => it.width / it.height);
Expand All @@ -24,13 +26,17 @@ export const useGraphViewport = () => {
const updateViewport = useCallback(
({
nodes = flow.getNodes(),
mode = '100%',
mode = zoomMode,
}: {
/** List of nodes, if not provided will use current nodes in the graph */
nodes?: Node[];
/** Zoom mode. More info in the type definition {@link zoomMode} */
mode?: zoomMode;
}) => {
if (mode !== zoomMode) {
setZoomMode(() => mode);
}

const bounds = getNodesBounds(nodes);

if (mode === 'fit') {
Expand Down Expand Up @@ -74,7 +80,7 @@ export const useGraphViewport = () => {

console.error('Unknown zoom mode', mode);
},
[flow, reactFlowWidth, reactFlowHeight]
[flow, zoomMode, reactFlowWidth, reactFlowHeight]
);

return useMemo(() => ({ updateViewport, aspectRatio }), [updateViewport, aspectRatio]);
Expand Down

0 comments on commit 859c864

Please sign in to comment.