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

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