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

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