Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@ export function RealtimePage({ websiteId }: { websiteId: string }) {
<RealtimeCountries data={countries} />
</Panel>
<Panel gridColumn={isMobile ? null : 'span 2'} padding="0">
<WorldMap data={countries} />
<WorldMap data={countries} allowFilter={false} />
</Panel>
</GridRow>
</Grid>
Expand Down
29 changes: 25 additions & 4 deletions src/components/metrics/WorldMap.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import {
useCountryNames,
useLocale,
useMessages,
useNavigation,
useWebsiteMetricsQuery,
} from '@/components/hooks';
import { getThemeColors } from '@/lib/colors';
Expand All @@ -16,17 +17,20 @@ import { formatLongNumber } from '@/lib/format';
export interface WorldMapProps extends ColumnProps {
websiteId?: string;
data?: any[];
allowFilter?: boolean;
}

export function WorldMap({ websiteId, data, ...props }: WorldMapProps) {
export function WorldMap({ websiteId, data, allowFilter = true, ...props }: WorldMapProps) {
const [tooltip, setTooltipPopup] = useState();
const { theme } = useTheme();
const { colors } = getThemeColors(theme);
const { locale } = useLocale();
const { formatMessage, labels } = useMessages();
const { countryNames } = useCountryNames(locale);
const { router, updateParams, query } = useNavigation();
const visitorsLabel = formatMessage(labels.visitors).toLocaleLowerCase(locale);
const unknownLabel = formatMessage(labels.unknown);
const selectedCountry = allowFilter ? query.country?.replace(/^eq\./, '') : undefined;

const { data: mapData } = useWebsiteMetricsQuery(websiteId, {
type: 'country',
Expand All @@ -39,6 +43,11 @@ export function WorldMap({ websiteId, data, ...props }: WorldMapProps) {

const getFillColor = (code: string) => {
if (code === 'AQ') return;

if (code && code === selectedCountry) {
return colors.map.hoverColor;
}

const country = metrics?.find(({ x }) => x === code);

if (!country) {
Expand All @@ -54,6 +63,16 @@ export function WorldMap({ websiteId, data, ...props }: WorldMapProps) {
return code === 'AQ' ? 0 : 1;
};

const isClickable = (code: string) => {
return allowFilter && !!code && code !== 'AQ';
};

const handleClick = (code: string) => {
if (!isClickable(code)) return;

router.replace(updateParams({ country: code === selectedCountry ? undefined : `eq.${code}` }));
};

const handleHover = (code: string) => {
if (code === 'AQ') return;
const country = metrics?.find(({ x }) => x === code);
Expand All @@ -77,6 +96,7 @@ export function WorldMap({ websiteId, data, ...props }: WorldMapProps) {
{({ geographies }) => {
return geographies.map(geo => {
const code = ISO_COUNTRIES[geo.id];
const cursor = isClickable(code) ? 'pointer' : 'default';

return (
<Geography
Expand All @@ -86,12 +106,13 @@ export function WorldMap({ websiteId, data, ...props }: WorldMapProps) {
stroke={colors.map.strokeColor}
opacity={getOpacity(code)}
style={{
default: { outline: 'none' },
hover: { outline: 'none', fill: colors.map.hoverColor },
pressed: { outline: 'none' },
default: { outline: 'none', cursor },
hover: { outline: 'none', fill: colors.map.hoverColor, cursor },
pressed: { outline: 'none', cursor },
}}
onMouseOver={() => handleHover(code)}
onMouseOut={() => setTooltipPopup(null)}
onClick={() => handleClick(code)}
/>
);
});
Expand Down