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.native.js 4.6KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149
  1. import {
  2. getLocalizedDateFormatter,
  3. getLocalizedDurationFormatter
  4. } from '../base/i18n';
  5. import { NavigateSectionList } from '../base/react';
  6. import { parseURIString, safeDecodeURIComponent } from '../base/util';
  7. /**
  8. * Creates a displayable list item of a recent list entry.
  9. *
  10. * @private
  11. * @param {Object} item - The recent list entry.
  12. * @param {string} defaultServerURL - The default server URL.
  13. * @param {Function} t - The translate function.
  14. * @returns {Object}
  15. */
  16. function toDisplayableItem(item, defaultServerURL, t) {
  17. const location = parseURIString(item.conference);
  18. const baseURL = `${location.protocol}//${location.host}`;
  19. const serverName = baseURL === defaultServerURL ? null : location.host;
  20. return {
  21. colorBase: serverName,
  22. id: {
  23. date: item.date,
  24. url: item.conference
  25. },
  26. key: `key-${item.conference}-${item.date}`,
  27. lines: [
  28. _toDateString(item.date, t),
  29. _toDurationString(item.duration),
  30. serverName
  31. ],
  32. title: safeDecodeURIComponent(location.room),
  33. url: item.conference
  34. };
  35. }
  36. /**
  37. * Generates a duration string for the item.
  38. *
  39. * @private
  40. * @param {number} duration - The item's duration.
  41. * @returns {string}
  42. */
  43. function _toDurationString(duration) {
  44. if (duration) {
  45. return getLocalizedDurationFormatter(duration);
  46. }
  47. return null;
  48. }
  49. /**
  50. * Generates a date string for the item.
  51. *
  52. * @private
  53. * @param {number} itemDate - The item's timestamp.
  54. * @param {Function} t - The translate function.
  55. * @returns {string}
  56. */
  57. function _toDateString(itemDate, t) {
  58. const m = getLocalizedDateFormatter(itemDate);
  59. const date = new Date(itemDate);
  60. const dateInMs = date.getTime();
  61. const now = new Date();
  62. const todayInMs = (new Date()).setHours(0, 0, 0, 0);
  63. const yesterdayInMs = todayInMs - 86400000; // 1 day = 86400000ms
  64. if (dateInMs >= todayInMs) {
  65. return m.fromNow();
  66. } else if (dateInMs >= yesterdayInMs) {
  67. return t('dateUtils.yesterday');
  68. } else if (date.getFullYear() !== now.getFullYear()) {
  69. // We only want to include the year in the date if its not the current
  70. // year.
  71. return m.format('ddd, MMMM DD h:mm A, gggg');
  72. }
  73. return m.format('ddd, MMMM DD h:mm A');
  74. }
  75. /**
  76. * Transforms the history list to a displayable list
  77. * with sections.
  78. *
  79. * @private
  80. * @param {Array<Object>} recentList - The recent list form the redux store.
  81. * @param {Function} t - The translate function.
  82. * @param {string} defaultServerURL - The default server URL.
  83. * @returns {Array<Object>}
  84. */
  85. export function toDisplayableList(recentList, t, defaultServerURL) {
  86. const { createSection } = NavigateSectionList;
  87. const todaySection = createSection(t('dateUtils.today'), 'today');
  88. const yesterdaySection
  89. = createSection(t('dateUtils.yesterday'), 'yesterday');
  90. const earlierSection
  91. = createSection(t('dateUtils.earlier'), 'earlier');
  92. const today = new Date();
  93. const todayString = today.toDateString();
  94. const yesterday = new Date();
  95. yesterday.setDate(yesterday.getDate() - 1);
  96. const yesterdayString = yesterday.toDateString();
  97. for (const item of recentList) {
  98. const itemDateString = new Date(item.date).toDateString();
  99. const displayableItem = toDisplayableItem(item, defaultServerURL, t);
  100. if (itemDateString === todayString) {
  101. todaySection.data.push(displayableItem);
  102. } else if (itemDateString === yesterdayString) {
  103. yesterdaySection.data.push(displayableItem);
  104. } else {
  105. earlierSection.data.push(displayableItem);
  106. }
  107. }
  108. const displayableList = [];
  109. // the recent list in the redux store has the latest date in the last index
  110. // therefore all the sectionLists' data that was created by parsing through
  111. // the recent list is in reverse order and must be reversed for the most
  112. // item to show first
  113. if (todaySection.data.length) {
  114. todaySection.data.reverse();
  115. displayableList.push(todaySection);
  116. }
  117. if (yesterdaySection.data.length) {
  118. yesterdaySection.data.reverse();
  119. displayableList.push(yesterdaySection);
  120. }
  121. if (earlierSection.data.length) {
  122. earlierSection.data.reverse();
  123. displayableList.push(earlierSection);
  124. }
  125. return displayableList;
  126. }
  127. /**
  128. * Returns <tt>true</tt> if recent list is enabled and <tt>false</tt> otherwise.
  129. *
  130. * @returns {boolean} <tt>true</tt> if recent list is enabled and <tt>false</tt>
  131. * otherwise.
  132. */
  133. export function isRecentListEnabled() {
  134. return true;
  135. }