|
| 1 | +import { useTranslation } from 'react-i18next'; |
| 2 | + |
| 3 | + |
| 4 | +export const LAST_LANGUAGE_LOCAL_STORAGE_KEY = 'bridge/last-language'; |
| 5 | + |
| 6 | +export const getLastLanguage = (): string => |
| 7 | + localStorage.getItem(LAST_LANGUAGE_LOCAL_STORAGE_KEY) ?? navigator.language; |
| 8 | + |
| 9 | +export type Duration = { |
| 10 | + days: number; |
| 11 | + hours: number; |
| 12 | + minutes: number; |
| 13 | + seconds: number; |
| 14 | +}; |
| 15 | + |
| 16 | +export const relativeTimeFormatter = (langArg: string) => |
| 17 | + Intl.RelativeTimeFormat ? new Intl.RelativeTimeFormat(langArg) : null; |
| 18 | + |
| 19 | +export const dateTimeFormatter = (langArg: string) => |
| 20 | + new Intl.DateTimeFormat(langArg, { |
| 21 | + month: 'short', |
| 22 | + day: 'numeric', |
| 23 | + hour: 'numeric', |
| 24 | + minute: 'numeric', |
| 25 | + year: 'numeric', |
| 26 | + }); |
| 27 | + |
| 28 | +function getDuration(ms: number): Duration { |
| 29 | + let seconds = Math.floor(ms / 1000); |
| 30 | + let minutes = Math.floor(seconds / 60); |
| 31 | + seconds %= 60; |
| 32 | + let hours = Math.floor(minutes / 60); |
| 33 | + minutes %= 60; |
| 34 | + const days = Math.floor(hours / 24); |
| 35 | + hours %= 24; |
| 36 | + return { days, hours, minutes, seconds }; |
| 37 | +} |
| 38 | + |
| 39 | +export const formatDuration = (ms: number, options?) => { |
| 40 | + const { t } = useTranslation('plugin__camel-openshift-console-plugin'); |
| 41 | + const langArg = getLastLanguage(); |
| 42 | + const duration = getDuration(ms); |
| 43 | + |
| 44 | + // Check for null. If dateTime is null, it returns incorrect date Jan 1 1970. |
| 45 | + if (!duration) { |
| 46 | + return '-'; |
| 47 | + } |
| 48 | + |
| 49 | + const d = new Date(ms); |
| 50 | + const justNow = t('Just now'); |
| 51 | + |
| 52 | + // If the event occurred less than one minute in the future, assume it's clock drift and show "Just now." |
| 53 | + if (!options?.omitSuffix && ms < 60000 && ms > -60000) { |
| 54 | + return justNow; |
| 55 | + } |
| 56 | + |
| 57 | + // Do not attempt to handle other dates in the future. |
| 58 | + if (ms < 0) { |
| 59 | + return '-'; |
| 60 | + } |
| 61 | + |
| 62 | + const { days, hours, minutes } = getDuration(ms); |
| 63 | + |
| 64 | + if (options?.omitSuffix) { |
| 65 | + if (days) { |
| 66 | + return t('{{count}} day', { count: days }); |
| 67 | + } |
| 68 | + if (hours) { |
| 69 | + return t('{{count}} hour', { count: hours }); |
| 70 | + } |
| 71 | + return t('{{count}} minute', { count: minutes }); |
| 72 | + } |
| 73 | + |
| 74 | + // Fallback to normal date/time formatting if Intl.RelativeTimeFormat is not |
| 75 | + // available. This is the case for older Safari versions. |
| 76 | + // https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Intl/RelativeTimeFormat#browser_compatibility |
| 77 | + if (!relativeTimeFormatter(langArg)) { |
| 78 | + return dateTimeFormatter(langArg).format(d); |
| 79 | + } |
| 80 | + |
| 81 | + if (!days && !hours && !minutes) { |
| 82 | + return justNow; |
| 83 | + } |
| 84 | + |
| 85 | + if (days) { |
| 86 | + return relativeTimeFormatter(langArg).format(-days, 'day'); |
| 87 | + } |
| 88 | + |
| 89 | + if (hours) { |
| 90 | + return relativeTimeFormatter(langArg).format(-hours, 'hour'); |
| 91 | + } |
| 92 | + |
| 93 | + return relativeTimeFormatter(langArg).format(-minutes, 'minute'); |
| 94 | +}; |
0 commit comments