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

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240
  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 todaySection = NavigateSectionList.createSection(
  117. t('recentList.today'),
  118. 'today'
  119. );
  120. const yesterdaySection = NavigateSectionList.createSection(
  121. t('recentList.yesterday'),
  122. 'yesterday'
  123. );
  124. const earlierSection = NavigateSectionList.createSection(
  125. t('recentList.earlier'),
  126. 'earlier'
  127. );
  128. const today = new Date().toDateString();
  129. const yesterdayDate = new Date();
  130. yesterdayDate.setDate(yesterdayDate.getDate() - 1);
  131. const yesterday = yesterdayDate.toDateString();
  132. for (const item of _recentList) {
  133. const itemDay = new Date(item.date).toDateString();
  134. const displayableItem = this._toDisplayableItem(item);
  135. if (itemDay === today) {
  136. todaySection.data.push(displayableItem);
  137. } else if (itemDay === yesterday) {
  138. yesterdaySection.data.push(displayableItem);
  139. } else {
  140. earlierSection.data.push(displayableItem);
  141. }
  142. }
  143. const displayableList = [];
  144. if (todaySection.data.length) {
  145. todaySection.data.reverse();
  146. displayableList.push(todaySection);
  147. }
  148. if (yesterdaySection.data.length) {
  149. yesterdaySection.data.reverse();
  150. displayableList.push(yesterdaySection);
  151. }
  152. if (earlierSection.data.length) {
  153. earlierSection.data.reverse();
  154. displayableList.push(earlierSection);
  155. }
  156. return displayableList;
  157. }
  158. _toDateString: number => string
  159. /**
  160. * Generates a date string for the item.
  161. *
  162. * @private
  163. * @param {number} itemDate - The item's timestamp.
  164. * @returns {string}
  165. */
  166. _toDateString(itemDate) {
  167. const date = new Date(itemDate);
  168. const m = getLocalizedDateFormatter(itemDate);
  169. if (date.toDateString() === new Date().toDateString()) {
  170. // The date is today, we use fromNow format.
  171. return m.fromNow();
  172. }
  173. return m.format('lll');
  174. }
  175. _toDurationString: number => string
  176. /**
  177. * Generates a duration string for the item.
  178. *
  179. * @private
  180. * @param {number} duration - The item's duration.
  181. * @returns {string}
  182. */
  183. _toDurationString(duration) {
  184. if (duration) {
  185. return getLocalizedDurationFormatter(duration).humanize();
  186. }
  187. return null;
  188. }
  189. }
  190. /**
  191. * Maps redux state to component props.
  192. *
  193. * @param {Object} state - The redux state.
  194. * @returns {{
  195. * _defaultServerURL: string,
  196. * _recentList: Array
  197. * }}
  198. */
  199. export function _mapStateToProps(state: Object) {
  200. return {
  201. _defaultServerURL: state['features/app'].app._getDefaultURL(),
  202. _recentList: state['features/recent-list']
  203. };
  204. }
  205. export default translate(connect(_mapStateToProps)(RecentList));