Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: Zoom label not updating on selection/input #389

Merged
merged 5 commits into from
Feb 7, 2024
Merged
Show file tree
Hide file tree
Changes from 4 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 6 additions & 1 deletion src/containers/Views/GraphView/index.tsx
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import React from "react";
import dynamic from "next/dynamic";
import styled from "styled-components";
import debounce from "lodash.debounce";
import { toast } from "react-hot-toast";
import { Space } from "react-zoomable-ui";
import { ElkRoot } from "reaflow/dist/layout/useLayout";
Expand Down Expand Up @@ -96,7 +97,6 @@ const GraphCanvas = ({ isWidget }: GraphProps) => {
const direction = useGraph(state => state.direction);
const nodes = useGraph(state => state.nodes);
const edges = useGraph(state => state.edges);

const [paneWidth, setPaneWidth] = React.useState(2000);
const [paneHeight, setPaneHeight] = React.useState(2000);

Expand Down Expand Up @@ -156,6 +156,7 @@ function getViewType(nodes: NodeData[]) {

export const Graph = ({ isWidget = false }: GraphProps) => {
const setViewPort = useGraph(state => state.setViewPort);
const viewPort = useGraph(state => state.viewPort);
const loading = useGraph(state => state.loading);
const isPremium = useUser(state => state.premium);
const viewType = useGraph(state => getViewType(state.nodes));
Expand Down Expand Up @@ -192,6 +193,9 @@ export const Graph = ({ isWidget = false }: GraphProps) => {
if (viewType === "premium" && !isWidget) {
if (!isPremium) return <PremiumView />;
}
const debouncedOnZoomChangeHandler = debounce(() => {
setViewPort(viewPort!);
}, 300);

return (
<>
Expand All @@ -205,6 +209,7 @@ export const Graph = ({ isWidget = false }: GraphProps) => {
{...bindLongPress()}
>
<Space
onUpdated={() => debouncedOnZoomChangeHandler()}
onCreate={setViewPort}
onContextMenu={e => e.preventDefault()}
treatTwoFingerTrackPadGesturesLikeTouch={gesturesEnabled}
Expand Down
4 changes: 4 additions & 0 deletions src/store/useGraph.ts
Shahidullah-Muffakir marked this conversation as resolved.
Show resolved Hide resolved
Original file line number Diff line number Diff line change
Expand Up @@ -194,14 +194,17 @@ const useGraph = create<Graph & GraphActions>((set, get) => ({
setZoomFactor: zoomFactor => {
const viewPort = get().viewPort;
viewPort?.camera?.recenter(viewPort.centerX, viewPort.centerY, zoomFactor);
set({ viewPort });
},
zoomIn: () => {
const viewPort = get().viewPort;
viewPort?.camera?.recenter(viewPort.centerX, viewPort.centerY, viewPort.zoomFactor + 0.1);
set({viewPort})
},
zoomOut: () => {
const viewPort = get().viewPort;
viewPort?.camera?.recenter(viewPort.centerX, viewPort.centerY, viewPort.zoomFactor - 0.1);
set({ viewPort });
},
centerView: () => {
const viewPort = get().viewPort;
Expand All @@ -211,6 +214,7 @@ const useGraph = create<Graph & GraphActions>((set, get) => ({
if (canvas) {
viewPort?.camera?.centerFitElementIntoView(canvas);
}
set({ viewPort });
},
toggleFullscreen: fullscreen => set({ fullscreen }),
setViewPort: viewPort => set({ viewPort }),
Expand Down