Skip to content

Commit f9f2781

Browse files
authored
Use LocationData type in MapComponent props type. (#42)
* Refactor types on map page to a common file. This helps in using them later from other places. * Use LocationData type in MapComponent props type. * Fix logic issue when getting minimum value. The current logic will give minimum value as Infinity incase the array has all values as 0. * Rename shared metric types.
1 parent 7b46188 commit f9f2781

File tree

4 files changed

+37
-34
lines changed

4 files changed

+37
-34
lines changed

apps/dashboard/app/(main)/websites/[id]/map/page.tsx

Lines changed: 1 addition & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -11,26 +11,9 @@ import { Skeleton } from '@/components/ui/skeleton';
1111
import { Tabs, TabsList, TabsTrigger } from '@/components/ui/tabs';
1212
import { useMapLocationData } from '@/hooks/use-dynamic-query';
1313
import { cn } from '@/lib/utils';
14+
import type { LocationData } from '@databuddy/shared';
1415
import { WebsitePageHeader } from '../_components/website-page-header';
1516

16-
interface CountryData {
17-
country: string;
18-
country_code?: string;
19-
visitors: number;
20-
pageviews: number;
21-
}
22-
23-
interface RegionData {
24-
country: string;
25-
visitors: number;
26-
pageviews: number;
27-
}
28-
29-
interface LocationData {
30-
countries: CountryData[];
31-
regions: RegionData[];
32-
}
33-
3417
const MapComponent = dynamic(
3518
() =>
3619
import('@/components/analytics/map-component').then((mod) => ({

apps/dashboard/components/analytics/map-component.tsx

Lines changed: 11 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ import { useEffect, useMemo, useRef, useState } from 'react';
88
import { GeoJSON, MapContainer } from 'react-leaflet';
99
import { getCountryPopulation } from '@/lib/data';
1010
import { useCountries } from '@/lib/geo';
11+
import type { LocationData } from '@databuddy/shared';
1112
import { CountryFlag } from './icons/CountryFlag';
1213

1314
interface TooltipContent {
@@ -35,7 +36,7 @@ export function MapComponent({
3536
}: {
3637
height: string;
3738
mode?: 'total' | 'perCapita';
38-
locationData?: any;
39+
locationData?: LocationData;
3940
isLoading?: boolean;
4041
}) {
4142
const locationsData = locationData;
@@ -46,14 +47,14 @@ export function MapComponent({
4647
}
4748

4849
const validCountries = locationsData.countries.filter(
49-
(country: any) => country.country && country.country.trim() !== ''
50+
(country) => country.country && country.country.trim() !== ''
5051
);
5152

5253
const totalVisitors =
53-
validCountries.reduce((sum: number, c: any) => sum + c.visitors, 0) || 1;
54+
validCountries.reduce((sum, c) => sum + c.visitors, 0) || 1;
5455

5556
return {
56-
data: validCountries.map((country: any) => ({
57+
data: validCountries.map((country) => ({
5758
value:
5859
country.country_code?.toUpperCase() || country.country.toUpperCase(),
5960
count: country.visitors,
@@ -85,7 +86,7 @@ export function MapComponent({
8586
return null;
8687
}
8788

88-
return countryData.data.map((item: any) => {
89+
return countryData.data.map((item) => {
8990
const population = getCountryPopulation(item.value);
9091
const perCapitaValue = population > 0 ? item.count / population : 0;
9192
return {
@@ -101,9 +102,10 @@ export function MapComponent({
101102
}
102103

103104
const metricToUse = mode === 'perCapita' ? 'perCapita' : 'count';
104-
const values = processedCountryData?.map((d: any) => d[metricToUse]) || [0];
105+
const values = processedCountryData?.map((d) => d[metricToUse]) || [0];
105106
const maxValue = Math.max(...values);
106-
const minValue = Math.min(...values.filter((v: number) => v > 0));
107+
const nonZeroValues = values.filter((v) => v > 0);
108+
const minValue = nonZeroValues.length > 0 ? Math.min(...nonZeroValues) : 0;
107109

108110
const baseBlue = '59, 130, 246';
109111
const lightBlue = '147, 197, 253';
@@ -135,7 +137,7 @@ export function MapComponent({
135137
const handleStyle = (feature: Feature<any>) => {
136138
const dataKey = feature?.properties?.ISO_A2;
137139
const foundData = processedCountryData?.find(
138-
({ value }: any) => value === dataKey
140+
({ value }) => value === dataKey
139141
);
140142

141143
const metricValue =
@@ -177,7 +179,7 @@ export function MapComponent({
177179

178180
const name = feature.properties?.ADMIN;
179181
const foundData = processedCountryData?.find(
180-
({ value }: any) => value === code
182+
({ value }) => value === code
181183
);
182184
const count = foundData?.count || 0;
183185
const percentage = foundData?.percentage || 0;

packages/shared/src/types/metrics.ts

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -7,19 +7,19 @@ export interface BaseMetricData {
77
pageviews: number;
88
}
99

10-
export interface DeviceTypeData extends BaseMetricData {}
10+
export interface DeviceTypeMetricData extends BaseMetricData {}
1111

12-
export interface BrowserData extends BaseMetricData {}
12+
export interface BrowserMetricData extends BaseMetricData {}
1313

14-
export interface CountryData extends BaseMetricData {}
14+
export interface CountryMetricData extends BaseMetricData {}
1515

16-
export interface RegionData extends BaseMetricData {}
16+
export interface RegionMetricData extends BaseMetricData {}
1717

18-
export interface PageData extends BaseMetricData {}
18+
export interface PageMetricData extends BaseMetricData {}
1919

20-
export interface ReferrerData extends BaseMetricData {}
20+
export interface ReferrerMetricData extends BaseMetricData {}
2121

22-
export interface PerformanceData extends BaseMetricData {
22+
export interface PerformanceMetricData extends BaseMetricData {
2323
sessions: number;
2424
avg_load_time: number;
2525
avg_ttfb?: number;

packages/shared/src/types/website.ts

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -46,3 +46,21 @@ export interface WebsitesApiResponse {
4646
data?: Website[];
4747
error?: string;
4848
}
49+
50+
export interface CountryData {
51+
country: string;
52+
country_code?: string;
53+
visitors: number;
54+
pageviews: number;
55+
}
56+
57+
export interface RegionData {
58+
country: string;
59+
visitors: number;
60+
pageviews: number;
61+
}
62+
63+
export interface LocationData {
64+
countries: CountryData[];
65+
regions: RegionData[];
66+
}

0 commit comments

Comments
 (0)