signal-desktop/ts/util/expirationTimer.ts

93 lines
2.8 KiB
TypeScript
Raw Normal View History

2023-01-03 19:55:46 +00:00
// Copyright 2020 Signal Messenger, LLC
// SPDX-License-Identifier: AGPL-3.0-only
import humanizeDuration from 'humanize-duration';
import type { Unit } from 'humanize-duration';
import { isNumber } from 'lodash';
import type { LocalizerType } from '../types/Util';
2022-11-16 20:18:02 +00:00
import { SECOND, DurationInSeconds } from './durations';
const SECONDS_PER_WEEK = 604800;
2022-11-16 20:18:02 +00:00
export const DEFAULT_DURATIONS_IN_SECONDS: ReadonlyArray<DurationInSeconds> = [
DurationInSeconds.ZERO,
DurationInSeconds.fromWeeks(4),
DurationInSeconds.fromWeeks(1),
DurationInSeconds.fromDays(1),
DurationInSeconds.fromHours(8),
DurationInSeconds.fromHours(1),
DurationInSeconds.fromMinutes(5),
DurationInSeconds.fromSeconds(30),
];
2022-11-16 20:18:02 +00:00
export const DEFAULT_DURATIONS_SET: ReadonlySet<DurationInSeconds> = new Set(
2021-06-01 20:45:43 +00:00
DEFAULT_DURATIONS_IN_SECONDS
);
export type FormatOptions = {
capitalizeOff?: boolean;
largest?: number; // how many units to show (the largest n)
2021-06-01 20:45:43 +00:00
};
export function format(
i18n: LocalizerType,
2022-11-16 20:18:02 +00:00
dirtySeconds?: DurationInSeconds,
{ capitalizeOff = false, largest }: FormatOptions = {}
2021-06-01 20:45:43 +00:00
): string {
let seconds = Math.abs(dirtySeconds || 0);
if (!seconds) {
2023-03-30 00:03:25 +00:00
return capitalizeOff
? i18n('icu:off')
: i18n('icu:disappearingMessages__off');
}
seconds = Math.max(Math.floor(seconds), 1);
2022-09-21 23:57:44 +00:00
// locale strings coming from electron use a dash as separator
// but humanizeDuration uses an underscore
const locale: string = i18n.getLocale().replace(/-/g, '_');
2022-09-21 23:57:44 +00:00
const localeWithoutRegion: string = locale.split('_', 1)[0];
const fallbacks: Array<string> = [];
if (localeWithoutRegion !== locale) {
fallbacks.push(localeWithoutRegion);
}
if (localeWithoutRegion === 'nb' || localeWithoutRegion === 'nn') {
fallbacks.push('no');
}
if (localeWithoutRegion !== 'en') {
fallbacks.push('en');
}
// humanizeDuration only supports zh_CN and zh_TW
if (locale === 'zh_HK') {
fallbacks.push('zh_TW');
}
const allUnits: Array<Unit> = ['y', 'mo', 'w', 'd', 'h', 'm', 's'];
const defaultUnits: Array<Unit> =
seconds % SECONDS_PER_WEEK === 0 ? ['w'] : ['d', 'h', 'm', 's'];
2022-11-16 20:18:02 +00:00
return humanizeDuration(seconds * SECOND, {
2023-01-01 11:41:40 +00:00
// if we have an explicit `largest` specified,
// allow it to pick from all the units
units: largest ? allUnits : defaultUnits,
largest,
language: locale,
...(fallbacks.length ? { fallbacks } : {}),
});
}
// normally we would not have undefineds all over,
// but most use-cases start out with undefineds
export function calculateExpirationTimestamp({
expireTimer,
expirationStartTimestamp,
}: {
expireTimer?: DurationInSeconds | null;
expirationStartTimestamp?: number | null;
}): number | undefined {
return isNumber(expirationStartTimestamp) && isNumber(expireTimer)
2022-11-16 20:18:02 +00:00
? expirationStartTimestamp + DurationInSeconds.toMillis(expireTimer)
: undefined;
}