選択できるのは25トピックまでです。 トピックは、先頭が英数字で、英数字とダッシュ('-')を使用した35文字以内のものにしてください。

functions.native.ts 4.8KB

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