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

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