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

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