Ver Fonte

add generic support store hooks

AykutSarac há 2 anos atrás
pai
commit
b02f7ccb99
2 ficheiros alterados com 11 adições e 2 exclusões
  1. 4 1
      src/store/useConfig.tsx
  2. 7 1
      src/store/useGraph.tsx

+ 4 - 1
src/store/useConfig.tsx

@@ -2,9 +2,12 @@ import { ReactZoomPanPinchRef } from "react-zoom-pan-pinch";
 import { defaultJson } from "src/constants/data";
 import create from "zustand";
 
+type StateType = keyof typeof initialStates;
+type StateKey<T extends StateType> = typeof initialStates[T];
+
 interface ConfigActions {
   setJson: (json: string) => void;
-  setConfig: (key: keyof Config, value: unknown) => void;
+  setConfig: <T extends StateType, K extends StateKey<T>>(key: T, value: K) => void;
   getJson: () => string;
   zoomIn: () => void;
   zoomOut: () => void;

+ 7 - 1
src/store/useGraph.tsx

@@ -17,8 +17,14 @@ const initialStates = {
 
 export type Graph = typeof initialStates;
 
+type StateType = keyof typeof initialStates;
+type StateKey<T extends StateType> = typeof initialStates[T];
+
 interface GraphActions {
-  setGraphValue: (key: keyof Graph, value: any) => void;
+  setGraphValue: <T extends StateType, K extends StateKey<T>>(
+    key: T,
+    value: K
+  ) => void;
   setLoading: (loading: boolean) => void;
   setDirection: (direction: CanvasDirection) => void;
   expandNodes: (nodeId: string) => void;