Render disappearing message timers generically

This commit is contained in:
Evan Hahn 2021-05-03 18:24:40 -05:00 committed by GitHub
parent c1730e055f
commit 736075322c
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
16 changed files with 372 additions and 307 deletions

View file

@ -1,77 +0,0 @@
// Copyright 2020 Signal Messenger, LLC
// SPDX-License-Identifier: AGPL-3.0-only
import * as Backbone from 'backbone';
import * as moment from 'moment';
import { LocalizerType } from '../types/Util';
type ExpirationTime = [
number,
(
| 'second'
| 'seconds'
| 'minute'
| 'minutes'
| 'hour'
| 'hours'
| 'day'
| 'week'
)
];
const EXPIRATION_TIMES: Array<ExpirationTime> = [
[0, 'seconds'],
[5, 'seconds'],
[10, 'seconds'],
[30, 'seconds'],
[1, 'minute'],
[5, 'minutes'],
[30, 'minutes'],
[1, 'hour'],
[6, 'hours'],
[12, 'hours'],
[1, 'day'],
[1, 'week'],
];
export const TimerOption = Backbone.Model.extend({
getName(i18n: LocalizerType) {
return (
i18n(['timerOption', this.get('time'), this.get('unit')].join('_')) ||
moment.duration(this.get('time'), this.get('unit')).humanize()
);
},
getAbbreviated(i18n: LocalizerType) {
return i18n(
['timerOption', this.get('time'), this.get('unit'), 'abbreviated'].join(
'_'
)
);
},
});
export const ExpirationTimerOptions = new (Backbone.Collection.extend({
model: TimerOption,
getName(i18n: LocalizerType, seconds = 0) {
const o = this.findWhere({ seconds });
if (o) {
return o.getName(i18n);
}
return [seconds, 'seconds'].join(' ');
},
getAbbreviated(i18n: LocalizerType, seconds = 0) {
const o = this.findWhere({ seconds });
if (o) {
return o.getAbbreviated(i18n);
}
return [seconds, 's'].join('');
},
}))(
EXPIRATION_TIMES.map(o => {
const duration = moment.duration(o[0], o[1]); // 5, 'seconds'
return {
time: o[0],
unit: o[1],
seconds: duration.asSeconds(),
};
})
);

View file

@ -0,0 +1,49 @@
// Copyright 2020-2021 Signal Messenger, LLC
// SPDX-License-Identifier: AGPL-3.0-only
import * as moment from 'moment';
import humanizeDuration from 'humanize-duration';
import { LocalizerType } from '../types/Util';
const SECONDS_PER_WEEK = 604800;
export const DEFAULT_DURATIONS_IN_SECONDS = [
0,
5,
10,
30,
moment.duration(1, 'minute').asSeconds(),
moment.duration(5, 'minutes').asSeconds(),
moment.duration(30, 'minutes').asSeconds(),
moment.duration(1, 'hour').asSeconds(),
moment.duration(6, 'hours').asSeconds(),
moment.duration(12, 'hours').asSeconds(),
moment.duration(1, 'day').asSeconds(),
moment.duration(1, 'week').asSeconds(),
];
export function format(i18n: LocalizerType, dirtySeconds?: number): string {
let seconds = Math.abs(dirtySeconds || 0);
if (!seconds) {
return i18n('disappearingMessages__off');
}
seconds = Math.max(Math.floor(seconds), 1);
const locale: string = i18n.getLocale();
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');
}
return humanizeDuration(seconds * 1000, {
units: seconds % SECONDS_PER_WEEK === 0 ? ['w'] : ['d', 'h', 'm', 's'],
language: locale,
...(fallbacks.length ? { fallbacks } : {}),
});
}

View file

@ -16492,7 +16492,7 @@
"rule": "React-createRef",
"path": "ts/components/conversation/ConversationHeader.js",
"line": " this.menuTriggerRef = react_1.default.createRef();",
"lineNumber": 32,
"lineNumber": 51,
"reasonCategory": "usageTrusted",
"updated": "2020-08-28T16:12:19.904Z",
"reasonDetail": "Used to reference popup menu"
@ -16501,7 +16501,7 @@
"rule": "React-createRef",
"path": "ts/components/conversation/ConversationHeader.tsx",
"line": " this.menuTriggerRef = React.createRef();",
"lineNumber": 112,
"lineNumber": 109,
"reasonCategory": "usageTrusted",
"updated": "2020-05-20T20:10:43.540Z",
"reasonDetail": "Used to reference popup menu"