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

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289
  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: *;
  47. /**
  48. * Constructor of the ConferenceNotification component.
  49. *
  50. * @inheritdoc
  51. */
  52. constructor(props: 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. clearInterval(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. const now = Date.now();
  94. const label
  95. = event.startDate < now && event.endDate > now
  96. ? 'calendarSync.ongoingMeeting'
  97. : 'calendarSync.nextMeeting';
  98. return (
  99. <View
  100. style = { [
  101. styles.notificationContainer,
  102. this._getNotificationPosition()
  103. ] } >
  104. <View
  105. style = { this._getNotificationContentStyle() }>
  106. <TouchableOpacity
  107. onPress = { this._onGoToNext } >
  108. <View style = { styles.touchableView }>
  109. <View
  110. style = {
  111. styles.notificationTextContainer
  112. }>
  113. <Text style = { styles.notificationText }>
  114. { t(label) }
  115. </Text>
  116. <Text style = { styles.notificationText }>
  117. {
  118. getLocalizedDateFormatter(
  119. event.startDate
  120. ).fromNow()
  121. }
  122. </Text>
  123. </View>
  124. <View
  125. style = {
  126. styles.notificationIconContainer
  127. }>
  128. <Icon
  129. name = 'navigate_next'
  130. style = { styles.notificationIcon } />
  131. </View>
  132. </View>
  133. </TouchableOpacity>
  134. </View>
  135. </View>
  136. );
  137. }
  138. return null;
  139. }
  140. _getNotificationContentStyle: () => Array<Object>;
  141. /**
  142. * Decides the color of the notification and some additional
  143. * styles based on notificationPosition.
  144. *
  145. * @private
  146. * @returns {Array<Object>}
  147. */
  148. _getNotificationContentStyle() {
  149. const { event } = this.state;
  150. const { _aspectRatio } = this.props;
  151. const now = Date.now();
  152. const style = [
  153. styles.notificationContent
  154. ];
  155. if (event && event.startDate < now && event.endDate > now) {
  156. style.push(styles.notificationContentPast);
  157. } else {
  158. style.push(styles.notificationContentNext);
  159. }
  160. if (_aspectRatio === ASPECT_RATIO_NARROW) {
  161. style.push(styles.notificationContentSide);
  162. } else {
  163. style.push(styles.notificationContentTop);
  164. }
  165. return style;
  166. }
  167. _getNotificationPosition: () => Object;
  168. /**
  169. * Decides the position of the notification.
  170. *
  171. * @private
  172. * @returns {Object}
  173. */
  174. _getNotificationPosition() {
  175. const { _aspectRatio } = this.props;
  176. if (_aspectRatio === ASPECT_RATIO_NARROW) {
  177. return styles.notificationContainerSide;
  178. }
  179. return styles.notificationContainerTop;
  180. }
  181. _maybeDisplayNotification: () => void;
  182. /**
  183. * Periodically checks if there is an event in the calendar for which we
  184. * need to show a notification.
  185. *
  186. * @private
  187. * @returns {void}
  188. */
  189. _maybeDisplayNotification() {
  190. const { _currentConferenceURL, _eventList } = this.props;
  191. let eventToShow;
  192. if (_eventList && _eventList.length) {
  193. const now = Date.now();
  194. for (const event of _eventList) {
  195. const eventUrl
  196. = getURLWithoutParamsNormalized(new URL(event.url));
  197. if (eventUrl !== _currentConferenceURL) {
  198. if ((!eventToShow
  199. && event.startDate > now
  200. && event.startDate < now + ALERT_MILLISECONDS)
  201. || (event.startDate < now && event.endDate > now)) {
  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. * @returns {void}
  217. */
  218. _onGoToNext() {
  219. const { event } = this.state;
  220. if (event && event.url) {
  221. this.props.dispatch(appNavigate(event.url));
  222. }
  223. }
  224. }
  225. /**
  226. * Maps redux state to component props.
  227. *
  228. * @param {Object} state - The redux state.
  229. * @returns {{
  230. * _aspectRatio: Symbol,
  231. * _currentConferenceURL: string,
  232. * _eventList: Array
  233. * }}
  234. */
  235. function _mapStateToProps(state: Object) {
  236. const { locationURL } = state['features/base/connection'];
  237. return {
  238. _aspectRatio: state['features/base/responsive-ui'].aspectRatio,
  239. _currentConferenceURL:
  240. locationURL ? getURLWithoutParamsNormalized(locationURL) : '',
  241. _eventList: state['features/calendar-sync'].events
  242. };
  243. }
  244. export default CALENDAR_ENABLED
  245. ? translate(connect(_mapStateToProps)(ConferenceNotification))
  246. : undefined;