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

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