You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

functions.js 3.4KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117
  1. // @flow
  2. import moment from 'moment';
  3. import { i18next } from '../base/i18n';
  4. import { parseURIString } from '../base/util';
  5. import { RECENT_URL_STORAGE } from './constants';
  6. /**
  7. * Retreives the recent room list and generates all the data needed to be
  8. * displayed.
  9. *
  10. * @returns {Promise} The {@code Promise} to be resolved when the list is
  11. * available.
  12. */
  13. export function getRecentRooms(): Promise<Array<Object>> {
  14. return new Promise((resolve, reject) =>
  15. window.localStorage._getItemAsync(RECENT_URL_STORAGE).then(
  16. /* onFulfilled */ recentURLs => {
  17. const recentRoomDS = [];
  18. if (recentURLs) {
  19. for (const e of JSON.parse(recentURLs)) {
  20. const location = parseURIString(e.conference);
  21. if (location && location.room && location.hostname) {
  22. recentRoomDS.push({
  23. baseURL:
  24. `${location.protocol}//${location.host}`,
  25. conference: e.conference,
  26. conferenceDuration: e.conferenceDuration,
  27. conferenceDurationString:
  28. _getDurationString(e.conferenceDuration),
  29. dateString: _getDateString(e.date),
  30. dateTimeStamp: e.date,
  31. initials: _getInitials(location.room),
  32. room: location.room,
  33. serverName: location.hostname
  34. });
  35. }
  36. }
  37. }
  38. resolve(recentRoomDS.reverse());
  39. },
  40. /* onRejected */ reject)
  41. );
  42. }
  43. /**
  44. * Retreives the recent URL list as a list of objects.
  45. *
  46. * @returns {Array} The list of already stored recent URLs.
  47. */
  48. export function getRecentURLs() {
  49. const recentURLs = window.localStorage.getItem(RECENT_URL_STORAGE);
  50. return recentURLs ? JSON.parse(recentURLs) : [];
  51. }
  52. /**
  53. * Updates the recent URL list.
  54. *
  55. * @param {Array} recentURLs - The new URL list.
  56. * @returns {void}
  57. */
  58. export function updateRecentURLs(recentURLs: Array<Object>) {
  59. window.localStorage.setItem(
  60. RECENT_URL_STORAGE,
  61. JSON.stringify(recentURLs)
  62. );
  63. }
  64. /**
  65. * Returns a well formatted date string to be displayed in the list.
  66. *
  67. * @param {number} dateTimeStamp - The UTC timestamp to be converted to String.
  68. * @private
  69. * @returns {string}
  70. */
  71. function _getDateString(dateTimeStamp: number) {
  72. const date = new Date(dateTimeStamp);
  73. const m = moment(date).locale(i18next.language);
  74. if (date.toDateString() === new Date().toDateString()) {
  75. // The date is today, we use fromNow format.
  76. return m.fromNow();
  77. }
  78. return m.format('lll');
  79. }
  80. /**
  81. * Returns a well formatted duration string to be displayed as the conference
  82. * length.
  83. *
  84. * @param {number} duration - The duration in MS.
  85. * @private
  86. * @returns {string}
  87. */
  88. function _getDurationString(duration: number) {
  89. return moment.duration(duration)
  90. .locale(i18next.language)
  91. .humanize();
  92. }
  93. /**
  94. * Returns the initials supposed to be used based on the room name.
  95. *
  96. * @param {string} room - The room name.
  97. * @private
  98. * @returns {string}
  99. */
  100. function _getInitials(room: string) {
  101. return room && room.charAt(0) ? room.charAt(0).toUpperCase() : '?';
  102. }