Эх сурвалжийг харах

perf: fix theme switch lag on mobile

- Map: use map.setStyle() instead of destroying/recreating the entire
  Mapbox instance on every theme change
- MiniPic: use single Image with dynamic src instead of conditional
  mount/unmount of two separate Image components

Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
rriecc 9 сар өмнө
parent
commit
44fc2d6c0e

+ 24 - 12
components/map.tsx

@@ -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

+ 5 - 13
components/mini-pic.tsx

@@ -34,19 +34,11 @@ const MiniPic = ({ onClick, showOverlay = false }: MiniPicProps) => {
       onTouchStart={(e) => handleStart(e.touches[0].clientX, e.touches[0].clientY)}
       onTouchEnd={(e) => handleEnd(e.changedTouches[0].clientX, e.changedTouches[0].clientY)}
     >
-      {isDark ? (
-        <Image
-          alt="cover"
-          className="rounded-b-none object-cover"
-          src="mini-dark.jpg"
-        />
-      ) : (
-        <Image
-          alt="cover"
-          className="rounded-b-none object-cover"
-          src="mini-light.jpg"
-        />
-      )}
+      <Image
+        alt="Mini Cooper"
+        className="rounded-b-none object-cover"
+        src={isDark ? "mini-dark.jpg" : "mini-light.jpg"}
+      />
       {showOverlay && (
         <div className="absolute inset-0 flex items-end justify-center pb-4 z-10 pointer-events-none">
           <span className="bg-black/50 text-white text-xs px-3 py-1.5 rounded-full backdrop-blur-sm">