Skip to content

Commit

Permalink
fix: extraced the useUpdateMinZoom hooks from the DynamicMinZoom comp…
Browse files Browse the repository at this point in the history
…onent
  • Loading branch information
sujal-into committed Nov 25, 2024
1 parent b42b56d commit 066f8cc
Show file tree
Hide file tree
Showing 2 changed files with 31 additions and 27 deletions.
30 changes: 3 additions & 27 deletions packages/components/socialFeed/Map/Map.web.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -30,11 +30,13 @@ import { PictureMapPost } from "@/components/socialFeed/Map/MapPosts/PictureMapP
import { VideoMapPost } from "@/components/socialFeed/Map/MapPosts/VideoMapPost";
import { useFetchFeedLocation } from "@/hooks/feed/useFetchFeed";
import { usePost } from "@/hooks/feed/usePost";
import { useUpdateMinZoom } from "@/hooks/feed/useUpdateMinZoom";
import { useSelectedNetworkId } from "@/hooks/useSelectedNetwork";
import {
DEFAULT_MAP_POSITION,
getMapPostIconColorRgba,
getMapPostIconSVGString,
MAP_HALF_BOUND,

Check failure on line 39 in packages/components/socialFeed/Map/Map.web.tsx

View workflow job for this annotation

GitHub Actions / check-js

'MAP_HALF_BOUND' is defined but never used
MAP_LAYER_URL,
MAP_MAX_BOUND,
} from "@/utils/feed/map";
Expand Down Expand Up @@ -116,33 +118,7 @@ const DynamicMinZoom = ({
setMinZoom: Dispatch<SetStateAction<number>>;
}) => {
const map = useMap();

useEffect(() => {
const lat = 45;
const lng = 90;

const adjustedWorldMapBounds: LatLngBoundsLiteral = [
[-lat, -lng],
[lat, lng],
];

const updateMinZoom = () => {
if (map) {
const calculatedZoom = map.getBoundsZoom(adjustedWorldMapBounds, false);
setMinZoom(calculatedZoom);
map.setMinZoom(calculatedZoom);
map.setZoom(calculatedZoom);
}
};

updateMinZoom();

window.addEventListener("resize", updateMinZoom);

return () => {
window.removeEventListener("resize", updateMinZoom);
};
}, [map, setMinZoom]);
useUpdateMinZoom(map, setMinZoom);

return null;
};
Expand Down
28 changes: 28 additions & 0 deletions packages/hooks/feed/useUpdateMinZoom.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
import { Map } from "leaflet";
import { Dispatch, SetStateAction, useEffect } from "react";

import { MAP_HALF_BOUND } from "@/utils/feed/map";

export const useUpdateMinZoom = (
map: Map,
setMinZoom: Dispatch<SetStateAction<number>>,
) => {
useEffect(() => {
const updateMinZoom = () => {
if (map) {
const calculatedZoom = map.getBoundsZoom(MAP_HALF_BOUND, false);
setMinZoom(calculatedZoom);
map.setMinZoom(calculatedZoom);
map.setZoom(calculatedZoom);
}
};

updateMinZoom();

window.addEventListener("resize", updateMinZoom);

return () => {
window.removeEventListener("resize", updateMinZoom);
};
}, [map, setMinZoom]);
};

0 comments on commit 066f8cc

Please sign in to comment.