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 7.7KB

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