Skip to content

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

Refactor Globe components to streamline tooltip management and enhance map interaction

Bill Yang704abc3de714
diff --git a/client/src/app/[site]/globe/globe.css b/client/src/app/[site]/globe/globe.css
new file mode 100644
index 000000000..90391459f
--- /dev/null
+++ b/client/src/app/[site]/globe/globe.css
@@ -0,0 +1,16 @@
+/* Globe Mapbox Popup Styling */
+.mapboxgl-popup.globe-tooltip .mapboxgl-popup-content {
+ background-color: #0a0a0a;
+ color: white;
+ border-radius: 0.375rem;
+ padding: 0.5rem;
+ box-shadow: 0 10px 15px -3px rgb(0 0 0 / 0.1), 0 4px 6px -4px rgb(0 0 0 / 0.1);
+}
+
+.mapboxgl-popup.globe-tooltip .mapboxgl-popup-tip {
+ border-top-color: #0a0a0a;
+}
+
+.mapboxgl-popup.globe-tooltip .mapboxgl-popup-close-button {
+ display: none;
+}
diff --git a/client/src/app/[site]/globe/globeStore.ts b/client/src/app/[site]/globe/globeStore.ts
index 67bfc934c..ce43a0e1b 100644
--- a/client/src/app/[site]/globe/globeStore.ts
+++ b/client/src/app/[site]/globe/globeStore.ts
@@ -1,32 +1,12 @@
import { create } from "zustand";
import { MapView } from "./components/ModeSelector";
-interface TooltipContent {
- name: string;
- code: string;
- count: number;
- percentage: number;
-}
-
-interface TooltipPosition {
- x: number;
- y: number;
-}
-
interface GlobeStore {
- tooltipContent: TooltipContent | null;
- tooltipPosition: TooltipPosition;
mapView: MapView;
- setTooltipContent: (content: TooltipContent | null) => void;
- setTooltipPosition: (position: TooltipPosition) => void;
setMapView: (view: MapView) => void;
}
export const useGlobeStore = create<GlobeStore>(set => ({
- tooltipContent: null,
- tooltipPosition: { x: 0, y: 0 },
mapView: "coordinates",
- setTooltipContent: content => set({ tooltipContent: content }),
- setTooltipPosition: position => set({ tooltipPosition: position }),
setMapView: view => set({ mapView: view }),
}));
diff --git a/client/src/app/[site]/globe/hooks/useCoordinatesLayer.ts b/client/src/app/[site]/globe/hooks/useCoordinatesLayer.ts
index 1a5727f83..3466f0d3d 100644
--- a/client/src/app/[site]/globe/hooks/useCoordinatesLayer.ts
+++ b/client/src/app/[site]/globe/hooks/useCoordinatesLayer.ts
@@ -1,4 +1,4 @@
-import { useEffect } from "react";
+import { useEffect, useRef } from "react";
import mapboxgl from "mapbox-gl";
import { scaleSequentialSqrt } from "d3-scale";
import { interpolateYlOrRd } from "d3-scale-chromatic";
@@ -19,19 +19,28 @@ export function useCoordinatesLayer({
liveSessionLocations,
mapLoaded,
minutes,
- setTooltipContent,
mapView,
}: {
map: React.RefObject<mapboxgl.Map | null>;
liveSessionLocations: LiveSessionLocation[] | undefined;
mapLoaded: boolean;
minutes: number;
- setTooltipContent: (content: any) => void;
mapView: string;
}) {
+ const popupRef = useRef<mapboxgl.Popup | null>(null);
+
useEffect(() => {
if (!map.current || !liveSessionLocations || !mapLoaded) return;
+ // Initialize popup once
+ if (!popupRef.current) {
+ popupRef.current = new mapboxgl.Popup({
+ closeButton: false,
+ closeOnClick: false,
+ className: "globe-tooltip",
+ });
+ }
+
const addCoordinatesLayer = () => {
if (!map.current) return;
@@ -120,24 +129,30 @@ export function useCoordinatesLayer({
});
map.current.on("mousemove", "realtime-coordinates-layer", e => {
- if (!map.current || !e.features || e.features.length === 0) return;
+ if (!map.current || !e.features || e.features.length === 0 || !popupRef.current) return;
const feature = e.features[0];
const city = feature.properties?.city || "Unknown";
const count = feature.properties?.count || 0;
- setTooltipContent({
- name: city,
- code: "",
- count,
- percentage: 0,
- });
+ const coordinates = e.lngLat;
+ const html = `
+ <div class="flex items-center gap-1 mb-1">
+ <span class="text-sm font-medium">${city}</span>
+ </div>
+ <div class="text-sm">
+ <span class="font-bold text-accent-400">${count.toLocaleString()}</span>
+ <span class="text-neutral-300"> sessions</span>
+ </div>
+ `;
+
+ popupRef.current.setLngLat(coordinates).setHTML(html).addTo(map.current);
});
map.current.on("mouseleave", "realtime-coordinates-layer", () => {
- if (!map.current) return;
+ if (!map.current || !popupRef.current) return;
map.current.getCanvas().style.cursor = "";
- setTooltipContent(null);
+ popupRef.current.remove();
});
map.current.on("click", "realtime-coordinates-layer", e => {
@@ -171,5 +186,5 @@ export function useCoordinatesLayer({
};
addCoordinatesLayer();
- }, [liveSessionLocations, mapLoaded, map, minutes, setTooltipContent]);
+ }, [liveSessionLocations, mapLoaded, map, minutes]);
}
diff --git a/client/src/app/[site]/globe/hooks/useCountriesLayer.ts b/client/src/app/[site]/globe/hooks/useCountriesLayer.ts
index daf253102..4c59c1a43 100644
--- a/client/src/app/[site]/globe/hooks/useCountriesLayer.ts
+++ b/client/src/app/[site]/globe/hooks/useCountriesLayer.ts
@@ -1,4 +1,4 @@
-import { useEffect } from "react";
+import { useEffect, useRef } from "react";
import mapboxgl from "mapbox-gl";
import { FilterParameter } from "@rybbit/shared";
import { addFilter } from "../../../../lib/store";
@@ -9,7 +9,6 @@ interface UseCountriesLayerProps {
processedCountryData: any;
colorScale: (value: number) => string;
countryData: any;
- setTooltipContent: (content: any) => void;
mapLoaded: boolean;
mapView: string;
}
@@ -20,13 +19,23 @@ export function useCountriesLayer({
processedCountryData,
colorScale,
countryData,
- setTooltipContent,
mapLoaded,
mapView,
}: UseCountriesLayerProps) {
+ const popupRef = useRef<mapboxgl.Popup | null>(null);
+
useEffect(() => {
if (!map.current || !countriesGeoData || !processedCountryData || !mapLoaded) return;
+ // Initialize popup once
+ if (!popupRef.current) {
+ popupRef.current = new mapboxgl.Popup({
+ closeButton: false,
+ closeOnClick: false,
+ className: "globe-tooltip",
+ });
+ }
+
const addCountriesLayer = () => {
if (!map.current) return;
@@ -75,8 +84,8 @@ export function useCountriesLayer({
},
});
- map.current.on("mousemove", "countries-fill", (e) => {
- if (!map.current || !e.features || e.features.length === 0) return;
+ map.current.on("mousemove", "countries-fill", e => {
+ if (!map.current || !e.features || e.features.length === 0 || !popupRef.current) return;
map.current.getCanvas().style.cursor = "pointer";
const feature = e.features[0];
@@ -88,21 +97,28 @@ export function useCountriesLayer({
const foundData = currentData?.find((d: any) => d.value === code);
const percentage = foundData?.percentage || 0;
- setTooltipContent({
- name,
- code,
- count,
- percentage,
- });
+ // Use Mapbox native popup
+ const coordinates = e.lngLat;
+ const html = `
+ <div class="flex items-center gap-1 mb-1">
+ <span class="text-sm font-medium">${name}</span>
+ </div>
+ <div class="text-sm">
+ <span class="font-bold text-accent-400">${count.toLocaleString()}</span>
+ <span class="text-neutral-300"> (${percentage.toFixed(1)}%) sessions</span>
+ </div>
+ `;
+
+ popupRef.current.setLngLat(coordinates).setHTML(html).addTo(map.current);
});
map.current.on("mouseleave", "countries-fill", () => {
- if (!map.current) return;
+ if (!map.current || !popupRef.current) return;
map.current.getCanvas().style.cursor = "";
- setTooltipContent(null);
+ popupRef.current.remove();
});
- map.current.on("click", "countries-fill", (e) => {
+ map.current.on("click", "countries-fill", e => {
if (!e.features || e.features.length === 0) return;
const feature = e.features[0];
@@ -118,5 +134,5 @@ export function useCountriesLayer({
};
addCountriesLayer();
- }, [countriesGeoData, processedCountryData, colorScale, map, countryData, setTooltipContent, mapLoaded]);
+ }, [countriesGeoData, processedCountryData, colorScale, map, countryData, mapLoaded]);
}
diff --git a/client/src/app/[site]/globe/hooks/useSubdivisionsLayer.ts b/client/src/app/[site]/globe/hooks/useSubdivisionsLayer.ts
index f110d596b..327723de2 100644
--- a/client/src/app/[site]/globe/hooks/useSubdivisionsLayer.ts
+++ b/client/src/app/[site]/globe/hooks/useSubdivisionsLayer.ts
@@ -1,4 +1,4 @@
-import { useEffect } from "react";
+import { useEffect, useRef } from "react";
import mapboxgl from "mapbox-gl";
import { FilterParameter } from "@rybbit/shared";
import { addFilter } from "../../../../lib/store";
@@ -9,7 +9,6 @@ interface UseSubdivisionsLayerProps {
processedSubdivisionData: any;
colorScale: (value: number) => string;
subdivisionData: any;
- setTooltipContent: (content: any) => void;
mapLoaded: boolean;
mapView: string;
}
@@ -20,13 +19,23 @@ export function useSubdivisionsLayer({
processedSubdivisionData,
colorScale,
subdivisionData,
- setTooltipContent,
mapLoaded,
mapView,
}: UseSubdivisionsLayerProps) {
+ const popupRef = useRef<mapboxgl.Popup | null>(null);
+
useEffect(() => {
if (!map.current || !subdivisionsGeoData || !processedSubdivisionData || !mapLoaded) return;
+ // Initialize popup once
+ if (!popupRef.current) {
+ popupRef.current = new mapboxgl.Popup({
+ closeButton: false,
+ closeOnClick: false,
+ className: "globe-tooltip",
+ });
+ }
+
const addSubdivisionsLayer = () => {
if (!map.current) return;
@@ -75,8 +84,8 @@ export function useSubdivisionsLayer({
},
});
- map.current.on("mousemove", "subdivisions-fill", (e) => {
- if (!map.current || !e.features || e.features.length === 0) return;
+ map.current.on("mousemove", "subdivisions-fill", e => {
+ if (!map.current || !e.features || e.features.length === 0 || !popupRef.current) return;
map.current.getCanvas().style.cursor = "pointer";
const feature = e.features[0];
@@ -88,21 +97,27 @@ export function useSubdivisionsLayer({
const foundData = currentData?.find((d: any) => d.value === code);
const percentage = foundData?.percentage || 0;
- setTooltipContent({
- name,
- code,
- count,
- percentage,
- });
+ const coordinates = e.lngLat;
+ const html = `
+ <div class="flex items-center gap-1 mb-1">
+ <span class="text-sm font-medium">${name}</span>
+ </div>
+ <div class="text-sm">
+ <span class="font-bold text-accent-400">${count.toLocaleString()}</span>
+ <span class="text-neutral-300"> (${percentage.toFixed(1)}%) sessions</span>
+ </div>
+ `;
+
+ popupRef.current.setLngLat(coordinates).setHTML(html).addTo(map.current);
});
map.current.on("mouseleave", "subdivisions-fill", () => {
- if (!map.current) return;
+ if (!map.current || !popupRef.current) return;
map.current.getCanvas().style.cursor = "";
- setTooltipContent(null);
+ popupRef.current.remove();
});
- map.current.on("click", "subdivisions-fill", (e) => {
+ map.current.on("click", "subdivisions-fill", e => {
if (!e.features || e.features.length === 0) return;
const feature = e.features[0];
@@ -118,5 +133,5 @@ export function useSubdivisionsLayer({
};
addSubdivisionsLayer();
- }, [subdivisionsGeoData, processedSubdivisionData, colorScale, map, subdivisionData, setTooltipContent, mapLoaded]);
+ }, [subdivisionsGeoData, processedSubdivisionData, colorScale, map, subdivisionData, mapLoaded]);
}
diff --git a/client/src/app/[site]/globe/page.tsx b/client/src/app/[site]/globe/page.tsx
index e88adf6ea..ba144c415 100644
--- a/client/src/app/[site]/globe/page.tsx
+++ b/client/src/app/[site]/globe/page.tsx
@@ -1,13 +1,13 @@
"use client";
import "mapbox-gl/dist/mapbox-gl.css";
+import "./globe.css";
import { useMemo, useRef } from "react";
import { useSingleCol } from "@/api/analytics/useSingleCol";
import { DisabledOverlay } from "../../../components/DisabledOverlay";
import { useSetPageTitle } from "../../../hooks/useSetPageTitle";
import { useCountries, useSubdivisions } from "../../../lib/geo";
-import { CountryFlag } from "../components/shared/icons/CountryFlag";
import { SubHeader } from "../components/SubHeader/SubHeader";
import MapViewSelector from "./components/ModeSelector";
import { useMapbox } from "./hooks/useMapbox";
@@ -25,8 +25,7 @@ export default function GlobePage() {
useSetPageTitle("Rybbit · Globe");
const mapContainer = useRef<HTMLDivElement>(null);
- const { tooltipContent, tooltipPosition, mapView, setTooltipContent, setTooltipPosition, setMapView } =
- useGlobeStore();
+ const mapView = useGlobeStore(state => state.mapView);
const { data: countryData } = useSingleCol({ parameter: "country" });
const { data: subdivisionData } = useSingleCol({ parameter: "region", limit: 10000 });
@@ -51,7 +50,6 @@ export default function GlobePage() {
processedCountryData,
colorScale,
countryData,
- setTooltipContent,
mapLoaded,
mapView,
});
@@ -62,7 +60,6 @@ export default function GlobePage() {
processedSubdivisionData,
colorScale,
subdivisionData,
- setTooltipContent,
mapLoaded,
mapView,
});
@@ -72,7 +69,6 @@ export default function GlobePage() {
liveSessionLocations,
mapLoaded,
minutes: 30,
- setTooltipContent,
mapView,
});
@@ -80,17 +76,7 @@ export default function GlobePage() {
return (
<DisabledOverlay message="Globe" featurePath="globe">
- <div
- className="relative w-full h-dvh"
- onMouseMove={e => {
- if (tooltipContent) {
- setTooltipPosition({
- x: e.clientX,
- y: e.clientY,
- });
- }
- }}
- >
+ <div className="relative w-full h-dvh">
<div className="p-2 md:p-4 relative z-50">
<SubHeader />
</div>
@@ -106,25 +92,6 @@ export default function GlobePage() {
<GlobeSessions />
</div>
</div>
- {tooltipContent && (
- <div
- className="fixed z-50 bg-neutral-1000 text-white rounded-md p-2 shadow-lg text-sm pointer-events-none"
- style={{
- left: tooltipPosition.x,
- top: tooltipPosition.y - 10,
- transform: "translate(-50%, -100%)",
- }}
- >
- <div className="font-sm flex items-center gap-1">
- {tooltipContent.code && <CountryFlag country={tooltipContent.code.slice(0, 2)} />}
- {tooltipContent.name}
- </div>
- <div>
- <span className="font-bold text-accent-400">{tooltipContent.count.toLocaleString()}</span>{" "}
- <span className="text-neutral-300">({tooltipContent.percentage.toFixed(1)}%) sessions</span>
- </div>
- </div>
- )}
</div>
</DisabledOverlay>
);