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

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