umami/src/components/metrics/CitiesTable.js

47 lines
1.3 KiB
JavaScript
Raw Normal View History

2023-08-26 19:46:27 +00:00
import { useRouter } from 'next/router';
2023-04-14 05:28:29 +00:00
import MetricsTable from './MetricsTable';
import { emptyFilter } from 'lib/filters';
import FilterLink from 'components/common/FilterLink';
import useLocale from 'components/hooks/useLocale';
import useMessages from 'components/hooks/useMessages';
2023-08-26 19:46:27 +00:00
import useCountryNames from 'components/hooks/useCountryNames';
2023-04-14 05:28:29 +00:00
2023-04-21 15:00:42 +00:00
export function CitiesTable({ websiteId, ...props }) {
2023-04-14 05:28:29 +00:00
const { locale } = useLocale();
const { formatMessage, labels } = useMessages();
2023-08-26 19:46:27 +00:00
const { basePath } = useRouter();
const countryNames = useCountryNames(locale);
2023-04-14 05:28:29 +00:00
2023-08-26 19:46:27 +00:00
const renderLabel = (city, country) => {
const name = countryNames[country];
return name ? `${city}, ${name}` : city;
};
const renderLink = ({ x: city, country }) => {
2023-04-14 05:28:29 +00:00
return (
2023-08-26 19:46:27 +00:00
<FilterLink id="city" value={city} label={renderLabel(city, country)}>
{country && (
<img
src={`${basePath}/images/flags/${country?.toLowerCase() || 'xx'}.png`}
2023-08-26 19:46:27 +00:00
alt={country}
/>
)}
</FilterLink>
2023-04-14 05:28:29 +00:00
);
2023-08-26 19:46:27 +00:00
};
2023-04-14 05:28:29 +00:00
return (
<MetricsTable
{...props}
title={formatMessage(labels.cities)}
type="city"
metric={formatMessage(labels.visitors)}
websiteId={websiteId}
dataFilter={emptyFilter}
renderLabel={renderLink}
/>
);
}
2023-04-21 15:00:42 +00:00
export default CitiesTable;