umami/src/components/hooks/useFormat.ts

47 lines
1.3 KiB
TypeScript
Raw Normal View History

import useMessages from './useMessages';
import { BROWSERS } from 'lib/constants';
import useLocale from './useLocale';
import useCountryNames from './useCountryNames';
2023-09-20 23:36:18 +02:00
import regions from 'public/iso-3166-2.json';
export function useFormat() {
const { formatMessage, labels } = useMessages();
const { locale } = useLocale();
const countryNames = useCountryNames(locale);
2023-12-02 01:02:50 +01:00
const formatBrowser = (value: string): string => {
return BROWSERS[value] || value;
};
2023-12-02 01:02:50 +01:00
const formatCountry = (value: string): string => {
return countryNames[value] || value;
};
2023-12-02 01:02:50 +01:00
const formatRegion = (value: string): string => {
2023-09-20 23:36:18 +02:00
return regions[value] ? regions[value] : value;
};
2023-12-02 01:02:50 +01:00
const formatDevice = (value: string): string => {
return formatMessage(labels[value] || labels.unknown);
};
2023-12-02 01:02:50 +01:00
const formatValue = (value: string, type: string): string => {
switch (type) {
case 'browser':
return formatBrowser(value);
case 'country':
return formatCountry(value);
2023-09-20 23:36:18 +02:00
case 'region':
return formatRegion(value);
case 'device':
return formatDevice(value);
default:
return value;
}
};
2023-09-20 23:36:18 +02:00
return { formatBrowser, formatCountry, formatRegion, formatDevice, formatValue };
}
export default useFormat;