🐸 Rybbit - open-source and privacy-friendly alternative to Google Analytics that is 10x more intuitive.
Add UserLocationMap to UserSidebar for enhanced user location visualization
Bill Yang689c45141d9b
diff --git a/client/src/app/[site]/user/[userId]/components/UserLocationMap.tsx b/client/src/app/[site]/user/[userId]/components/UserLocationMap.tsxnew file mode 100644index 000000000..d97aa4d8c--- /dev/null+++ b/client/src/app/[site]/user/[userId]/components/UserLocationMap.tsx@@ -0,0 +1,93 @@+"use client";++import mapboxgl from "mapbox-gl";+import "mapbox-gl/dist/mapbox-gl.css";+import { useEffect, useRef, useState } from "react";+import { useTheme } from "next-themes";+import { useConfigs } from "../../../../../lib/configs";++interface UserLocationMapProps {+ country: string;+ region: string;+ city: string;+}++export function UserLocationMap({ country, region, city }: UserLocationMapProps) {+ const containerRef = useRef<HTMLDivElement>(null);+ const mapRef = useRef<mapboxgl.Map | null>(null);+ const markerRef = useRef<mapboxgl.Marker | null>(null);+ const { configs } = useConfigs();+ const { resolvedTheme } = useTheme();+ const [error, setError] = useState(false);++ const style =+ resolvedTheme === "dark"+ ? "mapbox://styles/mapbox/dark-v11"+ : "mapbox://styles/mapbox/light-v11";++ useEffect(() => {+ if (!containerRef.current || !configs?.mapboxToken) return;++ const query = [city, region, country].filter(Boolean).join(", ");+ if (!query) {+ setError(true);+ return;+ }++ // Clean up previous map instance before creating a new one+ markerRef.current?.remove();+ markerRef.current = null;+ mapRef.current?.remove();+ mapRef.current = null;++ mapboxgl.accessToken = configs.mapboxToken;++ fetch(+ `https://api.mapbox.com/geocoding/v5/mapbox.places/${encodeURIComponent(query)}.json?access_token=${configs.mapboxToken}&limit=1`+ )+ .then(res => res.json())+ .then(data => {+ if (!data.features?.length || !containerRef.current) {+ setError(true);+ return;+ }++ const [lng, lat] = data.features[0].center;++ const map = new mapboxgl.Map({+ container: containerRef.current,+ style,+ center: [lng, lat],+ zoom: 1,+ pitch: 0,+ bearing: 0,+ antialias: true,+ attributionControl: false,+ interactive: true,+ });++ mapRef.current = map;++ markerRef.current = new mapboxgl.Marker({ color: "#10b981" })+ .setLngLat([lng, lat])+ .addTo(map);+ })+ .catch(() => setError(true));++ return () => {+ markerRef.current?.remove();+ markerRef.current = null;+ mapRef.current?.remove();+ mapRef.current = null;+ };+ }, [configs?.mapboxToken, country, region, city, style]);++ if (error) return null;++ return (+ <div+ ref={containerRef}+ className="w-full h-full rounded-md overflow-hidden [&_.mapboxgl-ctrl-bottom-left]:hidden! [&_.mapboxgl-ctrl-logo]:hidden! [&_.mapboxgl-ctrl-bottom-right]:hidden!"+ />+ );+}diff --git a/client/src/app/[site]/user/[userId]/components/UserSidebar.tsx b/client/src/app/[site]/user/[userId]/components/UserSidebar.tsxindex a072c336b..2a3f83b47 100644--- a/client/src/app/[site]/user/[userId]/components/UserSidebar.tsx+++ b/client/src/app/[site]/user/[userId]/components/UserSidebar.tsx@@ -18,6 +18,8 @@ import { VisitCalendar } from "./Calendar";import { EventIcon, PageviewIcon } from "../../../../../components/EventIcons";import { UserInfo, UserSessionCountResponse } from "../../../../../api/analytics/endpoints";import { DeviceIcon } from "../../../components/shared/icons/Device";+import { useConfigs } from "../../../../../lib/configs";+import { UserLocationMap } from "./UserLocationMap";interface UserSidebarProps {data: UserInfo | undefined;@@ -87,6 +89,7 @@ function StatCard({export function UserSidebar({ data, isLoading, sessionCount, getRegionName }: UserSidebarProps) {const t = useExtracted();const { formatRelative } = useDateTimeFormat();+ const { configs } = useConfigs();const isIdentified = !!data?.identified_user_id;// Filter custom traits (exclude username, name, email)@@ -277,6 +280,17 @@ export function UserSidebar({ data, isLoading, sessionCount, getRegionName }: Us</SidebarCard>)}+ {/* Location Map */}+ {configs?.mapboxToken && data?.country && (+ <div className="bg-white dark:bg-neutral-900 rounded-lg border border-neutral-100 dark:border-neutral-850 aspect-square overflow-hidden">+ <UserLocationMap+ country={data.country}+ region={data.region}+ city={data.city}+ />+ </div>+ )}+{/* Linked Devices (identified users only) */}{/* {isIdentified && data?.linked_devices && data.linked_devices.length > 0 && (<SidebarCard>