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.7KB

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