signal-desktop/ts/util/formatRelativeTime.ts
Scott Nonnenberg 643739f65d
Responding to feedback on the updated visuals (#2549)
* Conversation List Item: timestamp bold only when convo has unread

* Preserve the positioning of overlays on re-entry into convo

* ConversationListItem: Handle missing and broken thumbnails

* Shorten timestamp in left pane for better Android consistency

* Update convo last updated if last was expire timer change

But not if it was from a sync instead of from you or from a contact.

* Make links in quotes the same color as the text

* MediaGridItem: Update placeholder icon colors for dark theme

* Ensure turning off timer shows 'Timer set to off' in left pane

* ConversationListItem: Show unread count in blue circle

* Add one pixel margin to blue indicator for text alignment

* Ensure replies to voice message can bet sent successfully
2018-07-20 16:37:57 -07:00

57 lines
1.6 KiB
TypeScript

import moment from 'moment';
import { Localizer } from '../types/Util';
const getExtendedFormats = (i18n: Localizer) => ({
y: 'lll',
M: `${i18n('timestampFormat_M') || 'MMM D'} LT`,
d: 'ddd LT',
});
const getShortFormats = (i18n: Localizer) => ({
y: 'll',
M: i18n('timestampFormat_M') || 'MMM D',
d: 'ddd',
});
function isToday(timestamp: moment.Moment) {
const today = moment().format('ddd');
const targetDay = moment(timestamp).format('ddd');
return today === targetDay;
}
function isYear(timestamp: moment.Moment) {
const year = moment().format('YYYY');
const targetYear = moment(timestamp).format('YYYY');
return year === targetYear;
}
export function formatRelativeTime(
rawTimestamp: number | Date,
options: { extended: boolean; i18n: Localizer }
) {
const { extended, i18n } = options;
const formats = extended ? getExtendedFormats(i18n) : getShortFormats(i18n);
const timestamp = moment(rawTimestamp);
const now = moment();
const diff = moment.duration(now.diff(timestamp));
if (diff.years() >= 1 || !isYear(timestamp)) {
return timestamp.format(formats.y);
} else if (diff.months() >= 1 || diff.days() > 6) {
return timestamp.format(formats.M);
} else if (diff.days() >= 1 || !isToday(timestamp)) {
return timestamp.format(formats.d);
} else if (diff.hours() >= 1) {
const key = extended ? 'hoursAgo' : 'hoursAgoShort';
return i18n(key, [String(diff.hours())]);
} else if (diff.minutes() >= 1) {
const key = extended ? 'minutesAgo' : 'minutesAgoShort';
return i18n(key, [String(diff.minutes())]);
}
return i18n('justNow');
}