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.

ConferenceNotification.native.js 7.7KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284
  1. // @flow
  2. import React, { Component } from 'react';
  3. import { Text, TouchableOpacity, View } from 'react-native';
  4. import { connect } from 'react-redux';
  5. import { appNavigate } from '../../app';
  6. import { getURLWithoutParamsNormalized } from '../../base/connection';
  7. import { Icon } from '../../base/font-icons';
  8. import { getLocalizedDateFormatter, translate } from '../../base/i18n';
  9. import { ASPECT_RATIO_NARROW } from '../../base/responsive-ui';
  10. import { CALENDAR_ENABLED } from '../constants';
  11. import styles from './styles';
  12. const ALERT_MILLISECONDS = 5 * 60 * 1000;
  13. type Props = {
  14. /**
  15. * The current aspect ratio of the screen.
  16. */
  17. _aspectRatio: Symbol,
  18. /**
  19. * The URL of the current conference without params.
  20. */
  21. _currentConferenceURL: string,
  22. /**
  23. * The calendar event list.
  24. */
  25. _eventList: Array<Object>,
  26. /**
  27. * The Redux dispatch function.
  28. */
  29. dispatch: Function,
  30. /**
  31. * The translate function.
  32. */
  33. t: Function
  34. };
  35. type State = {
  36. /**
  37. * The event object to display the notification for.
  38. */
  39. event?: Object
  40. };
  41. /**
  42. * Component to display a permanent badge-like notification on the conference
  43. * screen when another meeting is about to start.
  44. */
  45. class ConferenceNotification extends Component<Props, State> {
  46. updateIntervalId: number;
  47. /**
  48. * Constructor of the ConferenceNotification component.
  49. *
  50. * @inheritdoc
  51. */
  52. constructor(props) {
  53. super(props);
  54. this.state = {
  55. event: undefined
  56. };
  57. this._getNotificationContentStyle
  58. = this._getNotificationContentStyle.bind(this);
  59. this._getNotificationPosition
  60. = this._getNotificationPosition.bind(this);
  61. this._maybeDisplayNotification
  62. = this._maybeDisplayNotification.bind(this);
  63. this._onGoToNext = this._onGoToNext.bind(this);
  64. }
  65. /**
  66. * Implements React Component's componentDidMount.
  67. *
  68. * @inheritdoc
  69. */
  70. componentDidMount() {
  71. this.updateIntervalId = setInterval(
  72. this._maybeDisplayNotification,
  73. 10 * 1000
  74. );
  75. }
  76. /**
  77. * Implements React Component's componentWillUnmount.
  78. *
  79. * @inheritdoc
  80. */
  81. componentWillUnmount() {
  82. clearTimeout(this.updateIntervalId);
  83. }
  84. /**
  85. * Implements the React Components's render.
  86. *
  87. * @inheritdoc
  88. */
  89. render() {
  90. const { event } = this.state;
  91. const { t } = this.props;
  92. if (event) {
  93. return (
  94. <View
  95. style = { [
  96. styles.notificationContainer,
  97. this._getNotificationPosition()
  98. ] } >
  99. <View
  100. style = { this._getNotificationContentStyle() }>
  101. <TouchableOpacity
  102. onPress = { this._onGoToNext } >
  103. <View style = { styles.touchableView }>
  104. <View
  105. style = {
  106. styles.notificationTextContainer
  107. }>
  108. <Text style = { styles.notificationText }>
  109. { t('calendarSync.nextMeeting') }
  110. </Text>
  111. <Text style = { styles.notificationText }>
  112. {
  113. getLocalizedDateFormatter(
  114. event.startDate
  115. ).fromNow()
  116. }
  117. </Text>
  118. </View>
  119. <View
  120. style = {
  121. styles.notificationIconContainer
  122. }>
  123. <Icon
  124. name = 'navigate_next'
  125. style = { styles.notificationIcon } />
  126. </View>
  127. </View>
  128. </TouchableOpacity>
  129. </View>
  130. </View>
  131. );
  132. }
  133. return null;
  134. }
  135. _getNotificationContentStyle: () => Array<Object>
  136. /**
  137. * Decides the color of the notification and some additional
  138. * styles based on notificationPosition.
  139. *
  140. * @private
  141. * @returns {Array<Object>}
  142. */
  143. _getNotificationContentStyle() {
  144. const { event } = this.state;
  145. const { _aspectRatio } = this.props;
  146. const now = Date.now();
  147. const style = [
  148. styles.notificationContent
  149. ];
  150. if (event && event.startDate < now && event.endDate > now) {
  151. style.push(styles.notificationContentPast);
  152. } else {
  153. style.push(styles.notificationContentNext);
  154. }
  155. if (_aspectRatio === ASPECT_RATIO_NARROW) {
  156. style.push(styles.notificationContentSide);
  157. } else {
  158. style.push(styles.notificationContentTop);
  159. }
  160. return style;
  161. }
  162. _getNotificationPosition: () => Object;
  163. /**
  164. * Decides the position of the notification.
  165. *
  166. * @private
  167. * @returns {Object}
  168. */
  169. _getNotificationPosition() {
  170. const { _aspectRatio } = this.props;
  171. if (_aspectRatio === ASPECT_RATIO_NARROW) {
  172. return styles.notificationContainerSide;
  173. }
  174. return styles.notificationContainerTop;
  175. }
  176. _maybeDisplayNotification: () => void;
  177. /**
  178. * Periodically checks if there is an event in the calendar for which we
  179. * need to show a notification.
  180. *
  181. * @private
  182. * @returns {void}
  183. */
  184. _maybeDisplayNotification() {
  185. const { _currentConferenceURL, _eventList } = this.props;
  186. let eventToShow;
  187. if (_eventList && _eventList.length) {
  188. const now = Date.now();
  189. for (const event of _eventList) {
  190. const eventUrl
  191. = getURLWithoutParamsNormalized(new URL(event.url));
  192. if (eventUrl !== _currentConferenceURL) {
  193. if ((!eventToShow
  194. && event.startDate > now
  195. && event.startDate < now + ALERT_MILLISECONDS)
  196. || (event.startDate < now && event.endDate > now)) {
  197. eventToShow = event;
  198. }
  199. }
  200. }
  201. }
  202. this.setState({
  203. event: eventToShow
  204. });
  205. }
  206. _onGoToNext: () => void;
  207. /**
  208. * Opens the meeting URL that the notification shows.
  209. *
  210. * @param {string} url - The URL to open.
  211. * @private
  212. * @returns {void}
  213. */
  214. _onGoToNext() {
  215. const { event } = this.state;
  216. if (event && event.url) {
  217. this.props.dispatch(appNavigate(event.url));
  218. }
  219. }
  220. }
  221. /**
  222. * Maps redux state to component props.
  223. *
  224. * @param {Object} state - The redux state.
  225. * @returns {{
  226. * _aspectRatio: Symbol,
  227. * _currentConferenceURL: string,
  228. * _eventList: Array
  229. * }}
  230. */
  231. function _mapStateToProps(state: Object) {
  232. const { locationURL } = state['features/base/connection'];
  233. return {
  234. _aspectRatio: state['features/base/responsive-ui'].aspectRatio,
  235. _currentConferenceURL:
  236. locationURL ? getURLWithoutParamsNormalized(locationURL) : '',
  237. _eventList: state['features/calendar-sync'].events
  238. };
  239. }
  240. export default CALENDAR_ENABLED
  241. ? translate(connect(_mapStateToProps)(ConferenceNotification))
  242. : undefined;