Skip to content

Commit

Permalink
chore(meshwide): implement provider
Browse files Browse the repository at this point in the history
  • Loading branch information
selankon committed May 6, 2024
1 parent 8d1a0fd commit 884ffc1
Show file tree
Hide file tree
Showing 2 changed files with 70 additions and 21 deletions.
80 changes: 60 additions & 20 deletions plugins/lime-plugin-mesh-wide/src/hooks/useNodes.tsx
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
import { ComponentChildren, createContext } from "preact";
import { useMemo } from "preact/compat";
import { useContext } from "preact/hooks";

import { isValidCoordinate } from "plugins/lime-plugin-mesh-wide/src/lib/utils";
import {
Expand All @@ -7,8 +9,31 @@ import {
} from "plugins/lime-plugin-mesh-wide/src/meshWideQueries";
import { INodes } from "plugins/lime-plugin-mesh-wide/src/meshWideTypes";

// todo(kon): this should be inside a provider to don't repeat the calculations
export const useNodes = () => {
interface NodesContextType {
hasInvalidNodes: boolean;
allNodes: {
meshWideNodesReference: INodes;
meshWideNodesActual: INodes;
};
invalidNodes: {
invalidNodesReference: INodes;
invalidNodesActual: INodes;
};
locatedNodes: {
locatedNodesReference: INodes;
locatedNodesActual: INodes;
allLocatedNodes: INodes;
locatedNewNodes: INodes;
};
}

const NodesContext = createContext<NodesContextType>(null);

export const NodesProvider = ({
children,
}: {
children: ComponentChildren;
}) => {
const { data: meshWideNodesReference } = useMeshWideNodesReference({});
const { data: meshWideNodesActual } = useMeshWideNodes({});

Expand Down Expand Up @@ -83,22 +108,37 @@ export const useNodes = () => {
...locatedNewNodes,
};

return {
hasInvalidNodes,
allNodes: {
meshWideNodesReference,
meshWideNodesActual,
},
// Invalid nodes doesn't contain a correct lat long
invalidNodes: {
invalidNodesReference,
invalidNodesActual,
},
locatedNodes: {
locatedNodesReference,
locatedNodesActual,
allLocatedNodes,
locatedNewNodes, // New nodes (not on the ref state)
},
};
return (
<NodesContext.Provider
value={{
hasInvalidNodes,
allNodes: {
meshWideNodesReference,
meshWideNodesActual,
},
// Invalid nodes doesn't contain a correct lat long
invalidNodes: {
invalidNodesReference,
invalidNodesActual,
},
locatedNodes: {
locatedNodesReference,
locatedNodesActual,
allLocatedNodes,
locatedNewNodes, // New nodes (not on the ref state)
},
}}
>
{children}
</NodesContext.Provider>
);
};

// Helper hook to use the context
export const useNodes = () => {
const context = useContext(NodesContext);
if (context === null) {
throw new Error("useNodesContext must be used within a NodesProvider");
}
return context;
};
11 changes: 10 additions & 1 deletion plugins/lime-plugin-mesh-wide/src/meshWidePage.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,9 @@ import { useLoadLeaflet } from "plugins/lime-plugin-locate/src/locateQueries";
import { FloatingAlert } from "plugins/lime-plugin-mesh-wide/src/components/Map/FloatingAlert";
import { MeshWideMap } from "plugins/lime-plugin-mesh-wide/src/containers/Map";
import { SelectedFeatureBottomSheet } from "plugins/lime-plugin-mesh-wide/src/containers/SelectedFeatureBottomSheet";
import { NodesProvider } from "plugins/lime-plugin-mesh-wide/src/hooks/useNodes";

const MeshWidePage = () => {
const MeshWide = () => {
const {
isError: isAssetError,
isFetchedAfterMount: assetsLoaded,
Expand Down Expand Up @@ -45,4 +46,12 @@ const MeshWidePage = () => {
);
};

const MeshWidePage = () => {
return (
<NodesProvider>
<MeshWide />
</NodesProvider>
);
};

export default MeshWidePage;

0 comments on commit 884ffc1

Please sign in to comment.