Nelze vybrat více než 25 témat Téma musí začínat písmenem nebo číslem, může obsahovat pomlčky („-“) a může být dlouhé až 35 znaků.

AbstractPageReloadOverlay.tsx 8.1KB

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