Skip to content

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

Implement geo data hooks and enhance Countries and Map components

Bill Yangd63a832d9159
diff --git a/client/src/app/[site]/main/components/sections/Countries.tsx b/client/src/app/[site]/main/components/sections/Countries.tsx
index d791cd705..3fa0d67c2 100644
--- a/client/src/app/[site]/main/components/sections/Countries.tsx
+++ b/client/src/app/[site]/main/components/sections/Countries.tsx
@@ -1,5 +1,5 @@
"use client";
-import { Globe } from "lucide-react";
+import { ChevronRight, Globe } from "lucide-react";
import { useState } from "react";
import {
Tabs,
@@ -11,6 +11,7 @@ import { Card, CardContent } from "../../../../../components/ui/card";
import { getCountryName } from "../../../../../lib/utils";
import { StandardSection } from "../../../components/shared/StandardSection/StandardSection";
import { CountryFlag } from "../../../components/shared/icons/CountryFlag";
+import { useSubdivisions } from "../../../../../lib/geo";
type Tab = "countries" | "regions" | "languages";
@@ -47,6 +48,9 @@ const getCountryFromLanguage = (languageCode: string): string | null => {
export function Countries() {
const [tab, setTab] = useState<Tab>("countries");
+
+ const { data: subdivisions } = useSubdivisions();
+
return (
<Card className="">
<CardContent className="mt-2">
@@ -66,15 +70,45 @@ export function Countries() {
title="Countries"
getValue={(e) => e.value}
getKey={(e) => e.value}
- getLabel={(e) => (
- <div className="flex gap-2 items-center">
- <CountryFlag country={e.value} />
- {getCountryName(e.value)}
- </div>
- )}
+ getLabel={(e) => {
+ console.info(e.value);
+ return (
+ <div className="flex gap-2 items-center">
+ <CountryFlag country={e.value} />
+ {getCountryName(e.value)}
+ </div>
+ );
+ }}
+ />
+ </TabsContent>
+ <TabsContent value="regions">
+ <StandardSection
+ filterParameter="iso_3166_2"
+ title="Regions"
+ getValue={(e) => e.value}
+ getKey={(e) => e.value}
+ getLabel={(e) => {
+ if (!e.value) {
+ return "Unknown";
+ }
+
+ const region = subdivisions?.features.find(
+ (feature) => feature.properties.iso_3166_2 === e.value
+ )?.properties;
+
+ return (
+ <div className="flex gap-2 items-center">
+ <CountryFlag
+ country={region?.iso_3166_2.slice(0, 2) ?? ""}
+ />
+ {region?.iso_3166_2.slice(0, 2)}
+ <ChevronRight className="w-4 h-4 mx-[-4px]" />
+ {region?.name}
+ </div>
+ );
+ }}
/>
</TabsContent>
- <TabsContent value="regions"></TabsContent>
<TabsContent value="languages">
<StandardSection
filterParameter="language"
diff --git a/client/src/app/[site]/main/components/sections/Map.tsx b/client/src/app/[site]/main/components/sections/Map.tsx
index 8ba122d4c..e9d9ca9df 100644
--- a/client/src/app/[site]/main/components/sections/Map.tsx
+++ b/client/src/app/[site]/main/components/sections/Map.tsx
@@ -16,9 +16,7 @@ import { Feature, GeoJsonObject } from "geojson";
import "leaflet/dist/leaflet.css";
import { useSingleCol } from "@/api/analytics/useSingleCol";
import { addFilter, FilterParameter } from "../../../../../lib/store";
-
-const countriesGeoUrl = "/countries.json";
-const subdivisionsGeoUrl = "/subdivisions.json";
+import { useCountries, useSubdivisions } from "../../../../../lib/geo";
interface TooltipContent {
name: string;
@@ -86,19 +84,8 @@ export function Map() {
.range([startColor, endColor]);
}, [countryData?.data, subdivisionData?.data, mapView]);
- const [countriesGeoData, setCountriesGeoData] =
- useState<GeoJsonObject | null>(null);
- const [subdivisionsGeoData, setSubdivisionsGeoData] =
- useState<GeoJsonObject | null>(null);
-
- useEffect(() => {
- fetch(countriesGeoUrl)
- .then((res) => res.json())
- .then(setCountriesGeoData);
- fetch(subdivisionsGeoUrl)
- .then((res) => res.json())
- .then(setSubdivisionsGeoData);
- }, []);
+ const { data: subdivisionsGeoData } = useSubdivisions();
+ const { data: countriesGeoData } = useCountries();
const handleStyle = (feature: Feature | undefined) => {
const borderColors = [
@@ -224,7 +211,7 @@ export function Map() {
{mapView === "countries" && countriesGeoData && (
<GeoJSON
key={`countries-${dataVersion}`}
- data={countriesGeoData}
+ data={countriesGeoData as GeoJsonObject}
style={handleStyle}
onEachFeature={handleEachFeature}
/>
@@ -232,7 +219,7 @@ export function Map() {
{mapView === "subdivisions" && subdivisionsGeoData && (
<GeoJSON
key={`subdivisions-${dataVersion}`}
- data={subdivisionsGeoData}
+ data={subdivisionsGeoData as GeoJsonObject}
style={handleStyle}
onEachFeature={handleEachFeature}
/>
diff --git a/client/src/lib/geo.ts b/client/src/lib/geo.ts
new file mode 100644
index 000000000..972201d2a
--- /dev/null
+++ b/client/src/lib/geo.ts
@@ -0,0 +1,52 @@
+import { useQuery } from "@tanstack/react-query";
+
+const countriesGeoUrl = "/countries.json";
+const subdivisionsGeoUrl = "/subdivisions.json";
+
+export type Subdivisions = {
+ type: string;
+ features: Array<{
+ type: string;
+ properties: {
+ name: string;
+ iso_3166_2: string;
+ admin: string;
+ border: number;
+ };
+ geometry: {
+ type: string;
+ coordinates: Array<Array<Array<any>>>;
+ };
+ }>;
+};
+
+export type Country = {
+ type: string;
+ features: Array<{
+ type: string;
+ properties: {
+ ISO_A2: string;
+ ADMIN: string;
+ ISO_A3: string;
+ BORDER: number;
+ };
+ geometry: {
+ type: string;
+ coordinates: Array<Array<Array<any>>>;
+ };
+ }>;
+};
+
+export const useSubdivisions = () => {
+ return useQuery<Subdivisions>({
+ queryKey: ["subdivisions"],
+ queryFn: () => fetch(subdivisionsGeoUrl).then((res) => res.json()),
+ });
+};
+
+export const useCountries = () => {
+ return useQuery<Country>({
+ queryKey: ["countries"],
+ queryFn: () => fetch(countriesGeoUrl).then((res) => res.json()),
+ });
+};