您最多选择25个主题 主题必须以字母或数字开头,可以包含连字符 (-),并且长度不得超过35个字符

AbstractPageReloadOverlay.js 6.7KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260
  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 abstract React Component for the page reload overlays.
  15. */
  16. export default class AbstractPageReloadOverlay extends Component<*, *> {
  17. /**
  18. * 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. _interval: ?number
  49. state: {
  50. /**
  51. * The translation key for the title of the overlay.
  52. *
  53. * @type {string}
  54. */
  55. message: string,
  56. /**
  57. * Current value(time) of the timer.
  58. *
  59. * @type {number}
  60. */
  61. timeLeft: number,
  62. /**
  63. * How long the overlay dialog will be displayed before the
  64. * conference will be reloaded.
  65. *
  66. * @type {number}
  67. */
  68. timeoutSeconds: number,
  69. /**
  70. * The translation key for the title of the overlay.
  71. *
  72. * @type {string}
  73. */
  74. title: string
  75. }
  76. /**
  77. * Check if this overlay needs to be rendered. This function will be called
  78. * by the {@code OverlayContainer}.
  79. *
  80. * @param {Object} state - The redux state.
  81. * @returns {boolean} - True if this overlay needs to be rendered, false
  82. * otherwise.
  83. */
  84. static needsRender(state) {
  85. const conferenceError = state['features/base/conference'].error;
  86. const connectionError = state['features/base/connection'].error;
  87. return (
  88. (connectionError && isFatalJitsiConnectionError(connectionError))
  89. || (conferenceError
  90. && isFatalJitsiConferenceError(conferenceError))
  91. );
  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 { timeoutSeconds, timeLeft } = this.state;
  195. const timeRemaining = timeoutSeconds - timeLeft;
  196. const percentageComplete = Math.floor(
  197. (timeRemaining / timeoutSeconds) * 100);
  198. return (
  199. <div
  200. className = 'progress-indicator'
  201. id = 'reloadProgressBar'>
  202. <div
  203. className = 'progress-indicator-fill'
  204. style = {{
  205. width: `${percentageComplete}%`
  206. }} />
  207. </div>
  208. );
  209. }
  210. }
  211. /**
  212. * Maps (parts of) the redux state to the associated component's props.
  213. *
  214. * @param {Object} state - The redux state.
  215. * @returns {{
  216. * isNetworkFailure: boolean,
  217. * reason: string
  218. * }}
  219. * @protected
  220. */
  221. export function abstractMapStateToProps(state: Object) {
  222. const conferenceError = state['features/base/conference'].error;
  223. const connectionError = state['features/base/connection'].error;
  224. return {
  225. isNetworkFailure: Boolean(connectionError),
  226. reason: (connectionError || conferenceError).message
  227. };
  228. }