Files
AstralRinth/packages/ui/src/composables/how-ago.ts
Calum H. 1884410e0d fix: standardize relative timestamping (#3612)
* fix(frontend): relative timestamps are incorrectly rounded.

Closes: #1371

* fix(all): remove legacy fromNow for proper relative timestamp creation

Closes: #1395
2025-05-07 21:37:35 +00:00

24 lines
796 B
TypeScript

import { createFormatter, type FormatOptions, type Formatter } from '@vintl/how-ago'
import type { IntlController } from '@vintl/vintl/controller'
import { useVIntl } from '@vintl/vintl'
import { computed } from 'vue'
/* eslint-disable @typescript-eslint/no-explicit-any */
const formatters = new WeakMap<IntlController<any>, Formatter>()
export function useRelativeTime(): Formatter {
const vintl = useVIntl()
let formatter = formatters.get(vintl)
if (formatter == null) {
const formatterRef = computed(() => createFormatter(vintl.intl))
const defaultOptions: FormatOptions = { roundingMode: 'halfExpand' as const }
formatter = (value, options) => formatterRef.value(value, { ...options, ...defaultOptions })
formatters.set(vintl, formatter)
}
return formatter
}