Skip to content

Commit

Permalink
[Layout Quality] save the layout quality in the localstorage
Browse files Browse the repository at this point in the history
  • Loading branch information
sim51 committed Jun 13, 2024
1 parent fcde127 commit 67e6991
Showing 1 changed file with 16 additions and 3 deletions.
19 changes: 16 additions & 3 deletions src/core/layouts/index.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,11 @@
import { connectedCloseness } from "graphology-metrics/layout-quality";
import { debounce } from "lodash";
import { debounce, pick } from "lodash";

import { graphDatasetActions, graphDatasetAtom, sigmaGraphAtom } from "../graph";
import { dataGraphToFullGraph } from "../graph/utils";
import { resetCamera } from "../sigma";
import { atom } from "../utils/atoms";
import { stringify } from "../utils/json";
import { Producer, asyncAction, producerToAction } from "../utils/producers";
import { LAYOUTS } from "./collection";
import { LayoutMapping, LayoutQuality, LayoutState } from "./types";
Expand All @@ -13,11 +14,20 @@ function getEmptyLayoutState(): LayoutState {
return { quality: { enabled: false, showGrid: true }, type: "idle" };
}

function getLocalStorageLayoutState(): LayoutState {
const raw = localStorage.getItem("layout");
const state = raw ? JSON.parse(raw) : null;
return {
...getEmptyLayoutState(),
...state,
};
}

/**
* Public API:
* ***********
*/
export const layoutStateAtom = atom<LayoutState>(getEmptyLayoutState());
export const layoutStateAtom = atom<LayoutState>(getLocalStorageLayoutState());

/**
* Actions:
Expand Down Expand Up @@ -105,8 +115,8 @@ layoutStateAtom.bind((layoutState, prevState) => {
),
);

// Compute the layout quality metric when node's position changed
const { computeLayoutQualityMetric } = layoutActions;

if (updatedQualityKeys.has("enabled")) {
const fn = debounce(computeLayoutQualityMetric, 500, { leading: true, maxWait: 500 });
if (layoutState.quality.enabled) {
Expand All @@ -118,4 +128,7 @@ layoutStateAtom.bind((layoutState, prevState) => {
sigmaGraphAtom.get().off("nodeAttributesUpdated", fn);
}
}

// Save the quality in the localstorage
localStorage.setItem("layout", stringify(pick(layoutState, ["quality"])));
});

0 comments on commit 67e6991

Please sign in to comment.