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

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