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

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290
  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 ReloadButton from './ReloadButton';
  15. declare var APP: Object;
  16. const logger = require('jitsi-meet-logger').getLogger(__filename);
  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. export default class AbstractPageReloadOverlay<P: Props>
  70. extends Component<P, State> {
  71. /**
  72. * Determines whether this overlay needs to be rendered (according to a
  73. * specific redux state). Called by {@link OverlayContainer}.
  74. *
  75. * @param {Object} state - The redux state.
  76. * @returns {boolean} - If this overlay needs to be rendered, {@code true};
  77. * {@code false}, otherwise.
  78. */
  79. static needsRender(state: Object) {
  80. // FIXME web does not rely on the 'recoverable' flag set on an error
  81. // action, but on a predefined list of fatal errors. Because of that
  82. // the value of 'fatalError' which relies on the flag should not be used
  83. // on web yet (until conference/connection and their errors handling is
  84. // not unified).
  85. return typeof APP === 'undefined'
  86. ? Boolean(state['features/overlay'].fatalError)
  87. : this.needsRenderWeb(state);
  88. }
  89. /**
  90. * Determines whether this overlay needs to be rendered (according to a
  91. * specific redux state). Called by {@link OverlayContainer}.
  92. *
  93. * @param {Object} state - The redux state.
  94. * @returns {boolean} - If this overlay needs to be rendered, {@code true};
  95. * {@code false}, otherwise.
  96. */
  97. static needsRenderWeb(state: Object) {
  98. const conferenceError = state['features/base/conference'].error;
  99. const configError = state['features/base/config'].error;
  100. const connectionError = state['features/base/connection'].error;
  101. return (
  102. (connectionError && isFatalJitsiConnectionError(connectionError))
  103. || (conferenceError
  104. && isFatalJitsiConferenceError(conferenceError))
  105. || configError);
  106. }
  107. _interval: ?IntervalID;
  108. /**
  109. * Initializes a new AbstractPageReloadOverlay instance.
  110. *
  111. * @param {Object} props - The read-only properties with which the new
  112. * instance is to be initialized.
  113. * @public
  114. */
  115. constructor(props: P) {
  116. super(props);
  117. /**
  118. * How long the overlay dialog will be displayed, before the conference
  119. * will be reloaded.
  120. *
  121. * @type {number}
  122. */
  123. const timeoutSeconds = 10 + randomInt(0, 20);
  124. let message, title;
  125. if (this.props.isNetworkFailure) {
  126. title = 'dialog.conferenceDisconnectTitle';
  127. message = 'dialog.conferenceDisconnectMsg';
  128. } else {
  129. title = 'dialog.conferenceReloadTitle';
  130. message = 'dialog.conferenceReloadMsg';
  131. }
  132. this.state = {
  133. message,
  134. timeLeft: timeoutSeconds,
  135. timeoutSeconds,
  136. title
  137. };
  138. }
  139. /**
  140. * React Component method that executes once component is mounted.
  141. *
  142. * @inheritdoc
  143. * @returns {void}
  144. */
  145. componentDidMount() {
  146. // FIXME (CallStats - issue) This event will not make it to CallStats
  147. // because the log queue is not flushed before "fabric terminated" is
  148. // sent to the backed.
  149. // FIXME: We should dispatch action for this.
  150. if (typeof APP !== 'undefined') {
  151. if (APP.conference && APP.conference._room) {
  152. APP.conference._room.sendApplicationLog(JSON.stringify({
  153. name: 'page.reload',
  154. label: this.props.reason
  155. }));
  156. }
  157. }
  158. sendAnalytics(createPageReloadScheduledEvent(
  159. this.props.reason,
  160. this.state.timeoutSeconds,
  161. this.props.details));
  162. logger.info(
  163. `The conference will be reloaded after ${
  164. this.state.timeoutSeconds} seconds.`);
  165. this._interval
  166. = setInterval(
  167. () => {
  168. if (this.state.timeLeft === 0) {
  169. if (this._interval) {
  170. clearInterval(this._interval);
  171. this._interval = undefined;
  172. }
  173. this.props.dispatch(reloadNow());
  174. } else {
  175. this.setState(prevState => {
  176. return {
  177. timeLeft: prevState.timeLeft - 1
  178. };
  179. });
  180. }
  181. },
  182. 1000);
  183. }
  184. /**
  185. * Clears the timer interval.
  186. *
  187. * @inheritdoc
  188. * @returns {void}
  189. */
  190. componentWillUnmount() {
  191. if (this._interval) {
  192. clearInterval(this._interval);
  193. this._interval = undefined;
  194. }
  195. }
  196. /**
  197. * Renders the button for reloading the page if necessary.
  198. *
  199. * @protected
  200. * @returns {ReactElement|null}
  201. */
  202. _renderButton() {
  203. if (this.props.isNetworkFailure) {
  204. return (
  205. <ReloadButton textKey = 'dialog.rejoinNow' />
  206. );
  207. }
  208. return null;
  209. }
  210. /**
  211. * Renders the progress bar.
  212. *
  213. * @protected
  214. * @returns {ReactElement}
  215. */
  216. _renderProgressBar() {
  217. const { timeLeft, timeoutSeconds } = this.state;
  218. const timeRemaining = timeoutSeconds - timeLeft;
  219. const percentageComplete
  220. = Math.floor((timeRemaining / timeoutSeconds) * 100);
  221. return (
  222. <div
  223. className = 'progress-indicator'
  224. id = 'reloadProgressBar'>
  225. <div
  226. className = 'progress-indicator-fill'
  227. style = {{ width: `${percentageComplete}%` }} />
  228. </div>
  229. );
  230. }
  231. }
  232. /**
  233. * Maps (parts of) the redux state to the associated component's props.
  234. *
  235. * @param {Object} state - The redux state.
  236. * @protected
  237. * @returns {{
  238. * details: Object,
  239. * isNetworkFailure: boolean,
  240. * reason: string
  241. * }}
  242. */
  243. export function abstractMapStateToProps(state: Object) {
  244. const { error: configError } = state['features/base/config'];
  245. const { error: connectionError } = state['features/base/connection'];
  246. const { fatalError } = state['features/overlay'];
  247. return {
  248. details: fatalError && fatalError.details,
  249. isNetworkFailure:
  250. fatalError === configError || fatalError === connectionError,
  251. reason: fatalError && fatalError.message
  252. };
  253. }