2017-12-13 10:35:42 +00:00
|
|
|
// @flow
|
|
|
|
|
|
|
|
import moment from 'moment';
|
|
|
|
|
|
|
|
import { i18next } from '../base/i18n';
|
|
|
|
import { parseURIString } from '../base/util';
|
|
|
|
|
2018-01-26 18:18:43 +00:00
|
|
|
import { RECENT_URL_STORAGE } from './constants';
|
|
|
|
|
2017-12-25 18:03:46 +00:00
|
|
|
/**
|
2018-01-26 18:18:43 +00:00
|
|
|
* MomentJS uses static language bundle loading, so in order to support dynamic
|
|
|
|
* language selection in the app we need to load all bundles that we support in
|
|
|
|
* the app.
|
|
|
|
* FIXME: If we decide to support MomentJS in other features as well we may need
|
|
|
|
* to move this import and the lenient matcher to the i18n feature.
|
2017-12-25 18:03:46 +00:00
|
|
|
*/
|
|
|
|
require('moment/locale/bg');
|
|
|
|
require('moment/locale/de');
|
|
|
|
require('moment/locale/eo');
|
|
|
|
require('moment/locale/es');
|
|
|
|
require('moment/locale/fr');
|
|
|
|
require('moment/locale/hy-am');
|
|
|
|
require('moment/locale/it');
|
|
|
|
require('moment/locale/nb');
|
|
|
|
|
2018-01-26 18:18:43 +00:00
|
|
|
// OC is not available. Please submit OC translation to the MomentJS project.
|
2017-12-25 18:03:46 +00:00
|
|
|
|
|
|
|
require('moment/locale/pl');
|
|
|
|
require('moment/locale/pt');
|
|
|
|
require('moment/locale/pt-br');
|
|
|
|
require('moment/locale/ru');
|
|
|
|
require('moment/locale/sk');
|
|
|
|
require('moment/locale/sl');
|
|
|
|
require('moment/locale/sv');
|
|
|
|
require('moment/locale/tr');
|
|
|
|
require('moment/locale/zh-cn');
|
2017-12-20 00:49:36 +00:00
|
|
|
|
2017-12-13 10:35:42 +00:00
|
|
|
/**
|
2017-12-20 00:49:36 +00:00
|
|
|
* Retreives the recent room list and generates all the data needed to be
|
|
|
|
* displayed.
|
|
|
|
*
|
|
|
|
* @returns {Promise} The {@code Promise} to be resolved when the list is
|
|
|
|
* available.
|
|
|
|
*/
|
2017-12-13 10:35:42 +00:00
|
|
|
export function getRecentRooms(): Promise<Array<Object>> {
|
2017-12-20 00:49:36 +00:00
|
|
|
return new Promise((resolve, reject) =>
|
|
|
|
window.localStorage._getItemAsync(RECENT_URL_STORAGE).then(
|
|
|
|
/* onFulfilled */ recentURLs => {
|
|
|
|
const recentRoomDS = [];
|
2017-12-13 10:35:42 +00:00
|
|
|
|
2017-12-20 00:49:36 +00:00
|
|
|
if (recentURLs) {
|
2018-01-26 18:18:43 +00:00
|
|
|
// We init the locale on every list render, so then it
|
|
|
|
// changes immediately if a language change happens in the
|
|
|
|
// app.
|
2017-12-25 18:03:46 +00:00
|
|
|
const locale = _getSupportedLocale();
|
|
|
|
|
2017-12-20 00:49:36 +00:00
|
|
|
for (const e of JSON.parse(recentURLs)) {
|
|
|
|
const location = parseURIString(e.conference);
|
2017-12-13 10:35:42 +00:00
|
|
|
|
|
|
|
if (location && location.room && location.hostname) {
|
|
|
|
recentRoomDS.push({
|
|
|
|
baseURL:
|
|
|
|
`${location.protocol}//${location.host}`,
|
2017-12-20 00:49:36 +00:00
|
|
|
conference: e.conference,
|
|
|
|
conferenceDuration: e.conferenceDuration,
|
|
|
|
conferenceDurationString:
|
2017-12-25 18:03:46 +00:00
|
|
|
_getDurationString(
|
|
|
|
e.conferenceDuration,
|
|
|
|
locale
|
|
|
|
),
|
|
|
|
dateString: _getDateString(e.date, locale),
|
2017-12-20 00:49:36 +00:00
|
|
|
dateTimeStamp: e.date,
|
2017-12-13 10:35:42 +00:00
|
|
|
initials: _getInitials(location.room),
|
|
|
|
room: location.room,
|
|
|
|
serverName: location.hostname
|
|
|
|
});
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2017-12-20 00:49:36 +00:00
|
|
|
|
|
|
|
resolve(recentRoomDS.reverse());
|
|
|
|
},
|
|
|
|
/* onRejected */ reject)
|
|
|
|
);
|
2017-12-13 10:35:42 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2017-12-20 00:49:36 +00:00
|
|
|
* Retreives the recent URL list as a list of objects.
|
|
|
|
*
|
|
|
|
* @returns {Array} The list of already stored recent URLs.
|
|
|
|
*/
|
|
|
|
export function getRecentURLs() {
|
|
|
|
const recentURLs = window.localStorage.getItem(RECENT_URL_STORAGE);
|
|
|
|
|
|
|
|
return recentURLs ? JSON.parse(recentURLs) : [];
|
2017-12-13 10:35:42 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2017-12-20 00:49:36 +00:00
|
|
|
* Updates the recent URL list.
|
|
|
|
*
|
|
|
|
* @param {Array} recentURLs - The new URL list.
|
|
|
|
* @returns {void}
|
|
|
|
*/
|
|
|
|
export function updateRecentURLs(recentURLs: Array<Object>) {
|
2017-12-13 10:35:42 +00:00
|
|
|
window.localStorage.setItem(
|
|
|
|
RECENT_URL_STORAGE,
|
2017-12-20 00:49:36 +00:00
|
|
|
JSON.stringify(recentURLs)
|
2017-12-13 10:35:42 +00:00
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2017-12-20 00:49:36 +00:00
|
|
|
* Returns a well formatted date string to be displayed in the list.
|
|
|
|
*
|
|
|
|
* @param {number} dateTimeStamp - The UTC timestamp to be converted to String.
|
2017-12-25 18:03:46 +00:00
|
|
|
* @param {string} locale - The locale to init the formatter with. Note: This
|
2018-01-26 18:18:43 +00:00
|
|
|
* locale must be supported by the formatter so ensure this prerequisite before
|
|
|
|
* invoking the function.
|
2017-12-20 00:49:36 +00:00
|
|
|
* @private
|
|
|
|
* @returns {string}
|
|
|
|
*/
|
2017-12-25 18:03:46 +00:00
|
|
|
function _getDateString(dateTimeStamp: number, locale: string) {
|
2017-12-13 10:35:42 +00:00
|
|
|
const date = new Date(dateTimeStamp);
|
2017-12-25 18:03:46 +00:00
|
|
|
const m = _getLocalizedFormatter(date, locale);
|
2017-12-13 10:35:42 +00:00
|
|
|
|
|
|
|
if (date.toDateString() === new Date().toDateString()) {
|
2017-12-20 00:49:36 +00:00
|
|
|
// The date is today, we use fromNow format.
|
|
|
|
return m.fromNow();
|
2017-12-13 10:35:42 +00:00
|
|
|
}
|
|
|
|
|
2017-12-20 00:49:36 +00:00
|
|
|
return m.format('lll');
|
2017-12-13 10:35:42 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2017-12-20 00:49:36 +00:00
|
|
|
* Returns a well formatted duration string to be displayed as the conference
|
|
|
|
* length.
|
|
|
|
*
|
|
|
|
* @param {number} duration - The duration in MS.
|
2017-12-25 18:03:46 +00:00
|
|
|
* @param {string} locale - The locale to init the formatter with. Note: This
|
2018-01-26 18:18:43 +00:00
|
|
|
* locale must be supported by the formatter so ensure this prerequisite before
|
|
|
|
* invoking the function.
|
2017-12-20 00:49:36 +00:00
|
|
|
* @private
|
|
|
|
* @returns {string}
|
|
|
|
*/
|
2017-12-25 18:03:46 +00:00
|
|
|
function _getDurationString(duration: number, locale: string) {
|
|
|
|
return _getLocalizedFormatter(duration, locale)
|
2017-12-13 10:35:42 +00:00
|
|
|
.humanize();
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2017-12-20 00:49:36 +00:00
|
|
|
* Returns the initials supposed to be used based on the room name.
|
|
|
|
*
|
|
|
|
* @param {string} room - The room name.
|
|
|
|
* @private
|
|
|
|
* @returns {string}
|
|
|
|
*/
|
2017-12-13 10:35:42 +00:00
|
|
|
function _getInitials(room: string) {
|
|
|
|
return room && room.charAt(0) ? room.charAt(0).toUpperCase() : '?';
|
|
|
|
}
|
2017-12-25 18:03:46 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Returns a localized date formatter initialized with the
|
|
|
|
* provided date (@code Date) or duration (@code Number).
|
|
|
|
*
|
|
|
|
* @private
|
|
|
|
* @param {Date | number} dateToFormat - The date or duration to format.
|
|
|
|
* @param {string} locale - The locale to init the formatter with. Note: This
|
2018-01-26 18:18:43 +00:00
|
|
|
* locale must be supported by the formatter so ensure this prerequisite before
|
|
|
|
* invoking the function.
|
2017-12-25 18:03:46 +00:00
|
|
|
* @returns {Object}
|
|
|
|
*/
|
|
|
|
function _getLocalizedFormatter(dateToFormat: Date | number, locale: string) {
|
|
|
|
if (typeof dateToFormat === 'number') {
|
|
|
|
return moment.duration(dateToFormat).locale(locale);
|
|
|
|
}
|
|
|
|
|
|
|
|
return moment(dateToFormat).locale(locale);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* A lenient locale matcher to match language and dialect if possible.
|
|
|
|
*
|
|
|
|
* @private
|
|
|
|
* @returns {string}
|
|
|
|
*/
|
|
|
|
function _getSupportedLocale() {
|
2018-01-26 18:18:43 +00:00
|
|
|
const i18nLocale = i18next.language;
|
|
|
|
let supportedLocale;
|
|
|
|
|
|
|
|
if (i18nLocale) {
|
|
|
|
const localeRegexp = new RegExp('^([a-z]{2,2})(-)*([a-z]{2,2})*$');
|
|
|
|
const localeResult = localeRegexp.exec(i18nLocale.toLowerCase());
|
|
|
|
|
|
|
|
if (localeResult) {
|
|
|
|
const currentLocaleRegexp
|
|
|
|
= new RegExp(
|
|
|
|
`^${localeResult[1]}(-)*${`(${localeResult[3]})*` || ''}`);
|
|
|
|
|
|
|
|
supportedLocale
|
|
|
|
= moment.locales().find(lang => currentLocaleRegexp.exec(lang));
|
|
|
|
}
|
2017-12-25 18:03:46 +00:00
|
|
|
}
|
|
|
|
|
2018-01-26 18:18:43 +00:00
|
|
|
return supportedLocale || 'en';
|
2017-12-25 18:03:46 +00:00
|
|
|
}
|