mirror of
https://github.com/kremalicious/umami.git
synced 2024-06-28 16:57:52 +02:00
47 lines
1.2 KiB
TypeScript
47 lines
1.2 KiB
TypeScript
import useMessages from './useMessages';
|
|
import { BROWSERS } from 'lib/constants';
|
|
import useLocale from './useLocale';
|
|
import useCountryNames from './useCountryNames';
|
|
import regions from 'public/iso-3166-2.json';
|
|
|
|
export function useFormat() {
|
|
const { formatMessage, labels } = useMessages();
|
|
const { locale } = useLocale();
|
|
const countryNames = useCountryNames(locale);
|
|
|
|
const formatBrowser = (value: string) => {
|
|
return BROWSERS[value] || value;
|
|
};
|
|
|
|
const formatCountry = (value: string) => {
|
|
return countryNames[value] || value;
|
|
};
|
|
|
|
const formatRegion = (value: string) => {
|
|
return regions[value] ? regions[value] : value;
|
|
};
|
|
|
|
const formatDevice = (value: string) => {
|
|
return formatMessage(labels[value] || labels.unknown);
|
|
};
|
|
|
|
const formatValue = (value: string, type: string) => {
|
|
switch (type) {
|
|
case 'browser':
|
|
return formatBrowser(value);
|
|
case 'country':
|
|
return formatCountry(value);
|
|
case 'region':
|
|
return formatRegion(value);
|
|
case 'device':
|
|
return formatDevice(value);
|
|
default:
|
|
return value;
|
|
}
|
|
};
|
|
|
|
return { formatBrowser, formatCountry, formatRegion, formatDevice, formatValue };
|
|
}
|
|
|
|
export default useFormat;
|