refactor(react): cleanup provider
This commit is contained in:
@@ -1,22 +1,10 @@
|
|||||||
import { useRef, type ReactNode } from 'react';
|
import { useState, type ReactNode } from 'react';
|
||||||
import { type StoreApi } from 'zustand';
|
|
||||||
import { UseBoundStoreWithEqualityFn } from 'zustand/traditional';
|
|
||||||
|
|
||||||
import { Provider } from '../../contexts/RFStoreContext';
|
import { Provider } from '../../contexts/StoreContext';
|
||||||
import { createRFStore } from '../../store';
|
import { createStore } from '../../store';
|
||||||
import type { ReactFlowState, Node, Edge } from '../../types';
|
import type { Node, Edge } from '../../types';
|
||||||
|
|
||||||
export function ReactFlowProvider({
|
export type ReactFlowProviderProps = {
|
||||||
children,
|
|
||||||
initialNodes,
|
|
||||||
initialEdges,
|
|
||||||
defaultNodes,
|
|
||||||
defaultEdges,
|
|
||||||
initialWidth,
|
|
||||||
initialHeight,
|
|
||||||
fitView,
|
|
||||||
}: {
|
|
||||||
children: ReactNode;
|
|
||||||
initialNodes?: Node[];
|
initialNodes?: Node[];
|
||||||
initialEdges?: Edge[];
|
initialEdges?: Edge[];
|
||||||
defaultNodes?: Node[];
|
defaultNodes?: Node[];
|
||||||
@@ -24,19 +12,30 @@ export function ReactFlowProvider({
|
|||||||
initialWidth?: number;
|
initialWidth?: number;
|
||||||
initialHeight?: number;
|
initialHeight?: number;
|
||||||
fitView?: boolean;
|
fitView?: boolean;
|
||||||
}) {
|
children: ReactNode;
|
||||||
const storeRef = useRef<UseBoundStoreWithEqualityFn<StoreApi<ReactFlowState>> | null>(null);
|
};
|
||||||
if (!storeRef.current) {
|
|
||||||
storeRef.current = createRFStore({
|
export function ReactFlowProvider({
|
||||||
nodes: initialNodes,
|
initialNodes: nodes,
|
||||||
edges: initialEdges,
|
initialEdges: edges,
|
||||||
|
defaultNodes,
|
||||||
|
defaultEdges,
|
||||||
|
initialWidth: width,
|
||||||
|
initialHeight: height,
|
||||||
|
fitView,
|
||||||
|
children,
|
||||||
|
}: ReactFlowProviderProps) {
|
||||||
|
const [store] = useState(() =>
|
||||||
|
createStore({
|
||||||
|
nodes,
|
||||||
|
edges,
|
||||||
defaultNodes,
|
defaultNodes,
|
||||||
defaultEdges,
|
defaultEdges,
|
||||||
width: initialWidth,
|
width,
|
||||||
height: initialHeight,
|
height,
|
||||||
fitView,
|
fitView,
|
||||||
});
|
})
|
||||||
}
|
);
|
||||||
|
|
||||||
return <Provider value={storeRef.current}>{children}</Provider>;
|
return <Provider value={store}>{children}</Provider>;
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -1,6 +1,6 @@
|
|||||||
import { useContext, type ReactNode } from 'react';
|
import { useContext, type ReactNode } from 'react';
|
||||||
|
|
||||||
import StoreContext from '../../contexts/RFStoreContext';
|
import StoreContext from '../../contexts/StoreContext';
|
||||||
import { ReactFlowProvider } from '../../components/ReactFlowProvider';
|
import { ReactFlowProvider } from '../../components/ReactFlowProvider';
|
||||||
import type { Node, Edge } from '../../types';
|
import type { Node, Edge } from '../../types';
|
||||||
|
|
||||||
|
|||||||
@@ -1,8 +0,0 @@
|
|||||||
import { createContext } from 'react';
|
|
||||||
|
|
||||||
import { createRFStore } from '../store';
|
|
||||||
|
|
||||||
const StoreContext = createContext<ReturnType<typeof createRFStore> | null>(null);
|
|
||||||
|
|
||||||
export const Provider = StoreContext.Provider;
|
|
||||||
export default StoreContext;
|
|
||||||
@@ -0,0 +1,8 @@
|
|||||||
|
import { createContext } from 'react';
|
||||||
|
|
||||||
|
import { createStore } from '../store';
|
||||||
|
|
||||||
|
const StoreContext = createContext<ReturnType<typeof createStore> | null>(null);
|
||||||
|
|
||||||
|
export const Provider = StoreContext.Provider;
|
||||||
|
export default StoreContext;
|
||||||
@@ -1,10 +1,10 @@
|
|||||||
import { useContext, useMemo } from 'react';
|
import { useContext, useMemo } from 'react';
|
||||||
import { UseBoundStoreWithEqualityFn, useStoreWithEqualityFn as useZustandStore } from 'zustand/traditional';
|
import { UseBoundStoreWithEqualityFn, useStoreWithEqualityFn as useZustandStore } from 'zustand/traditional';
|
||||||
|
import { StoreApi } from 'zustand';
|
||||||
import { errorMessages } from '@xyflow/system';
|
import { errorMessages } from '@xyflow/system';
|
||||||
|
|
||||||
import StoreContext from '../contexts/RFStoreContext';
|
import StoreContext from '../contexts/StoreContext';
|
||||||
import type { Edge, Node, ReactFlowState } from '../types';
|
import type { Edge, Node, ReactFlowState } from '../types';
|
||||||
import { StoreApi } from 'zustand';
|
|
||||||
|
|
||||||
const zustandErrorMessage = errorMessages['error001']();
|
const zustandErrorMessage = errorMessages['error001']();
|
||||||
|
|
||||||
@@ -47,7 +47,6 @@ function useStoreApi<NodeType extends Node = Node, EdgeType extends Edge = Edge>
|
|||||||
getState: store.getState,
|
getState: store.getState,
|
||||||
setState: store.setState,
|
setState: store.setState,
|
||||||
subscribe: store.subscribe,
|
subscribe: store.subscribe,
|
||||||
destroy: store.destroy,
|
|
||||||
}),
|
}),
|
||||||
[store]
|
[store]
|
||||||
);
|
);
|
||||||
|
|||||||
@@ -18,7 +18,7 @@ import { applyEdgeChanges, applyNodeChanges, createSelectionChange, getSelection
|
|||||||
import getInitialState from './initialState';
|
import getInitialState from './initialState';
|
||||||
import type { ReactFlowState, Node, Edge, UnselectNodesAndEdgesParams, FitViewOptions } from '../types';
|
import type { ReactFlowState, Node, Edge, UnselectNodesAndEdgesParams, FitViewOptions } from '../types';
|
||||||
|
|
||||||
const createRFStore = ({
|
const createStore = ({
|
||||||
nodes,
|
nodes,
|
||||||
edges,
|
edges,
|
||||||
defaultNodes,
|
defaultNodes,
|
||||||
@@ -329,4 +329,4 @@ const createRFStore = ({
|
|||||||
Object.is
|
Object.is
|
||||||
);
|
);
|
||||||
|
|
||||||
export { createRFStore };
|
export { createStore };
|
||||||
|
|||||||
Reference in New Issue
Block a user