map.tsx 1.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263
  1. import React, { useEffect, useMemo, useRef, useState } from "react";
  2. import mapboxgl from "mapbox-gl";
  3. import { useTheme } from "next-themes";
  4. import Image from "next/image";
  5. const MapComponent: React.FC = () => {
  6. const { theme } = useTheme();
  7. const mapContainerRef = useRef<HTMLDivElement>(null);
  8. const [mapLoaded, setMapLoaded] = useState<boolean>(false);
  9. const style = useMemo(() => {
  10. return theme === "dark"
  11. ? "mapbox://styles/mapbox/dark-v10"
  12. : "mapbox://styles/mapbox/standard";
  13. }, [theme]);
  14. useEffect(() => {
  15. const map = new mapboxgl.Map({
  16. accessToken: process.env.NEXT_PUBLIC_MAPBOX_TOKEN,
  17. container: mapContainerRef.current as HTMLElement,
  18. style: style,
  19. center: [-122.3321, 47.6062],
  20. zoom: 10,
  21. });
  22. map.on("load", () => {
  23. setMapLoaded(true);
  24. });
  25. return () => map.remove(); // Cleanup on unmount
  26. }, [style]);
  27. return (
  28. <div className="w-full h-full rounded-3xl overflow-hidden">
  29. {!mapLoaded && (
  30. <Image
  31. alt="Map"
  32. className="w-full h-full object-cover"
  33. height={300}
  34. src="/map.jpg"
  35. width={300}
  36. />
  37. )}
  38. <div ref={mapContainerRef} className="w-full h-full" />
  39. <div
  40. className="absolute top-1/2 left-1/2 transform -translate-x-1/2 -translate-y-1/2
  41. flex items-center justify-center
  42. w-14 h-14 md:w-20 md:h-20 rounded-full
  43. shadow-lg cursor-pointer bg-blue-400/40 border-2 md:border-4 border-white/80 hover:animate-pulse"
  44. >
  45. <Image
  46. alt="Icon"
  47. className="w-8 h-8 md:w-10 md:h-10"
  48. height={300}
  49. src="https://raw.githubusercontent.com/Tarikul-Islam-Anik/Animated-Fluent-Emojis/master/Emojis/Smilies/Hugging%20Face.png"
  50. width={300}
  51. />
  52. </div>
  53. </div>
  54. );
  55. };
  56. export default MapComponent;