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 8.1KB

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