Skip to content

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

Enhance MapComponent and MapPage for improved functionality and user experience

Bill Yangd769b9229428
diff --git a/client/src/app/[site]/components/shared/Map.tsx b/client/src/app/[site]/components/shared/Map.tsx
index 87145bf35..843108d12 100644
--- a/client/src/app/[site]/components/shared/Map.tsx
+++ b/client/src/app/[site]/components/shared/Map.tsx
@@ -31,9 +31,11 @@ interface TooltipPosition {
export function MapComponent({
height,
mode = "total",
+ mapView: controlledMapView,
}: {
height: string;
mode?: "total" | "perCapita";
+ mapView?: "countries" | "subdivisions";
}) {
const {
data: countryData,
@@ -59,16 +61,15 @@ export function MapComponent({
}
}, [countryData, subdivisionData]);
- const [tooltipContent, setTooltipContent] = useState<TooltipContent | null>(
- null
- );
+ const [tooltipContent, setTooltipContent] = useState<TooltipContent | null>(null);
const [tooltipPosition, setTooltipPosition] = useState<TooltipPosition>({
x: 0,
y: 0,
});
- const [mapView, setMapView] = useState<"countries" | "subdivisions">(
- "countries"
- );
+ const [internalMapView, setInternalMapView] = useState<"countries" | "subdivisions">("countries");
+
+ // Use controlled value if provided, otherwise use internal state
+ const mapView = controlledMapView ?? internalMapView;
// Track which feature is currently hovered to control opacity without conflicts
const [hoveredId, setHoveredId] = useState<string | null>(null);
@@ -95,8 +96,7 @@ export function MapComponent({
const countryCode = item.value?.split("-")[0];
const population = getCountryPopulation(countryCode);
// For subdivisions, we divide by 10x the population since regions are smaller
- const perCapitaValue =
- population > 0 ? item.count / (population / 10) : 0;
+ const perCapitaValue = population > 0 ? item.count / (population / 10) : 0;
return {
...item,
perCapita: perCapitaValue,
@@ -106,18 +106,14 @@ export function MapComponent({
const colorScale = useMemo(() => {
if (mapView === "countries" && !processedCountryData) return () => "#eee";
- if (mapView === "subdivisions" && !processedSubdivisionData)
- return () => "#eee";
+ if (mapView === "subdivisions" && !processedSubdivisionData) return () => "#eee";
- const dataToUse =
- mapView === "countries" ? processedCountryData : processedSubdivisionData;
+ const dataToUse = mapView === "countries" ? processedCountryData : processedSubdivisionData;
// Get computed values from CSS variables
const getComputedColor = (cssVar: string) => {
// Get the HSL values from CSS
- const hslValues = getComputedStyle(document.documentElement)
- .getPropertyValue(cssVar)
- .trim();
+ const hslValues = getComputedStyle(document.documentElement).getPropertyValue(cssVar).trim();
return `hsl(${hslValues})`;
};
@@ -146,34 +142,20 @@ export function MapComponent({
const { data: countriesGeoData } = useCountries();
const handleStyle = (feature: Feature | undefined) => {
- const borderColors = [
- "#377eb8",
- "#ff7f00",
- "#4daf4a",
- "#1abc9c",
- "#984ea3",
- ];
+ const borderColors = ["#377eb8", "#ff7f00", "#4daf4a", "#1abc9c", "#984ea3"];
const isCountryView = mapView === "countries";
- const dataKey = isCountryView
- ? feature?.properties?.["ISO_A2"]
- : feature?.properties?.["iso_3166_2"];
- const borderKey = isCountryView
- ? feature?.properties?.["BORDER"]
- : feature?.properties?.["border"];
+ const dataKey = isCountryView ? feature?.properties?.["ISO_A2"] : feature?.properties?.["iso_3166_2"];
+ const borderKey = isCountryView ? feature?.properties?.["BORDER"] : feature?.properties?.["border"];
// Use the processed data that includes per capita metrics
- const dataToUse = isCountryView
- ? processedCountryData
- : processedSubdivisionData;
+ const dataToUse = isCountryView ? processedCountryData : processedSubdivisionData;
const foundData = dataToUse?.find(({ value }: any) => value === dataKey);
// Use count or per capita value based on mode
- const metricValue =
- mode === "perCapita" ? foundData?.perCapita || 0 : foundData?.count || 0;
+ const metricValue = mode === "perCapita" ? foundData?.perCapita || 0 : foundData?.count || 0;
- const color =
- metricValue > 0 ? colorScale(metricValue) : "rgba(140, 140, 140, 0.5)";
+ const color = metricValue > 0 ? colorScale(metricValue) : "rgba(140, 140, 140, 0.5)";
return {
color: color,
@@ -189,21 +171,15 @@ export function MapComponent({
layer.on({
mouseover: () => {
const isCountryView = mapView === "countries";
- const code = isCountryView
- ? feature.properties?.["ISO_A2"]
- : feature.properties?.["iso_3166_2"];
+ const code = isCountryView ? feature.properties?.["ISO_A2"] : feature.properties?.["iso_3166_2"];
// Mark this feature as hovered so handleStyle increases opacity
setHoveredId(code);
- const name = isCountryView
- ? feature.properties?.["ADMIN"]
- : feature.properties?.["name"];
+ const name = isCountryView ? feature.properties?.["ADMIN"] : feature.properties?.["name"];
// Use the processed data that includes per capita metrics
- const dataToUse = isCountryView
- ? processedCountryData
- : processedSubdivisionData;
+ const dataToUse = isCountryView ? processedCountryData : processedSubdivisionData;
const foundData = dataToUse?.find(({ value }: any) => value === code);
const count = foundData?.count || 0;
@@ -225,14 +201,10 @@ export function MapComponent({
},
click: () => {
const isCountryView = mapView === "countries";
- const code = isCountryView
- ? feature.properties?.["ISO_A2"]
- : feature.properties?.["iso_3166_2"];
+ const code = isCountryView ? feature.properties?.["ISO_A2"] : feature.properties?.["iso_3166_2"];
// Set filter based on the current map view
- const filterParameter: FilterParameter = isCountryView
- ? "country"
- : "region";
+ const filterParameter: FilterParameter = isCountryView ? "country" : "region";
addFilter({
parameter: filterParameter,
@@ -247,18 +219,14 @@ export function MapComponent({
const map = useMapEvent("zoomend", () => {
const newMapView = map.getZoom() >= 5 ? "subdivisions" : "countries";
if (newMapView !== mapView) {
- setMapView(newMapView);
+ setInternalMapView(newMapView);
setTooltipContent(null);
}
});
return null;
};
- const isLoading =
- isCountryLoading ||
- isSubdivisionLoading ||
- isCountryFetching ||
- isSubdivisionFetching;
+ const isLoading = isCountryLoading || isSubdivisionLoading || isCountryFetching || isSubdivisionFetching;
const [ref, { height: resolvedHeight }] = useMeasure();
@@ -283,9 +251,7 @@ export function MapComponent({
<div className="absolute inset-0 bg-neutral-900/30 backdrop-blur-sm z-10 flex items-center justify-center">
<div className="flex flex-col items-center gap-2">
<div className="h-8 w-8 rounded-full border-2 border-accent-400 border-t-transparent animate-spin"></div>
- <span className="text-sm text-neutral-300">
- Loading map data...
- </span>
+ <span className="text-sm text-neutral-300">Loading map data...</span>
</div>
</div>
)}
@@ -334,25 +300,17 @@ export function MapComponent({
}}
>
<div className="font-sm flex items-center gap-1">
- {tooltipContent.code && (
- <CountryFlag country={tooltipContent.code.slice(0, 2)} />
- )}
+ {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>
+ <span className="font-bold text-accent-400">{tooltipContent.count.toLocaleString()}</span>{" "}
+ <span className="text-neutral-300">({tooltipContent.percentage.toFixed(1)}%) sessions</span>
</div>
{mode === "perCapita" && mapView === "countries" && (
<div className="text-sm text-neutral-300">
- <span className="font-bold text-accent-400">
- {round(tooltipContent.perCapita ?? 0, 2)}
- </span>{" "}
- per million people
+ <span className="font-bold text-accent-400">{round(tooltipContent.perCapita ?? 0, 2)}</span> per million
+ people
</div>
)}
</div>
diff --git a/client/src/app/[site]/map/page.tsx b/client/src/app/[site]/map/page.tsx
index ba6c06b05..4d89487db 100644
--- a/client/src/app/[site]/map/page.tsx
+++ b/client/src/app/[site]/map/page.tsx
@@ -12,15 +12,25 @@ export default function MapPage() {
useSetPageTitle("Rybbit · Map");
const [mapMode, setMapMode] = useState<"total" | "perCapita">("total");
+ const [mapView, setMapView] = useState<"countries" | "subdivisions">("countries");
return (
<DisabledOverlay message="Map" featurePath="map">
- <div className="relative w-full h-[calc(100vh-45px)]">
+ <div className="relative w-full h-screen">
<div className="p-2 md:p-4 relative z-50">
<SubHeader />
</div>
- <div className="absolute bottom-2 md:bottom-4 left-2 md:left-4 z-50">
- <div className="flex items-center gap-3 bg-neutral-900 rounded-md px-3 py-2 text-sm">
+ <div className="absolute bottom-2 md:bottom-4 left-2 md:left-4 z-50 bg-neutral-900 rounded-md">
+ <div className="flex items-center gap-3 px-3 py-2 text-sm">
+ <span className={cn(mapView === "countries" ? "text-white" : "text-neutral-400")}>Countries</span>
+ <Switch
+ checked={mapView === "subdivisions"}
+ onCheckedChange={(checked) => setMapView(checked ? "subdivisions" : "countries")}
+ className="data-[state=checked]:bg-accent-400"
+ />
+ <span className={cn(mapView === "subdivisions" ? "text-white" : "text-neutral-400")}>Subdivisions</span>
+ </div>
+ <div className="flex items-center gap-3 px-3 py-2 text-sm">
<span className={cn(mapMode === "total" ? "text-white" : "text-neutral-400")}>Total Visits</span>
<Switch
checked={mapMode === "perCapita"}
@@ -31,7 +41,7 @@ export default function MapPage() {
</div>
</div>
<div className="absolute top-0 left-0 right-0 bottom-0 z-10">
- <MapComponent height="calc(100vh - 45px - 40px)" mode={mapMode} />
+ <MapComponent height="calc(100vh - 40px)" mode={mapMode} mapView={mapView} />
</div>
</div>
</DisabledOverlay>
diff --git a/client/src/components/ui/switch.tsx b/client/src/components/ui/switch.tsx
index e652c810d..f423a194b 100644
--- a/client/src/components/ui/switch.tsx
+++ b/client/src/components/ui/switch.tsx
@@ -11,7 +11,7 @@ const Switch = React.forwardRef<
>(({ className, ...props }, ref) => (
<SwitchPrimitives.Root
className={cn(
- "peer inline-flex h-5 w-9 shrink-0 cursor-pointer items-center rounded-full border-2 border-transparent shadow-sm transition-colors focus-visible:outline-none focus-visible:ring-2 focus-visible:ring-neutral-950 focus-visible:ring-offset-2 focus-visible:ring-offset-white disabled:cursor-not-allowed disabled:opacity-50 data-[state=checked]:bg-accent-400 data-[state=unchecked]:bg-neutral-200 dark:focus-visible:ring-neutral-300 dark:focus-visible:ring-offset-neutral-950 dark:data-[state=checked]:bg-accent-400 dark:data-[state=unchecked]:bg-neutral-800",
+ "peer inline-flex h-5 w-9 shrink-0 cursor-pointer items-center rounded-full border-2 border-transparent shadow-sm transition-colors focus-visible:outline-none focus-visible:ring-2 focus-visible:ring-neutral-950 focus-visible:ring-offset-2 focus-visible:ring-offset-white disabled:cursor-not-allowed disabled:opacity-50 data-[state=checked]:bg-accent-500 data-[state=unchecked]:bg-neutral-200 dark:focus-visible:ring-neutral-300 dark:focus-visible:ring-offset-neutral-950 dark:data-[state=checked]:bg-accent-500 dark:data-[state=unchecked]:bg-neutral-800",
className
)}
{...props}
@@ -19,7 +19,7 @@ const Switch = React.forwardRef<
>
<SwitchPrimitives.Thumb
className={cn(
- "pointer-events-none block h-4 w-4 rounded-full bg-white shadow-lg ring-0 transition-transform data-[state=checked]:translate-x-4 data-[state=unchecked]:translate-x-0 dark:bg-neutral-950"
+ "pointer-events-none block h-4 w-4 rounded-full bg-white shadow-lg ring-0 transition-transform data-[state=checked]:translate-x-4 data-[state=unchecked]:translate-x-0 dark:bg-white"
)}
/>
</SwitchPrimitives.Root>