Skip to content

Commit 33cab51

Browse files
committed
refactor: rename to MapProvider
1 parent f3068c5 commit 33cab51

8 files changed

Lines changed: 38 additions & 40 deletions

File tree

src/app.tsx

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ import { useEffect } from "react";
33
import "./app.css";
44
import Header from "./components/header";
55
import { Map } from "./components/map";
6-
import { LayersProvider } from "./components/map/provider";
6+
import { MapProvider } from "./components/map/provider";
77
import { Panel } from "./components/panel";
88
import { useStacValue } from "./components/stac/hooks";
99
import { isUrl } from "./components/stac/utils";
@@ -48,7 +48,7 @@ export default function App() {
4848
}, [error]);
4949

5050
return (
51-
<LayersProvider>
51+
<MapProvider>
5252
<Box zIndex={0} position={"absolute"} top={0} left={0}>
5353
<Map></Map>
5454
</Box>
@@ -69,7 +69,7 @@ export default function App() {
6969
</Box>
7070
)}
7171
<Toaster></Toaster>
72-
</LayersProvider>
72+
</MapProvider>
7373
);
7474
}
7575

src/components/map.tsx

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ import {
77
useControl,
88
type MapRef,
99
} from "react-map-gl/maplibre";
10-
import { useLayers } from "./map/context";
10+
import { useMap } from "./map/context";
1111
import { useColorModeValue } from "./ui/color-mode";
1212

1313
function DeckGLOverlay(props: DeckProps) {
@@ -22,13 +22,13 @@ export function Map() {
2222
"positron-gl-style",
2323
"dark-matter-gl-style"
2424
);
25-
const { layers, bounds } = useLayers();
25+
const { layers, fitBounds } = useMap();
2626

2727
useEffect(() => {
28-
if (bounds && mapRef.current) {
29-
mapRef.current.fitBounds(bounds, { padding: 200 });
28+
if (fitBounds && mapRef.current) {
29+
mapRef.current.fitBounds(fitBounds, { padding: 200 });
3030
}
31-
}, [bounds]);
31+
}, [fitBounds]);
3232

3333
return (
3434
<MaplibreMap

src/components/map/context.ts

Lines changed: 12 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -2,41 +2,39 @@ import { Layer } from "@deck.gl/core";
22
import type { LngLatBounds } from "maplibre-gl";
33
import { createContext, useContext, type Dispatch } from "react";
44

5-
export type LayersAction =
5+
export type MapAction =
66
| {
77
type: "set-layers";
88
layers: Layer[];
99
bbox?: number[];
1010
}
11-
| { type: "set-bbox"; bbox: number[] };
11+
| { type: "set-fit-bbox"; bbox: number[] };
1212

13-
export type LayersState = {
13+
export type MapState = {
1414
layers: Layer[];
15-
bounds?: LngLatBounds;
15+
fitBounds?: LngLatBounds;
1616
};
1717

18-
type LayersContextType = {
19-
state: LayersState;
20-
dispatch: Dispatch<LayersAction>;
18+
type MapContextType = {
19+
state: MapState;
20+
dispatch: Dispatch<MapAction>;
2121
};
2222

23-
export const LayersContext = createContext<LayersContextType | null>(null);
23+
export const LayersContext = createContext<MapContextType | null>(null);
2424

25-
export function useLayers() {
25+
export function useMap() {
2626
const context = useContext(LayersContext);
2727
if (context === null) {
28-
throw new Error("useLayers must be used from within a LayersProvider");
28+
throw new Error("useMap must be used from within a MapProvider");
2929
} else {
3030
return context.state;
3131
}
3232
}
3333

34-
export function useLayersDispatch() {
34+
export function useMapDispatch() {
3535
const context = useContext(LayersContext);
3636
if (context === null) {
37-
throw new Error(
38-
"useLayersDispatch must be used from within a LayersProvider"
39-
);
37+
throw new Error("useLayersDispatch must be used from within a MapProvider");
4038
} else {
4139
return context.dispatch;
4240
}

src/components/map/provider.tsx

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,23 +1,23 @@
11
import { LngLatBounds } from "maplibre-gl";
22
import { type ReactNode, useReducer } from "react";
3-
import { type LayersAction, type LayersState, LayersContext } from "./context";
3+
import { type MapAction, type MapState, LayersContext } from "./context";
44

5-
export function LayersProvider({ children }: { children: ReactNode }) {
5+
export function MapProvider({ children }: { children: ReactNode }) {
66
const [state, dispatch] = useReducer(reducer, { layers: [] });
77
return <LayersContext value={{ state, dispatch }}>{children}</LayersContext>;
88
}
99

10-
function reducer(state: LayersState, action: LayersAction) {
10+
function reducer(state: MapState, action: MapAction) {
1111
switch (action.type) {
1212
case "set-layers":
1313
return {
1414
...state,
1515
layers: action.layers,
16-
bounds: (action.bbox && bboxToBounds(action.bbox)) || undefined,
16+
fitBounds: (action.bbox && bboxToBounds(action.bbox)) || undefined,
1717
picked: undefined,
1818
};
19-
case "set-bbox":
20-
return { ...state, bounds: bboxToBounds(action.bbox) };
19+
case "set-fit-bbox":
20+
return { ...state, fitBounds: bboxToBounds(action.bbox) };
2121
}
2222
}
2323

src/components/stac/catalog.tsx

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ import type { BBox } from "geojson";
1313
import { useEffect, useState, type Dispatch, type SetStateAction } from "react";
1414
import { LuFolder } from "react-icons/lu";
1515
import type { StacCatalog, StacCollection, StacLink } from "stac-ts";
16-
import { useLayersDispatch } from "../map/context";
16+
import { useMapDispatch } from "../map/context";
1717
import { CollectionCard } from "./collection";
1818
import { ValueInfo } from "./shared";
1919
import { sanitizeBbox } from "./utils";
@@ -26,7 +26,7 @@ function Collections({
2626
setHref: Dispatch<SetStateAction<string>>;
2727
}) {
2828
const { collections, loading, error } = useCollections(href);
29-
const dispatch = useLayersDispatch();
29+
const dispatch = useMapDispatch();
3030

3131
useEffect(() => {
3232
const collectionBbox = [-180, -90, 180, 90];

src/components/stac/collection.tsx

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -15,14 +15,14 @@ import { useEffect, type Dispatch, type SetStateAction } from "react";
1515
import { LuFolderPlus, LuUpload, LuZoomIn } from "react-icons/lu";
1616
import type { StacCollection } from "stac-ts";
1717
import { RawJsonDialogButton } from "../json";
18-
import { useLayersDispatch } from "../map/context";
18+
import { useMapDispatch } from "../map/context";
1919
import { Tooltip } from "../ui/tooltip";
2020
import { AssetCard } from "./asset";
2121
import { ValueInfo } from "./shared";
2222
import { getSelfHref, sanitizeBbox } from "./utils";
2323

2424
export function Collection({ collection }: { collection: StacCollection }) {
25-
const dispatch = useLayersDispatch();
25+
const dispatch = useMapDispatch();
2626

2727
useEffect(() => {
2828
const bbox = sanitizeBbox(collection.extent.spatial.bbox[0]);
@@ -70,7 +70,7 @@ export function CollectionCard({
7070
setHref: Dispatch<SetStateAction<string>>;
7171
}) {
7272
const selfHref = getSelfHref(collection);
73-
const dispatch = useLayersDispatch();
73+
const dispatch = useMapDispatch();
7474
const thumbnail = Object.values(collection.assets || {}).find((asset) =>
7575
asset.roles?.includes("thumbnail")
7676
);
@@ -117,7 +117,7 @@ export function CollectionCard({
117117
size={"xs"}
118118
onClick={() =>
119119
dispatch({
120-
type: "set-bbox",
120+
type: "set-fit-bbox",
121121
bbox: collection.extent.spatial.bbox[0],
122122
})
123123
}

src/components/stac/item.tsx

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,12 +3,12 @@ import { GeoJsonLayer } from "@deck.gl/layers";
33
import { useEffect } from "react";
44
import { LuFile } from "react-icons/lu";
55
import type { StacItem } from "stac-ts";
6-
import { useLayersDispatch } from "../map/context";
6+
import { useMapDispatch } from "../map/context";
77
import { AssetCard } from "./asset";
88
import { ValueInfo } from "./shared";
99

1010
export function Item({ item }: { item: StacItem }) {
11-
const dispatch = useLayersDispatch();
11+
const dispatch = useMapDispatch();
1212

1313
useEffect(() => {
1414
if (item.geometry) {

src/components/stac/stac-geoparquet.tsx

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@ import {
2525
type SetStateAction,
2626
} from "react";
2727
import * as stacWasm from "../../stac-wasm";
28-
import { useLayersDispatch } from "../map/context";
28+
import { useMapDispatch } from "../map/context";
2929
import { toaster } from "../ui/toaster";
3030
import { useDuckDbQuery } from "./hooks";
3131
import type { StacValue } from "./types";
@@ -62,7 +62,7 @@ function Layer({
6262
path,
6363
select: "ST_AsWKB(geometry) as geometry, id",
6464
});
65-
const dispatch = useLayersDispatch();
65+
const dispatch = useMapDispatch();
6666

6767
useEffect(() => {
6868
if (table) {
@@ -123,7 +123,7 @@ function SummaryDataList({ path }: { path: string }) {
123123
"COUNT(*) as count, MIN(bbox.xmin) as xmin, MIN(bbox.ymin) as ymin, MAX(bbox.xmax) as xmax, MAX(bbox.ymax) as ymax",
124124
});
125125
const [summary, setSummary] = useState<Summary | undefined>();
126-
const dispatch = useLayersDispatch();
126+
const dispatch = useMapDispatch();
127127

128128
useEffect(() => {
129129
if (table) {
@@ -137,7 +137,7 @@ function SummaryDataList({ path }: { path: string }) {
137137

138138
useEffect(() => {
139139
if (summary?.bbox) {
140-
dispatch({ type: "set-bbox", bbox: summary.bbox });
140+
dispatch({ type: "set-fit-bbox", bbox: summary.bbox });
141141
}
142142
}, [summary?.bbox, dispatch]);
143143

0 commit comments

Comments
 (0)