|
|
@@ -1,24 +1,25 @@
|
|
|
-import React, { useEffect, useMemo, useRef, useState } from "react";
|
|
|
+import React, { useEffect, useRef, useState } from "react";
|
|
|
import mapboxgl from "mapbox-gl";
|
|
|
import { useTheme } from "next-themes";
|
|
|
import Image from "next/image";
|
|
|
|
|
|
+const LIGHT_STYLE = "mapbox://styles/mapbox/standard";
|
|
|
+const DARK_STYLE = "mapbox://styles/mapbox/dark-v10";
|
|
|
+
|
|
|
const MapComponent: React.FC = () => {
|
|
|
const { theme } = useTheme();
|
|
|
const mapContainerRef = useRef<HTMLDivElement>(null);
|
|
|
+ const mapRef = useRef<mapboxgl.Map | null>(null);
|
|
|
const [mapLoaded, setMapLoaded] = useState<boolean>(false);
|
|
|
|
|
|
- const style = useMemo(() => {
|
|
|
- return theme === "dark"
|
|
|
- ? "mapbox://styles/mapbox/dark-v10"
|
|
|
- : "mapbox://styles/mapbox/standard";
|
|
|
- }, [theme]);
|
|
|
-
|
|
|
+ // Create map once
|
|
|
useEffect(() => {
|
|
|
+ if (mapRef.current) return;
|
|
|
+
|
|
|
const map = new mapboxgl.Map({
|
|
|
accessToken: process.env.NEXT_PUBLIC_MAPBOX_TOKEN,
|
|
|
container: mapContainerRef.current as HTMLElement,
|
|
|
- style: style,
|
|
|
+ style: theme === "dark" ? DARK_STYLE : LIGHT_STYLE,
|
|
|
center: [-122.3321, 47.6062],
|
|
|
zoom: 10,
|
|
|
});
|
|
|
@@ -27,8 +28,19 @@ const MapComponent: React.FC = () => {
|
|
|
setMapLoaded(true);
|
|
|
});
|
|
|
|
|
|
- return () => map.remove(); // Cleanup on unmount
|
|
|
- }, [style]);
|
|
|
+ mapRef.current = map;
|
|
|
+
|
|
|
+ return () => {
|
|
|
+ map.remove();
|
|
|
+ mapRef.current = null;
|
|
|
+ };
|
|
|
+ }, []);
|
|
|
+
|
|
|
+ // Switch style without recreating the map
|
|
|
+ useEffect(() => {
|
|
|
+ if (!mapRef.current) return;
|
|
|
+ mapRef.current.setStyle(theme === "dark" ? DARK_STYLE : LIGHT_STYLE);
|
|
|
+ }, [theme]);
|
|
|
|
|
|
return (
|
|
|
<div className="w-full h-full rounded-3xl overflow-hidden">
|
|
|
@@ -43,9 +55,9 @@ const MapComponent: React.FC = () => {
|
|
|
)}
|
|
|
<div ref={mapContainerRef} className="w-full h-full" />
|
|
|
<div
|
|
|
- className="absolute top-1/2 left-1/2 transform -translate-x-1/2 -translate-y-1/2
|
|
|
+ className="absolute top-1/2 left-1/2 transform -translate-x-1/2 -translate-y-1/2
|
|
|
flex items-center justify-center
|
|
|
- w-14 h-14 md:w-20 md:h-20 rounded-full
|
|
|
+ w-14 h-14 md:w-20 md:h-20 rounded-full
|
|
|
shadow-lg cursor-pointer bg-blue-400/40 border-2 md:border-4 border-white/80 hover:animate-pulse"
|
|
|
>
|
|
|
<Image
|