Skip to content

Commit

Permalink
[core] remove cleanup for setSettings
Browse files Browse the repository at this point in the history
  • Loading branch information
sim51 committed Apr 9, 2024
1 parent e9f551d commit 4d02aa0
Showing 1 changed file with 12 additions and 26 deletions.
38 changes: 12 additions & 26 deletions packages/core/src/hooks/useSetSettings.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { useEffect, useState } from "react";
import { useCallback } from "react";
import { Settings } from "sigma/settings";

import { useSigmaContext } from "./context";
Expand All @@ -24,32 +24,18 @@ export function useSetSettings<
E extends Attributes = Attributes,
G extends Attributes = Attributes,
>(): (newSettings: Partial<Settings<N, E, G>>) => void {
const { sigma, container } = useSigmaContext<N, E, G>();
const [settings, setSettings] = useState<Partial<Settings<N, E, G>>>({});
const { sigma } = useSigmaContext<N, E, G>();

useEffect(() => {
if (!sigma || !settings) {
return;
}

const prevSettings: Partial<Settings<N, E, G>> = {};

(Object.keys(settings) as Array<keyof Settings<N, E, G>>).forEach((key) => {
// as never because of https://stackoverflow.com/questions/58656353/how-to-avoid-dynamic-keyof-object-assign-error-in-typescript
prevSettings[key] = settings[key] as never;
sigma.setSetting(key, settings[key] as never);
});

// cleanup
return () => {
if (sigma && container && container.offsetWidth > 0 && container.offsetHeight > 0) {
(Object.keys(prevSettings) as Array<keyof Settings<N, E, G>>).forEach((key) => {
// as never because of https://stackoverflow.com/questions/58656353/how-to-avoid-dynamic-keyof-object-assign-error-in-typescript
sigma.setSetting(key, prevSettings[key] as never);
});
}
};
}, [sigma, settings, container]);
const setSettings = useCallback(
(newSettings: Partial<Settings<N, E, G>>) => {
if (!sigma) return;
(Object.keys(newSettings) as Array<keyof Settings<N, E, G>>).forEach((key) => {
// as never because of https://stackoverflow.com/questions/58656353/how-to-avoid-dynamic-keyof-object-assign-error-in-typescript
sigma.setSetting(key, newSettings[key] as never);
});
},
[sigma],
);

return setSettings;
}

0 comments on commit 4d02aa0

Please sign in to comment.