2023-07-08 21:07:37 +02:00
|
|
|
import { useRouter } from 'next/router';
|
2023-04-14 07:28:29 +02:00
|
|
|
import FilterLink from 'components/common/FilterLink';
|
2023-07-08 21:07:37 +02:00
|
|
|
import { emptyFilter } from 'lib/filters';
|
2023-04-14 07:28:29 +02:00
|
|
|
import useLocale from 'hooks/useLocale';
|
|
|
|
import useMessages from 'hooks/useMessages';
|
2023-04-21 23:35:00 +02:00
|
|
|
import useCountryNames from 'hooks/useCountryNames';
|
2023-07-08 21:07:37 +02:00
|
|
|
import MetricsTable from './MetricsTable';
|
2023-04-14 07:28:29 +02:00
|
|
|
import regions from 'public/iso-3166-2.json';
|
|
|
|
|
2023-04-21 17:00:42 +02:00
|
|
|
export function RegionsTable({ websiteId, ...props }) {
|
2023-04-14 07:28:29 +02:00
|
|
|
const { locale } = useLocale();
|
|
|
|
const { formatMessage, labels } = useMessages();
|
2023-04-21 23:35:00 +02:00
|
|
|
const countryNames = useCountryNames(locale);
|
2023-07-08 21:07:37 +02:00
|
|
|
const { basePath } = useRouter();
|
2023-04-14 07:28:29 +02:00
|
|
|
|
2023-04-21 23:35:00 +02:00
|
|
|
const renderLabel = x => {
|
|
|
|
return regions[x] ? `${regions[x]}, ${countryNames[x.split('-')[0]]}` : x;
|
|
|
|
};
|
|
|
|
|
2023-04-24 04:52:44 +02:00
|
|
|
const renderLink = ({ x: code }) => {
|
2023-04-14 07:28:29 +02:00
|
|
|
return (
|
2023-04-24 04:52:44 +02:00
|
|
|
<FilterLink id="region" className={locale} value={code} label={renderLabel(code)}>
|
2023-07-08 21:07:37 +02:00
|
|
|
<img
|
|
|
|
src={`${basePath}/images/flags/${code?.split('-')?.[0]?.toLowerCase() || 'xx'}.png`}
|
|
|
|
alt={code}
|
|
|
|
/>
|
2023-04-24 04:52:44 +02:00
|
|
|
</FilterLink>
|
2023-04-14 07:28:29 +02:00
|
|
|
);
|
2023-04-21 23:35:00 +02:00
|
|
|
};
|
2023-04-14 07:28:29 +02:00
|
|
|
|
|
|
|
return (
|
|
|
|
<MetricsTable
|
|
|
|
{...props}
|
|
|
|
title={formatMessage(labels.regions)}
|
|
|
|
type="region"
|
|
|
|
metric={formatMessage(labels.visitors)}
|
|
|
|
websiteId={websiteId}
|
|
|
|
dataFilter={emptyFilter}
|
|
|
|
renderLabel={renderLink}
|
|
|
|
/>
|
|
|
|
);
|
|
|
|
}
|
2023-04-21 17:00:42 +02:00
|
|
|
|
|
|
|
export default RegionsTable;
|