fix: format 7/14 days to be a number of weeks for disappearing timer

pull/3206/head
Audric Ackermann 1 year ago
parent cc17285009
commit bf344329f8
No known key found for this signature in database

@ -107,7 +107,7 @@ export const CreateAccount = () => {
); );
dispatch(setAccountCreationStep(AccountCreation.DisplayName)); dispatch(setAccountCreationStep(AccountCreation.DisplayName));
// Note: we have to assume here that libsession threw an error because the name was too long. // 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'))); dispatch(setDisplayNameError(window.i18n('displayNameErrorDescriptionShorter')));
} }
}; };

@ -198,7 +198,7 @@ export const RestoreAccount = () => {
dispatch(setAccountRestorationStep(AccountRestoration.DisplayName)); dispatch(setAccountRestorationStep(AccountRestoration.DisplayName));
// Note: we have to assume here that libsession threw an error because the name was too long. // 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'))); dispatch(setDisplayNameError(window.i18n('displayNameErrorDescriptionShorter')));
} }
}; };

@ -1,6 +1,8 @@
import { isDevProd } from '../../shared/env_vars'; import { isDevProd } from '../../shared/env_vars';
import { formatAbbreviatedExpireTimer } from '../../util/i18n/formater/expirationTimer'; import {
import { formatTimeDuration } from '../../util/i18n/formater/generics'; formatAbbreviatedExpireTimer,
formatNonAbbreviatedExpireTimer,
} from '../../util/i18n/formater/expirationTimer';
import { DURATION_SECONDS } from '../constants'; import { DURATION_SECONDS } from '../constants';
type TimerOptionsEntry = { name: string; value: number }; type TimerOptionsEntry = { name: string; value: number };
@ -40,13 +42,13 @@ function getName(seconds = 0) {
return window.i18n('off'); return window.i18n('off');
} }
if (seconds > 0) { if (seconds > 0) {
return formatTimeDuration(seconds * 1000); return formatNonAbbreviatedExpireTimer(seconds);
} }
return [seconds, 'seconds'].join(' '); return [seconds, 'seconds'].join(' ');
} }
function getAbbreviated(seconds = 0) { function getAbbreviated(seconds: number) {
if (seconds >= 0) { if (seconds >= 0) {
return formatAbbreviatedExpireTimer(seconds); return formatAbbreviatedExpireTimer(seconds);
} }

@ -102,16 +102,10 @@ function getSelectedBlindedDisabledMsgRequests(state: StateType) {
} }
/** /**
* Defaults to 'all' if undefined * Defaults to 'all' if undefined/unset
*/ */
function getSelectedNotificationSetting(state: StateType) { function getSelectedNotificationSetting(state: StateType) {
const selectedConvoPubkey = getSelectedConversationKey(state); return getSelectedConversation(state)?.currentNotificationSetting || 'all';
if (!selectedConvoPubkey) {
return false;
}
const selectedConvo = getSelectedConversation(state);
return selectedConvo?.currentNotificationSetting || 'all';
} }
const getSelectedConversationType = (state: StateType): ConversationTypeEnum | null => { const getSelectedConversationType = (state: StateType): ConversationTypeEnum | null => {

@ -1,6 +1,7 @@
import { Duration, formatDuration, intervalToDuration } from 'date-fns'; import { Duration, formatDuration, intervalToDuration } from 'date-fns';
import { DURATION_SECONDS } from '../../../session/constants'; import { DURATION_SECONDS } from '../../../session/constants';
import { getForcedEnglishTimeLocale } from '../timeLocaleMap'; import { getForcedEnglishTimeLocale } from '../timeLocaleMap';
import { getTimeLocaleDictionary } from '../shared';
/** /**
* We decided against localizing the abbreviated durations like 1h, 1m, 1s as most apps don't. * 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; 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. * Format an expiring/disappearing message timer to its abbreviated form.
* Note: we don't localize this, and cannot have a value > 4 weeks * 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. * @returns '1h' for a duration of 3600s.
*/ */
export const formatAbbreviatedExpireTimer = (timerSeconds: number) => { export const formatAbbreviatedExpireTimer = (timerSeconds: number) => {
if (timerSeconds > DURATION_SECONDS.WEEKS * 4) { assertIsValidExpirationTimerSeconds(timerSeconds);
throw new Error('formatAbbreviatedExpireTimer is not design to handle >4 weeks durations ');
}
if (timerSeconds <= 0) { if (timerSeconds <= 0) {
return window.i18n('off'); return window.i18n('off');
} }
@ -76,6 +81,27 @@ export const formatAbbreviatedExpireTimer = (timerSeconds: number) => {
return unlocalizedDurationToAbbreviated(unlocalized); 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. * Format an expiring/disappearing message timer to its abbreviated form.
* Note: we don't localize this, and cannot have a value > 4 weeks * Note: we don't localize this, and cannot have a value > 4 weeks

Loading…
Cancel
Save