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

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