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

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