🐸 Rybbit - open-source and privacy-friendly alternative to Google Analytics that is 10x more intuitive.
color coordinates layer when selected
Bill Yangd721973d57b0
diff --git a/client/src/app/[site]/components/shared/Filters/ValueSelect.tsx b/client/src/app/[site]/components/shared/Filters/ValueSelect.tsxindex 88ae058fd..86b76f737 100644--- a/client/src/app/[site]/components/shared/Filters/ValueSelect.tsx+++ b/client/src/app/[site]/components/shared/Filters/ValueSelect.tsx@@ -13,8 +13,8 @@ export function ValueSelect({onChange,}: {parameter: FilterParameter;- value: string[];- onChange: (values: string[]) => void;+ value: (string | number)[];+ onChange: (values: (string | number)[]) => void;}) {const { data, isFetching } = useSingleCol({parameter,@@ -24,15 +24,15 @@ export function ValueSelect({const { getRegionName } = useGetRegionName();- const getValueLabel = (val: string) => {+ const getValueLabel = (val: string | number) => {if (parameter === "country") {- return getCountryName(val);+ return getCountryName(val as string);}if (parameter === "region") {- return getRegionName(val);+ return getRegionName(val as string);}if (parameter === "language") {- return getLanguageName(val);+ return getLanguageName(val as string);}return val;};diff --git a/client/src/app/[site]/components/shared/Filters/utils.ts b/client/src/app/[site]/components/shared/Filters/utils.tsindex 8dd1ce19e..da43a1767 100644--- a/client/src/app/[site]/components/shared/Filters/utils.ts+++ b/client/src/app/[site]/components/shared/Filters/utils.ts@@ -76,12 +76,12 @@ export const filterTypeToLabel = (type: FilterType) => {};export function getParameterValueLabel(filter: Filter, getRegionName: (region: string) => string | undefined) {- const formatValue = (value: string) => {+ const formatValue = (value: string | number) => {if (filter.parameter === "country") {- return getCountryName(value);+ return getCountryName(value as string);}if (filter.parameter === "region") {- return getRegionName(value);+ return getRegionName(value as string);}return value;};diff --git a/client/src/app/[site]/globe/hooks/useCoordinatesLayer.ts b/client/src/app/[site]/globe/hooks/useCoordinatesLayer.tsindex 3466f0d3d..ebe68f647 100644--- a/client/src/app/[site]/globe/hooks/useCoordinatesLayer.ts+++ b/client/src/app/[site]/globe/hooks/useCoordinatesLayer.ts@@ -3,7 +3,7 @@ import mapboxgl from "mapbox-gl";import { scaleSequentialSqrt } from "d3-scale";import { interpolateYlOrRd } from "d3-scale-chromatic";import { LiveSessionLocation } from "../../../../api/analytics/useGetSessionLocations";-import { addFilter } from "../../../../lib/store";+import { addFilter, useStore } from "../../../../lib/store";import { FilterParameter } from "@rybbit/shared/dist/filters";import { round } from "lodash";@@ -28,6 +28,7 @@ export function useCoordinatesLayer({mapView: string;}) {const popupRef = useRef<mapboxgl.Popup | null>(null);+ const filters = useStore(state => state.filters);useEffect(() => {if (!map.current || !liveSessionLocations || !mapLoaded) return;@@ -54,20 +55,41 @@ export function useCoordinatesLayer({const sizeMultiplier = getSizeMultiplier(totalLocations);+ // Get filtered lat/lon from filters+ const latFilter = filters.find(f => f.parameter === "lat");+ const lonFilter = filters.find(f => f.parameter === "lon");+ const filteredLat = latFilter?.value[0];+ const filteredLon = lonFilter?.value[0];+// Create GeoJSON points from live session locationsconst geojsonData: GeoJSON.FeatureCollection<GeoJSON.Point> = {type: "FeatureCollection",- features: liveSessionLocations.map(location => ({- type: "Feature",- geometry: {- type: "Point",- coordinates: [location.lon, location.lat],- },- properties: {- count: location.count,- city: location.city,- },- })),+ features: liveSessionLocations.map(location => {+ const roundedLat = round(location.lat, 4);+ const roundedLon = round(location.lon, 4);++ // Check if this location matches the filter with a small tolerance for floating point precision+ const isFiltered =+ filteredLat !== undefined &&+ filteredLon !== undefined &&+ Math.abs(roundedLat - Number(filteredLat)) < 0.00001 &&+ Math.abs(roundedLon - Number(filteredLon)) < 0.00001;++ return {+ type: "Feature",+ geometry: {+ type: "Point",+ coordinates: [roundedLon, roundedLat],+ },+ properties: {+ count: location.count,+ city: location.city,+ isFiltered,+ lat: roundedLat,+ lon: roundedLon,+ },+ };+ }),};// Add or update source@@ -111,11 +133,11 @@ export function useCoordinatesLayer({// 15,// ["interpolate", ["linear"], ["get", "count"], 1, 8 * sizeMultiplier, highest, 20 * sizeMultiplier],// ],- "circle-color": singleColor,- "circle-opacity": 0.7,- "circle-stroke-width": 1,- "circle-stroke-color": "#fff",- "circle-stroke-opacity": 0.3,+ "circle-color": ["case", ["get", "isFiltered"], "#3b82f6", singleColor],+ "circle-opacity": ["case", ["get", "isFiltered"], 0.9, 0.7],+ "circle-stroke-width": ["case", ["get", "isFiltered"], 2, 1],+ "circle-stroke-color": ["case", ["get", "isFiltered"], "#60a5fa", "#fff"],+ "circle-stroke-opacity": ["case", ["get", "isFiltered"], 1, 0.3],},layout: {visibility: mapView === "coordinates" ? "visible" : "none",@@ -162,18 +184,22 @@ export function useCoordinatesLayer({const city = feature.properties?.city || "Unknown";const count = feature.properties?.count || 0;- // Get the feature's coordinates- const coordinates = (feature.geometry as any).coordinates.slice().map((c: number) => round(c, 4));+ // Use the stored lat/lon properties instead of geometry coordinates+ // to avoid Mapbox coordinate transformations at different zoom levels+ const lat = feature.properties?.lat;+ const lon = feature.properties?.lon;++ if (lat === undefined || lon === undefined) return;addFilter({parameter: "lat" as FilterParameter,- value: [coordinates[1]],+ value: [lat],type: "equals",});addFilter({parameter: "lon" as FilterParameter,- value: [coordinates[0]],+ value: [lon],type: "equals",});});@@ -181,10 +207,39 @@ export function useCoordinatesLayer({// Update colors when data changesif (map.current.getLayer("realtime-coordinates-layer")) {- map.current.setPaintProperty("realtime-coordinates-layer", "circle-color", singleColor);+ map.current.setPaintProperty("realtime-coordinates-layer", "circle-color", [+ "case",+ ["get", "isFiltered"],+ "#3b82f6",+ singleColor,+ ]);+ map.current.setPaintProperty("realtime-coordinates-layer", "circle-opacity", [+ "case",+ ["get", "isFiltered"],+ 0.9,+ 0.7,+ ]);+ map.current.setPaintProperty("realtime-coordinates-layer", "circle-stroke-width", [+ "case",+ ["get", "isFiltered"],+ 2,+ 1,+ ]);+ map.current.setPaintProperty("realtime-coordinates-layer", "circle-stroke-color", [+ "case",+ ["get", "isFiltered"],+ "#60a5fa",+ "#fff",+ ]);+ map.current.setPaintProperty("realtime-coordinates-layer", "circle-stroke-opacity", [+ "case",+ ["get", "isFiltered"],+ 1,+ 0.3,+ ]);}};addCoordinatesLayer();- }, [liveSessionLocations, mapLoaded, map, minutes]);+ }, [liveSessionLocations, mapLoaded, map, minutes, filters]);}diff --git a/shared/src/filters.ts b/shared/src/filters.tsindex d7cb90eec..4185c9af7 100644--- a/shared/src/filters.ts+++ b/shared/src/filters.ts@@ -31,6 +31,6 @@ export type FilterParameter =export interface Filter {parameter: FilterParameter;- value: string[];+ value: (string | number)[];type: FilterType;}