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.

RecentList.native.js 6.0KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234
  1. // @flow
  2. import React, { Component } from 'react';
  3. import { connect } from 'react-redux';
  4. import { appNavigate } from '../../app';
  5. import {
  6. getLocalizedDateFormatter,
  7. getLocalizedDurationFormatter,
  8. translate
  9. } from '../../base/i18n';
  10. import { NavigateSectionList } from '../../base/react';
  11. import { parseURIString } from '../../base/util';
  12. /**
  13. * The type of the React {@code Component} props of {@link RecentList}
  14. */
  15. type Props = {
  16. /**
  17. * Renders the list disabled.
  18. */
  19. disabled: boolean,
  20. /**
  21. * The redux store's {@code dispatch} function.
  22. */
  23. dispatch: Dispatch<*>,
  24. /**
  25. * The translate function.
  26. */
  27. t: Function,
  28. /**
  29. * The default server URL.
  30. */
  31. _defaultServerURL: string,
  32. /**
  33. * The recent list from the Redux store.
  34. */
  35. _recentList: Array<Object>
  36. };
  37. /**
  38. * The native container rendering the list of the recently joined rooms.
  39. *
  40. */
  41. class RecentList extends Component<Props> {
  42. /**
  43. * Initializes a new {@code RecentList} instance.
  44. *
  45. * @inheritdoc
  46. */
  47. constructor(props: Props) {
  48. super(props);
  49. this._onPress = this._onPress.bind(this);
  50. this._toDateString = this._toDateString.bind(this);
  51. this._toDurationString = this._toDurationString.bind(this);
  52. this._toDisplayableItem = this._toDisplayableItem.bind(this);
  53. this._toDisplayableList = this._toDisplayableList.bind(this);
  54. }
  55. /**
  56. * Implements the React Components's render method.
  57. *
  58. * @inheritdoc
  59. */
  60. render() {
  61. const { disabled } = this.props;
  62. return (
  63. <NavigateSectionList
  64. disabled = { disabled }
  65. onPress = { this._onPress }
  66. sections = { this._toDisplayableList() } />
  67. );
  68. }
  69. _onPress: string => Function;
  70. /**
  71. * Handles the list's navigate action.
  72. *
  73. * @private
  74. * @param {string} url - The url string to navigate to.
  75. * @returns {void}
  76. */
  77. _onPress(url) {
  78. const { dispatch } = this.props;
  79. dispatch(appNavigate(url));
  80. }
  81. _toDisplayableItem: Object => Object;
  82. /**
  83. * Creates a displayable list item of a recent list entry.
  84. *
  85. * @private
  86. * @param {Object} item - The recent list entry.
  87. * @returns {Object}
  88. */
  89. _toDisplayableItem(item) {
  90. const { _defaultServerURL } = this.props;
  91. const location = parseURIString(item.conference);
  92. const baseURL = `${location.protocol}//${location.host}`;
  93. const serverName = baseURL === _defaultServerURL ? null : location.host;
  94. return {
  95. colorBase: serverName,
  96. key: `key-${item.conference}-${item.date}`,
  97. lines: [
  98. this._toDateString(item.date),
  99. this._toDurationString(item.duration),
  100. serverName
  101. ],
  102. title: location.room,
  103. url: item.conference
  104. };
  105. }
  106. _toDisplayableList: () => Array<Object>;
  107. /**
  108. * Transforms the history list to a displayable list
  109. * with sections.
  110. *
  111. * @private
  112. * @returns {Array<Object>}
  113. */
  114. _toDisplayableList() {
  115. const { _recentList, t } = this.props;
  116. const { createSection } = NavigateSectionList;
  117. const todaySection = createSection(t('recentList.today'), 'today');
  118. const yesterdaySection
  119. = createSection(t('recentList.yesterday'), 'yesterday');
  120. const earlierSection
  121. = createSection(t('recentList.earlier'), 'earlier');
  122. const today = new Date().toDateString();
  123. const yesterdayDate = new Date();
  124. yesterdayDate.setDate(yesterdayDate.getDate() - 1);
  125. const yesterday = yesterdayDate.toDateString();
  126. for (const item of _recentList) {
  127. const itemDay = new Date(item.date).toDateString();
  128. const displayableItem = this._toDisplayableItem(item);
  129. if (itemDay === today) {
  130. todaySection.data.push(displayableItem);
  131. } else if (itemDay === yesterday) {
  132. yesterdaySection.data.push(displayableItem);
  133. } else {
  134. earlierSection.data.push(displayableItem);
  135. }
  136. }
  137. const displayableList = [];
  138. if (todaySection.data.length) {
  139. todaySection.data.reverse();
  140. displayableList.push(todaySection);
  141. }
  142. if (yesterdaySection.data.length) {
  143. yesterdaySection.data.reverse();
  144. displayableList.push(yesterdaySection);
  145. }
  146. if (earlierSection.data.length) {
  147. earlierSection.data.reverse();
  148. displayableList.push(earlierSection);
  149. }
  150. return displayableList;
  151. }
  152. _toDateString: number => string;
  153. /**
  154. * Generates a date string for the item.
  155. *
  156. * @private
  157. * @param {number} itemDate - The item's timestamp.
  158. * @returns {string}
  159. */
  160. _toDateString(itemDate) {
  161. const date = new Date(itemDate);
  162. const m = getLocalizedDateFormatter(itemDate);
  163. if (date.toDateString() === new Date().toDateString()) {
  164. // The date is today, we use fromNow format.
  165. return m.fromNow();
  166. }
  167. return m.format('lll');
  168. }
  169. _toDurationString: number => string;
  170. /**
  171. * Generates a duration string for the item.
  172. *
  173. * @private
  174. * @param {number} duration - The item's duration.
  175. * @returns {string}
  176. */
  177. _toDurationString(duration) {
  178. if (duration) {
  179. return getLocalizedDurationFormatter(duration).humanize();
  180. }
  181. return null;
  182. }
  183. }
  184. /**
  185. * Maps redux state to component props.
  186. *
  187. * @param {Object} state - The redux state.
  188. * @returns {{
  189. * _defaultServerURL: string,
  190. * _recentList: Array
  191. * }}
  192. */
  193. export function _mapStateToProps(state: Object) {
  194. return {
  195. _defaultServerURL: state['features/app'].app._getDefaultURL(),
  196. _recentList: state['features/recent-list']
  197. };
  198. }
  199. export default translate(connect(_mapStateToProps)(RecentList));