Something went wrong. Try again.
Mirror — see github.com/blacksky-algorithms/blacksky.community
Something went wrong. Try again.
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192import {useCallback} from 'react'import {type I18n} from '@lingui/core'import {defineMessage, msg, plural} from '@lingui/core/macro'import {useLingui} from '@lingui/react'import {differenceInSeconds} from 'date-fns'
export type DateDiffFormat = 'long' | 'short'
type DateDiff = { value: number unit: 'now' | 'second' | 'minute' | 'hour' | 'day' | 'month' earlier: Date later: Date}
const NOW = 5const MINUTE = 60const HOUR = MINUTE * 60const DAY = HOUR * 24const MONTH_30 = DAY * 30
export function useGetTimeAgo({future = false}: {future?: boolean} = {}) { const {i18n} = useLingui() return useCallback( ( earlier: number | string | Date, later: number | string | Date, options?: {format: DateDiffFormat}, ) => { const diff = dateDiff(earlier, later, future ? 'up' : 'down') return formatDateDiff({diff, i18n, format: options?.format}) }, [i18n, future], )}
/** * Returns the difference between `earlier` and `later` dates, based on * opinionated rules. * * - All month are considered exactly 30 days. * - Dates assume `earlier` <= `later`, and will otherwise return 'now'. * - All values round down */export function dateDiff( earlier: number | string | Date, later: number | string | Date, rounding: 'up' | 'down' = 'down',): DateDiff { let diff = { value: 0, unit: 'now' as DateDiff['unit'], } const e = new Date(earlier) const l = new Date(later) const diffSeconds = differenceInSeconds(l, e)
if (diffSeconds < NOW) { diff = { value: 0, unit: 'now', } } else if (diffSeconds < MINUTE) { diff = { value: diffSeconds, unit: 'second', } } else if (diffSeconds < HOUR) { const value = rounding === 'up' ? Math.ceil(diffSeconds / MINUTE) : Math.floor(diffSeconds / MINUTE) diff = { value, unit: 'minute', } } else if (diffSeconds < DAY) { const value = rounding === 'up' ? Math.ceil(diffSeconds / HOUR) : Math.floor(diffSeconds / HOUR) diff = { value, unit: 'hour', } } else if (diffSeconds < MONTH_30) { const value = rounding === 'up' ? Math.ceil(diffSeconds / DAY) : Math.floor(diffSeconds / DAY) diff = { value, unit: 'day', } } else { const value = rounding === 'up' ? Math.ceil(diffSeconds / MONTH_30) : Math.floor(diffSeconds / MONTH_30) diff = { value, unit: 'month', } }
return { ...diff, earlier: e, later: l, }}
/** * Accepts a `DateDiff` and teturns the difference between `earlier` and * `later` dates, formatted as a natural language string. * * - All month are considered exactly 30 days. * - Dates assume `earlier` <= `later`, and will otherwise return 'now'. * - Differences >= 360 days are returned as the "M/D/YYYY" string * - All values round down */export function formatDateDiff({ diff, format = 'short', i18n,}: { diff: DateDiff format?: DateDiffFormat i18n: I18n}): string { const long = format === 'long'
switch (diff.unit) { case 'now': { return i18n._(msg`now`) } case 'second': { return long ? i18n._(plural(diff.value, {one: '# second', other: '# seconds'})) : i18n._( defineMessage({ message: `${diff.value}s`, comment: `How many seconds have passed, displayed in a narrow form`, }), ) } case 'minute': { return long ? i18n._(plural(diff.value, {one: '# minute', other: '# minutes'})) : i18n._( defineMessage({ message: `${diff.value}m`, comment: `How many minutes have passed, displayed in a narrow form`, }), ) } case 'hour': { return long ? i18n._(plural(diff.value, {one: '# hour', other: '# hours'})) : i18n._( defineMessage({ message: `${diff.value}h`, comment: `How many hours have passed, displayed in a narrow form`, }), ) } case 'day': { return long ? i18n._(plural(diff.value, {one: '# day', other: '# days'})) : i18n._( defineMessage({ message: `${diff.value}d`, comment: `How many days have passed, displayed in a narrow form`, }), ) } case 'month': { if (diff.value < 12) { return long ? i18n._(plural(diff.value, {one: '# month', other: '# months'})) : i18n._( defineMessage({ message: plural(diff.value, {one: '#mo', other: '#mo'}), comment: `How many months have passed, displayed in a narrow form`, }), ) } return i18n.date(new Date(diff.earlier)) } }}