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

AbstractPageReloadOverlay.js 7.7KB

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