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

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