Skip to content

🐸 Rybbit - open-source and privacy-friendly alternative to Google Analytics that is 10x more intuitive.

Add map style management to GlobeStore and integrate MapStyleSelector component

Bill Yang1f0676ce154e
diff --git a/client/src/app/[site]/globe/3d/hooks/useMapbox.ts b/client/src/app/[site]/globe/3d/hooks/useMapbox.ts
index 578165131..d1bbdc3a5 100644
--- a/client/src/app/[site]/globe/3d/hooks/useMapbox.ts
+++ b/client/src/app/[site]/globe/3d/hooks/useMapbox.ts
@@ -1,12 +1,39 @@
import { useEffect, useRef, useState } from "react";
import mapboxgl from "mapbox-gl";
import { useConfigs } from "../../../../../lib/configs";
+import { useGlobeStore } from "../../globeStore";
export function useMapbox(containerRef: React.RefObject<HTMLDivElement | null>, enabled: boolean = true) {
const map = useRef<mapboxgl.Map | null>(null);
const [mapLoaded, setMapLoaded] = useState(false);
const { configs } = useConfigs();
+ const { mapStyle } = useGlobeStore();
+
+ // Function to apply custom styling (fog, water color, etc.)
+ const applyCustomStyling = (mapInstance: mapboxgl.Map) => {
+ // Customize water/ocean color if the layer exists
+ try {
+ if (mapInstance.getLayer("water")) {
+ // mapInstance.setPaintProperty("water", "fill-color", "#0a1929");
+ }
+ } catch (error) {
+ console.warn("Could not set water color:", error);
+ }
+
+ // Set fog for starry background
+ try {
+ mapInstance.setFog({
+ color: "rgb(61, 76, 89)",
+ "high-color": "rgb(36, 92, 223)",
+ "horizon-blend": 0.01,
+ "space-color": "rgb(12, 12, 16)",
+ "star-intensity": 0.6,
+ });
+ } catch (error) {
+ console.warn("Could not set fog:", error);
+ }
+ };
useEffect(() => {
if (!enabled || !containerRef.current || !configs?.mapboxToken) {
@@ -28,8 +55,7 @@ export function useMapbox(containerRef: React.RefObject<HTMLDivElement | null>,
map.current = new mapboxgl.Map({
container: containerRef.current,
- // style: "mapbox://styles/mapbox/standard",
- style: "mapbox://styles/mapbox/dark-v11",
+ style: mapStyle,
projection: { name: "globe" },
zoom: 1.5,
center: [0, 20],
@@ -41,21 +67,7 @@ export function useMapbox(containerRef: React.RefObject<HTMLDivElement | null>,
map.current.on("style.load", () => {
if (!map.current) return;
-
- // Customize land color
- // map.current.setPaintProperty("land", "background-color", "#1a365d"); // dark blue
-
- // Customize water/ocean color
- map.current.setPaintProperty("water", "fill-color", "#0a1929"); // darker blue
-
- map.current.setFog({
- color: "rgb(61, 76, 89)",
- "high-color": "rgb(36, 92, 223)",
- "horizon-blend": 0.0,
- "space-color": "rgb(12, 12, 16)",
- "star-intensity": 0.6,
- });
-
+ applyCustomStyling(map.current);
setMapLoaded(true);
});
@@ -68,5 +80,27 @@ export function useMapbox(containerRef: React.RefObject<HTMLDivElement | null>,
};
}, [enabled, containerRef, configs?.mapboxToken]);
+ // Handle style changes
+ useEffect(() => {
+ if (!map.current || !mapLoaded) return;
+
+ const mapInstance = map.current;
+
+ const handleStyleLoad = () => {
+ // Wait for map to be idle (fully rendered) before applying custom styling
+ mapInstance.once("idle", () => {
+ applyCustomStyling(mapInstance);
+ });
+ };
+
+ mapInstance.once("style.load", handleStyleLoad);
+ mapInstance.setStyle(mapStyle);
+
+ // Cleanup function
+ return () => {
+ mapInstance.off("style.load", handleStyleLoad);
+ };
+ }, [mapStyle, mapLoaded]);
+
return { map, mapLoaded };
}
diff --git a/client/src/app/[site]/globe/components/MapStyleSelector.tsx b/client/src/app/[site]/globe/components/MapStyleSelector.tsx
new file mode 100644
index 000000000..af62c1320
--- /dev/null
+++ b/client/src/app/[site]/globe/components/MapStyleSelector.tsx
@@ -0,0 +1,54 @@
+import { Palette } from "lucide-react";
+import {
+ DropdownMenu,
+ DropdownMenuContent,
+ DropdownMenuItem,
+ DropdownMenuTrigger,
+} from "../../../../components/ui/dropdown-menu";
+import { useGlobeStore } from "../globeStore";
+
+interface MapStyle {
+ name: string;
+ url: string;
+}
+
+const MAP_STYLES: MapStyle[] = [
+ { name: "Standard", url: "mapbox://styles/mapbox/standard" },
+ { name: "Standard Satellite", url: "mapbox://styles/mapbox/standard-satellite" },
+ // { name: "Streets", url: "mapbox://styles/mapbox/streets-v12" },
+ { name: "Outdoors", url: "mapbox://styles/mapbox/outdoors-v12" },
+ { name: "Light", url: "mapbox://styles/mapbox/light-v11" },
+ { name: "Dark", url: "mapbox://styles/mapbox/dark-v11" },
+ { name: "Satellite", url: "mapbox://styles/mapbox/satellite-v9" },
+ { name: "Navigation Day", url: "mapbox://styles/mapbox/navigation-day-v1" },
+ { name: "Navigation Night", url: "mapbox://styles/mapbox/navigation-night-v1" },
+];
+
+export default function MapStyleSelector() {
+ const { mapStyle, setMapStyle } = useGlobeStore();
+
+ const currentStyle = MAP_STYLES.find(style => style.url === mapStyle);
+
+ return (
+ <DropdownMenu>
+ <DropdownMenuTrigger
+ className="text-xs font-medium rounded-lg bg-neutral-900/70 text-neutral-200 backdrop-blur-sm px-1.5 md:px-2.5 py-1.5 border border-neutral-800/50 hover:bg-neutral-800 hover:text-white transition-all inline-flex items-center gap-1.5"
+ unstyled
+ >
+ <Palette size={14} />
+ <span className="hidden md:inline">{currentStyle?.name || "Style"}</span>
+ </DropdownMenuTrigger>
+ <DropdownMenuContent align="start">
+ {MAP_STYLES.map(style => (
+ <DropdownMenuItem
+ key={style.url}
+ onClick={() => setMapStyle(style.url)}
+ className={mapStyle === style.url ? "bg-neutral-100 dark:bg-neutral-800" : ""}
+ >
+ {style.name}
+ </DropdownMenuItem>
+ ))}
+ </DropdownMenuContent>
+ </DropdownMenu>
+ );
+}
diff --git a/client/src/app/[site]/globe/globeStore.ts b/client/src/app/[site]/globe/globeStore.ts
index a101d2d6f..712e1d284 100644
--- a/client/src/app/[site]/globe/globeStore.ts
+++ b/client/src/app/[site]/globe/globeStore.ts
@@ -7,6 +7,8 @@ interface GlobeStore {
setMapView: (view: MapView) => void;
mapMode: "3D" | "2D";
setMapMode: (mode: "3D" | "2D") => void;
+ mapStyle: string;
+ setMapStyle: (style: string) => void;
}
export const useGlobeStore = create<GlobeStore>(set => ({
@@ -14,4 +16,6 @@ export const useGlobeStore = create<GlobeStore>(set => ({
setMapView: view => set({ mapView: view }),
mapMode: IS_CLOUD ? "3D" : "2D",
setMapMode: mode => set({ mapMode: mode }),
+ mapStyle: "mapbox://styles/mapbox/standard",
+ setMapStyle: style => set({ mapStyle: style }),
}));
diff --git a/client/src/app/[site]/globe/page.tsx b/client/src/app/[site]/globe/page.tsx
index f6c07dce3..cc557cfdf 100644
--- a/client/src/app/[site]/globe/page.tsx
+++ b/client/src/app/[site]/globe/page.tsx
@@ -12,6 +12,7 @@ import { useSetPageTitle } from "../../../hooks/useSetPageTitle";
import { SubHeader } from "../components/SubHeader/SubHeader";
import { GlobeSessions } from "./components/GlobeSessions";
import MapViewSelector from "./components/ModeSelector";
+import MapStyleSelector from "./components/MapStyleSelector";
import { TimelineScrubber } from "./components/TimelineScrubber";
import { OpenLayersMap } from "./2d/components/OpenLayersMap";
import { MapboxMap } from "./3d/components/MapboxMap";
@@ -84,7 +85,8 @@ export default function GlobePage() {
<MapViewSelector />
</div>
{mapView === "timeline" ? (
- <div className="pointer-events-auto">
+ <div className="pointer-events-auto flex gap-2">
+ {mapMode === "3D" && <MapStyleSelector />}
<Select
value={windowSize.toString()}
onValueChange={(value: string) => {
@@ -92,7 +94,7 @@ export default function GlobePage() {
setManualWindowSize(newSize);
}}
>
- <SelectTrigger className="w-[60px]" size="sm">
+ <SelectTrigger className="w-[60px] h-[30px]" size="sm">
<SelectValue />
</SelectTrigger>
<SelectContent>