diff options
| author | Fuwn <[email protected]> | 2026-01-24 13:09:50 +0000 |
|---|---|---|
| committer | Fuwn <[email protected]> | 2026-01-24 13:09:50 +0000 |
| commit | 396acf3bbbe00a192cb0ea0a9ccf91b1d8d2850b (patch) | |
| tree | b9df4ca6a70db45cfffbae6fdd7252e20fb8e93c /src/components/hooks/useFormat.ts | |
| download | umami-main.tar.xz umami-main.zip | |
Created from https://vercel.com/new
Diffstat (limited to 'src/components/hooks/useFormat.ts')
| -rw-r--r-- | src/components/hooks/useFormat.ts | 74 |
1 files changed, 74 insertions, 0 deletions
diff --git a/src/components/hooks/useFormat.ts b/src/components/hooks/useFormat.ts new file mode 100644 index 0000000..896fa07 --- /dev/null +++ b/src/components/hooks/useFormat.ts @@ -0,0 +1,74 @@ +import { BROWSERS, OS_NAMES } from '@/lib/constants'; +import regions from '../../../public/iso-3166-2.json'; +import { useCountryNames } from './useCountryNames'; +import { useLanguageNames } from './useLanguageNames'; +import { useLocale } from './useLocale'; +import { useMessages } from './useMessages'; + +export function useFormat() { + const { formatMessage, labels } = useMessages(); + const { locale } = useLocale(); + const { countryNames } = useCountryNames(locale); + const { languageNames } = useLanguageNames(locale); + + const formatOS = (value: string): string => { + return OS_NAMES[value] || value; + }; + + const formatBrowser = (value: string): string => { + return BROWSERS[value] || value; + }; + + const formatDevice = (value: string): string => { + return formatMessage(labels[value] || labels.unknown); + }; + + const formatCountry = (value: string): string => { + return countryNames[value] || value; + }; + + const formatRegion = (value?: string): string => { + const [country] = value?.split('-') || []; + return regions[value] ? `${regions[value]}, ${countryNames[country]}` : value; + }; + + const formatCity = (value: string, country?: string): string => { + return countryNames[country] ? `${value}, ${countryNames[country]}` : value; + }; + + const formatLanguage = (value: string): string => { + return languageNames[value?.split('-')[0]] || value; + }; + + const formatValue = (value: string, type: string, data?: Record<string, any>): string => { + switch (type) { + case 'os': + return formatOS(value); + case 'browser': + return formatBrowser(value); + case 'device': + return formatDevice(value); + case 'country': + return formatCountry(value); + case 'region': + return formatRegion(value); + case 'city': + return formatCity(value, data?.country); + case 'language': + return formatLanguage(value); + default: + return typeof value === 'string' ? value : undefined; + } + }; + + return { + formatOS, + formatBrowser, + formatDevice, + formatCountry, + formatRegion, + formatCity, + formatLanguage, + formatValue, + }; +} |