From bf344329f8bfa970b23fccf42b9995fa6ec50462 Mon Sep 17 00:00:00 2001 From: Audric Ackermann Date: Fri, 6 Sep 2024 09:47:01 +1000 Subject: [PATCH] fix: format 7/14 days to be a number of weeks for disappearing timer --- .../registration/stages/CreateAccount.tsx | 2 +- .../registration/stages/RestoreAccount.tsx | 2 +- .../disappearing_messages/timerOptions.ts | 10 +++--- ts/state/selectors/selectedConversation.ts | 10 ++---- ts/util/i18n/formater/expirationTimer.ts | 32 +++++++++++++++++-- 5 files changed, 39 insertions(+), 17 deletions(-) diff --git a/ts/components/registration/stages/CreateAccount.tsx b/ts/components/registration/stages/CreateAccount.tsx index 4fdfd1185..05c04e9e3 100644 --- a/ts/components/registration/stages/CreateAccount.tsx +++ b/ts/components/registration/stages/CreateAccount.tsx @@ -107,7 +107,7 @@ export const CreateAccount = () => { ); dispatch(setAccountCreationStep(AccountCreation.DisplayName)); // Note: we have to assume here that libsession threw an error because the name was too long. - // The error reporterd by libsession is not localized + // The error reported by libsession is not localized dispatch(setDisplayNameError(window.i18n('displayNameErrorDescriptionShorter'))); } }; diff --git a/ts/components/registration/stages/RestoreAccount.tsx b/ts/components/registration/stages/RestoreAccount.tsx index 0fb3413e2..d006287b0 100644 --- a/ts/components/registration/stages/RestoreAccount.tsx +++ b/ts/components/registration/stages/RestoreAccount.tsx @@ -198,7 +198,7 @@ export const RestoreAccount = () => { dispatch(setAccountRestorationStep(AccountRestoration.DisplayName)); // Note: we have to assume here that libsession threw an error because the name was too long. - // The error reporterd by libsession is not localized + // The error reported by libsession is not localized dispatch(setDisplayNameError(window.i18n('displayNameErrorDescriptionShorter'))); } }; diff --git a/ts/session/disappearing_messages/timerOptions.ts b/ts/session/disappearing_messages/timerOptions.ts index 585106a28..de368bf75 100644 --- a/ts/session/disappearing_messages/timerOptions.ts +++ b/ts/session/disappearing_messages/timerOptions.ts @@ -1,6 +1,8 @@ import { isDevProd } from '../../shared/env_vars'; -import { formatAbbreviatedExpireTimer } from '../../util/i18n/formater/expirationTimer'; -import { formatTimeDuration } from '../../util/i18n/formater/generics'; +import { + formatAbbreviatedExpireTimer, + formatNonAbbreviatedExpireTimer, +} from '../../util/i18n/formater/expirationTimer'; import { DURATION_SECONDS } from '../constants'; type TimerOptionsEntry = { name: string; value: number }; @@ -40,13 +42,13 @@ function getName(seconds = 0) { return window.i18n('off'); } if (seconds > 0) { - return formatTimeDuration(seconds * 1000); + return formatNonAbbreviatedExpireTimer(seconds); } return [seconds, 'seconds'].join(' '); } -function getAbbreviated(seconds = 0) { +function getAbbreviated(seconds: number) { if (seconds >= 0) { return formatAbbreviatedExpireTimer(seconds); } diff --git a/ts/state/selectors/selectedConversation.ts b/ts/state/selectors/selectedConversation.ts index a2f5c08b7..95c15ed9b 100644 --- a/ts/state/selectors/selectedConversation.ts +++ b/ts/state/selectors/selectedConversation.ts @@ -102,16 +102,10 @@ function getSelectedBlindedDisabledMsgRequests(state: StateType) { } /** - * Defaults to 'all' if undefined + * Defaults to 'all' if undefined/unset */ function getSelectedNotificationSetting(state: StateType) { - const selectedConvoPubkey = getSelectedConversationKey(state); - if (!selectedConvoPubkey) { - return false; - } - const selectedConvo = getSelectedConversation(state); - - return selectedConvo?.currentNotificationSetting || 'all'; + return getSelectedConversation(state)?.currentNotificationSetting || 'all'; } const getSelectedConversationType = (state: StateType): ConversationTypeEnum | null => { diff --git a/ts/util/i18n/formater/expirationTimer.ts b/ts/util/i18n/formater/expirationTimer.ts index a20398d4a..15021938a 100644 --- a/ts/util/i18n/formater/expirationTimer.ts +++ b/ts/util/i18n/formater/expirationTimer.ts @@ -1,6 +1,7 @@ import { Duration, formatDuration, intervalToDuration } from 'date-fns'; import { DURATION_SECONDS } from '../../../session/constants'; import { getForcedEnglishTimeLocale } from '../timeLocaleMap'; +import { getTimeLocaleDictionary } from '../shared'; /** * We decided against localizing the abbreviated durations like 1h, 1m, 1s as most apps don't. @@ -52,6 +53,12 @@ const secondsToDuration = (seconds: number): Duration => { return duration; }; +function assertIsValidExpirationTimerSeconds(timerSeconds: number) { + if (timerSeconds > DURATION_SECONDS.WEEKS * 4) { + throw new Error('assertIsValidExpirationTimer is not design to handle >4 weeks durations '); + } +} + /** * Format an expiring/disappearing message timer to its abbreviated form. * Note: we don't localize this, and cannot have a value > 4 weeks @@ -60,9 +67,7 @@ const secondsToDuration = (seconds: number): Duration => { * @returns '1h' for a duration of 3600s. */ export const formatAbbreviatedExpireTimer = (timerSeconds: number) => { - if (timerSeconds > DURATION_SECONDS.WEEKS * 4) { - throw new Error('formatAbbreviatedExpireTimer is not design to handle >4 weeks durations '); - } + assertIsValidExpirationTimerSeconds(timerSeconds); if (timerSeconds <= 0) { return window.i18n('off'); } @@ -76,6 +81,27 @@ export const formatAbbreviatedExpireTimer = (timerSeconds: number) => { return unlocalizedDurationToAbbreviated(unlocalized); }; +/** + * Format an expiring/disappearing message timer to its full localized form. + * Note: throws if the value is > 4 weeks + * + * @param timerSeconds the timer to format, in seconds + * @returns '1hour' for a duration of 3600s. + */ +export const formatNonAbbreviatedExpireTimer = (timerSeconds: number) => { + assertIsValidExpirationTimerSeconds(timerSeconds); + + if (timerSeconds <= 0) { + return window.i18n('off'); + } + + const duration = secondsToDuration(timerSeconds); + + return formatDuration(duration, { + locale: getTimeLocaleDictionary(), // we want the full form to be localized + }); +}; + /** * Format an expiring/disappearing message timer to its abbreviated form. * Note: we don't localize this, and cannot have a value > 4 weeks