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.

AbstractPageReloadOverlay.js 7.3KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274
  1. // @flow
  2. import PropTypes from 'prop-types';
  3. import React, { Component } from 'react';
  4. import {
  5. createPageReloadScheduledEvent,
  6. sendAnalytics
  7. } from '../../analytics';
  8. import {
  9. isFatalJitsiConferenceError,
  10. isFatalJitsiConnectionError
  11. } from '../../base/lib-jitsi-meet';
  12. import { randomInt } from '../../base/util';
  13. import { _reloadNow } from '../actions';
  14. import ReloadButton from './ReloadButton';
  15. declare var APP: Object;
  16. const logger = require('jitsi-meet-logger').getLogger(__filename);
  17. /**
  18. * Implements an abstract React {@link Component} for the page reload overlays.
  19. */
  20. export default class AbstractPageReloadOverlay extends Component<*, *> {
  21. /**
  22. * {@code AbstractPageReloadOverlay} component's property types.
  23. *
  24. * @static
  25. */
  26. static propTypes = {
  27. dispatch: PropTypes.func,
  28. /**
  29. * The indicator which determines whether the reload was caused by
  30. * network failure.
  31. *
  32. * @public
  33. * @type {boolean}
  34. */
  35. isNetworkFailure: PropTypes.bool,
  36. /**
  37. * The reason for the error that will cause the reload.
  38. * NOTE: Used by PageReloadOverlay only.
  39. *
  40. * @public
  41. * @type {string}
  42. */
  43. reason: PropTypes.string,
  44. /**
  45. * The function to translate human-readable text.
  46. *
  47. * @public
  48. * @type {Function}
  49. */
  50. t: PropTypes.func
  51. };
  52. /**
  53. * Determines whether this overlay needs to be rendered (according to a
  54. * specific redux state). Called by {@link OverlayContainer}.
  55. *
  56. * @param {Object} state - The redux state.
  57. * @returns {boolean} - If this overlay needs to be rendered, {@code true};
  58. * {@code false}, otherwise.
  59. */
  60. static needsRender(state) {
  61. const conferenceError = state['features/base/conference'].error;
  62. const configError = state['features/base/config'].error;
  63. const connectionError = state['features/base/connection'].error;
  64. return (
  65. (connectionError && isFatalJitsiConnectionError(connectionError))
  66. || (conferenceError
  67. && isFatalJitsiConferenceError(conferenceError))
  68. || configError
  69. );
  70. }
  71. _interval: ?number
  72. state: {
  73. /**
  74. * The translation key for the title of the overlay.
  75. *
  76. * @type {string}
  77. */
  78. message: string,
  79. /**
  80. * Current value(time) of the timer.
  81. *
  82. * @type {number}
  83. */
  84. timeLeft: number,
  85. /**
  86. * How long the overlay dialog will be displayed before the
  87. * conference will be reloaded.
  88. *
  89. * @type {number}
  90. */
  91. timeoutSeconds: number,
  92. /**
  93. * The translation key for the title of the overlay.
  94. *
  95. * @type {string}
  96. */
  97. title: string
  98. }
  99. /**
  100. * Initializes a new AbstractPageReloadOverlay instance.
  101. *
  102. * @param {Object} props - The read-only properties with which the new
  103. * instance is to be initialized.
  104. * @public
  105. */
  106. constructor(props: Object) {
  107. super(props);
  108. /**
  109. * How long the overlay dialog will be displayed, before the conference
  110. * will be reloaded.
  111. *
  112. * @type {number}
  113. */
  114. const timeoutSeconds = 10 + randomInt(0, 20);
  115. let message, title;
  116. if (this.props.isNetworkFailure) {
  117. title = 'dialog.conferenceDisconnectTitle';
  118. message = 'dialog.conferenceDisconnectMsg';
  119. } else {
  120. title = 'dialog.conferenceReloadTitle';
  121. message = 'dialog.conferenceReloadMsg';
  122. }
  123. this.state = {
  124. message,
  125. timeLeft: timeoutSeconds,
  126. timeoutSeconds,
  127. title
  128. };
  129. }
  130. /**
  131. * React Component method that executes once component is mounted.
  132. *
  133. * @inheritdoc
  134. * @returns {void}
  135. */
  136. componentDidMount() {
  137. // FIXME (CallStats - issue) This event will not make it to CallStats
  138. // because the log queue is not flushed before "fabric terminated" is
  139. // sent to the backed.
  140. // FIXME: We should dispatch action for this.
  141. if (typeof APP !== 'undefined') {
  142. if (APP.conference && APP.conference._room) {
  143. APP.conference._room.sendApplicationLog(JSON.stringify(
  144. {
  145. name: 'page.reload',
  146. label: this.props.reason
  147. }));
  148. }
  149. }
  150. sendAnalytics(createPageReloadScheduledEvent(
  151. this.props.reason, this.state.timeoutSeconds));
  152. logger.info(
  153. `The conference will be reloaded after ${
  154. this.state.timeoutSeconds} seconds.`);
  155. this._interval
  156. = setInterval(
  157. () => {
  158. if (this.state.timeLeft === 0) {
  159. if (this._interval) {
  160. clearInterval(this._interval);
  161. this._interval = undefined;
  162. }
  163. this.props.dispatch(_reloadNow());
  164. } else {
  165. this.setState(prevState => {
  166. return {
  167. timeLeft: prevState.timeLeft - 1
  168. };
  169. });
  170. }
  171. },
  172. 1000);
  173. }
  174. /**
  175. * Clears the timer interval.
  176. *
  177. * @inheritdoc
  178. * @returns {void}
  179. */
  180. componentWillUnmount() {
  181. if (this._interval) {
  182. clearInterval(this._interval);
  183. this._interval = undefined;
  184. }
  185. }
  186. /**
  187. * Renders the button for relaod the page if necessary.
  188. *
  189. * @protected
  190. * @returns {ReactElement|null}
  191. */
  192. _renderButton() {
  193. if (this.props.isNetworkFailure) {
  194. return (
  195. <ReloadButton textKey = 'dialog.rejoinNow' />
  196. );
  197. }
  198. return null;
  199. }
  200. /**
  201. * Renders the progress bar.
  202. *
  203. * @protected
  204. * @returns {ReactElement}
  205. */
  206. _renderProgressBar() {
  207. const { timeLeft, timeoutSeconds } = this.state;
  208. const timeRemaining = timeoutSeconds - timeLeft;
  209. const percentageComplete
  210. = Math.floor((timeRemaining / timeoutSeconds) * 100);
  211. return (
  212. <div
  213. className = 'progress-indicator'
  214. id = 'reloadProgressBar'>
  215. <div
  216. className = 'progress-indicator-fill'
  217. style = {{ width: `${percentageComplete}%` }} />
  218. </div>
  219. );
  220. }
  221. }
  222. /**
  223. * Maps (parts of) the redux state to the associated component's props.
  224. *
  225. * @param {Object} state - The redux state.
  226. * @protected
  227. * @returns {{
  228. * isNetworkFailure: boolean,
  229. * reason: string
  230. * }}
  231. */
  232. export function abstractMapStateToProps(state: Object) {
  233. const conferenceError = state['features/base/conference'].error;
  234. const configError = state['features/base/config'].error;
  235. const connectionError = state['features/base/connection'].error;
  236. return {
  237. isNetworkFailure: Boolean(configError || connectionError),
  238. reason: (configError || connectionError || conferenceError).message
  239. };
  240. }