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

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305
  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. /**
  28. * The details is an object containing more information about the
  29. * connection failed (shard changes, was the computer suspended, etc.)
  30. *
  31. * @public
  32. * @type {object}
  33. */
  34. details: PropTypes.object,
  35. dispatch: PropTypes.func,
  36. /**
  37. * The indicator which determines whether the reload was caused by
  38. * network failure.
  39. *
  40. * @public
  41. * @type {boolean}
  42. */
  43. isNetworkFailure: PropTypes.bool,
  44. /**
  45. * The reason for the error that will cause the reload.
  46. * NOTE: Used by PageReloadOverlay only.
  47. *
  48. * @public
  49. * @type {string}
  50. */
  51. reason: PropTypes.string,
  52. /**
  53. * The function to translate human-readable text.
  54. *
  55. * @public
  56. * @type {Function}
  57. */
  58. t: PropTypes.func
  59. };
  60. /**
  61. * Determines whether this overlay needs to be rendered (according to a
  62. * specific redux state). Called by {@link OverlayContainer}.
  63. *
  64. * @param {Object} state - The redux state.
  65. * @returns {boolean} - If this overlay needs to be rendered, {@code true};
  66. * {@code false}, otherwise.
  67. */
  68. static needsRender(state: Object) {
  69. // FIXME web does not rely on the 'recoverable' flag set on an error
  70. // action, but on a predefined list of fatal errors. Because of that
  71. // the value of 'fatalError' which relies on the flag should not be used
  72. // on web yet (until conference/connection and their errors handling is
  73. // not unified).
  74. return typeof APP === 'undefined'
  75. ? Boolean(state['features/overlay'].fatalError)
  76. : this.needsRenderWeb(state);
  77. }
  78. /**
  79. * Determines whether this overlay needs to be rendered (according to a
  80. * specific redux state). Called by {@link OverlayContainer}.
  81. *
  82. * @param {Object} state - The redux state.
  83. * @returns {boolean} - If this overlay needs to be rendered, {@code true};
  84. * {@code false}, otherwise.
  85. */
  86. static needsRenderWeb(state: Object) {
  87. const conferenceError = state['features/base/conference'].error;
  88. const configError = state['features/base/config'].error;
  89. const connectionError = state['features/base/connection'].error;
  90. return (
  91. (connectionError && isFatalJitsiConnectionError(connectionError))
  92. || (conferenceError
  93. && isFatalJitsiConferenceError(conferenceError))
  94. || configError);
  95. }
  96. _interval: ?IntervalID;
  97. state: {
  98. /**
  99. * The translation key for the title of the overlay.
  100. *
  101. * @type {string}
  102. */
  103. message: string,
  104. /**
  105. * Current value(time) of the timer.
  106. *
  107. * @type {number}
  108. */
  109. timeLeft: number,
  110. /**
  111. * How long the overlay dialog will be displayed before the
  112. * conference will be reloaded.
  113. *
  114. * @type {number}
  115. */
  116. timeoutSeconds: number,
  117. /**
  118. * The translation key for the title of the overlay.
  119. *
  120. * @type {string}
  121. */
  122. title: string
  123. };
  124. /**
  125. * Initializes a new AbstractPageReloadOverlay instance.
  126. *
  127. * @param {Object} props - The read-only properties with which the new
  128. * instance is to be initialized.
  129. * @public
  130. */
  131. constructor(props: Object) {
  132. super(props);
  133. /**
  134. * How long the overlay dialog will be displayed, before the conference
  135. * will be reloaded.
  136. *
  137. * @type {number}
  138. */
  139. const timeoutSeconds = 10 + randomInt(0, 20);
  140. let message, title;
  141. if (this.props.isNetworkFailure) {
  142. title = 'dialog.conferenceDisconnectTitle';
  143. message = 'dialog.conferenceDisconnectMsg';
  144. } else {
  145. title = 'dialog.conferenceReloadTitle';
  146. message = 'dialog.conferenceReloadMsg';
  147. }
  148. this.state = {
  149. message,
  150. timeLeft: timeoutSeconds,
  151. timeoutSeconds,
  152. title
  153. };
  154. }
  155. /**
  156. * React Component method that executes once component is mounted.
  157. *
  158. * @inheritdoc
  159. * @returns {void}
  160. */
  161. componentDidMount() {
  162. // FIXME (CallStats - issue) This event will not make it to CallStats
  163. // because the log queue is not flushed before "fabric terminated" is
  164. // sent to the backed.
  165. // FIXME: We should dispatch action for this.
  166. if (typeof APP !== 'undefined') {
  167. if (APP.conference && APP.conference._room) {
  168. APP.conference._room.sendApplicationLog(JSON.stringify({
  169. name: 'page.reload',
  170. label: this.props.reason
  171. }));
  172. }
  173. }
  174. sendAnalytics(createPageReloadScheduledEvent(
  175. this.props.reason,
  176. this.state.timeoutSeconds,
  177. this.props.details));
  178. logger.info(
  179. `The conference will be reloaded after ${
  180. this.state.timeoutSeconds} seconds.`);
  181. this._interval
  182. = setInterval(
  183. () => {
  184. if (this.state.timeLeft === 0) {
  185. if (this._interval) {
  186. clearInterval(this._interval);
  187. this._interval = undefined;
  188. }
  189. this.props.dispatch(_reloadNow());
  190. } else {
  191. this.setState(prevState => {
  192. return {
  193. timeLeft: prevState.timeLeft - 1
  194. };
  195. });
  196. }
  197. },
  198. 1000);
  199. }
  200. /**
  201. * Clears the timer interval.
  202. *
  203. * @inheritdoc
  204. * @returns {void}
  205. */
  206. componentWillUnmount() {
  207. if (this._interval) {
  208. clearInterval(this._interval);
  209. this._interval = undefined;
  210. }
  211. }
  212. /**
  213. * Renders the button for reloading the page if necessary.
  214. *
  215. * @protected
  216. * @returns {ReactElement|null}
  217. */
  218. _renderButton() {
  219. if (this.props.isNetworkFailure) {
  220. return (
  221. <ReloadButton textKey = 'dialog.rejoinNow' />
  222. );
  223. }
  224. return null;
  225. }
  226. /**
  227. * Renders the progress bar.
  228. *
  229. * @protected
  230. * @returns {ReactElement}
  231. */
  232. _renderProgressBar() {
  233. const { timeLeft, timeoutSeconds } = this.state;
  234. const timeRemaining = timeoutSeconds - timeLeft;
  235. const percentageComplete
  236. = Math.floor((timeRemaining / timeoutSeconds) * 100);
  237. return (
  238. <div
  239. className = 'progress-indicator'
  240. id = 'reloadProgressBar'>
  241. <div
  242. className = 'progress-indicator-fill'
  243. style = {{ width: `${percentageComplete}%` }} />
  244. </div>
  245. );
  246. }
  247. }
  248. /**
  249. * Maps (parts of) the redux state to the associated component's props.
  250. *
  251. * @param {Object} state - The redux state.
  252. * @protected
  253. * @returns {{
  254. * details: Object,
  255. * isNetworkFailure: boolean,
  256. * reason: string
  257. * }}
  258. */
  259. export function abstractMapStateToProps(state: Object) {
  260. const { error: configError } = state['features/base/config'];
  261. const { error: connectionError } = state['features/base/connection'];
  262. const { fatalError } = state['features/overlay'];
  263. return {
  264. details: fatalError && fatalError.details,
  265. isNetworkFailure:
  266. fatalError === configError || fatalError === connectionError,
  267. reason: fatalError && fatalError.message
  268. };
  269. }