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

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291
  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. import ReloadButton from './ReloadButton';
  15. /**
  16. * The type of the React {@code Component} props of
  17. * {@link AbstractPageReloadOverlay}.
  18. */
  19. export interface IProps extends WithTranslation {
  20. /**
  21. * The details is an object containing more information about the connection
  22. * failed (shard changes, was the computer suspended, etc.).
  23. */
  24. details?: Object;
  25. /**
  26. * Redux dispatch function.
  27. */
  28. dispatch: IStore['dispatch'];
  29. /**
  30. * The error that caused the display of the overlay.
  31. */
  32. error?: any;
  33. /**
  34. * The indicator which determines whether the reload was caused by network
  35. * failure.
  36. */
  37. isNetworkFailure: boolean;
  38. /**
  39. * The reason for the error that will cause the reload.
  40. * NOTE: Used by PageReloadOverlay only.
  41. */
  42. reason?: string;
  43. }
  44. /**
  45. * The type of the React {@code Component} state of
  46. * {@link AbstractPageReloadOverlay}.
  47. */
  48. interface IState {
  49. /**
  50. * The translation key for the title of the overlay.
  51. */
  52. message: string;
  53. /**
  54. * Current value(time) of the timer.
  55. */
  56. timeLeft: number;
  57. /**
  58. * How long the overlay dialog will be displayed before the conference will
  59. * be reloaded.
  60. */
  61. timeoutSeconds: number;
  62. /**
  63. * The translation key for the title of the overlay.
  64. */
  65. title: string;
  66. }
  67. /**
  68. * Implements an abstract React {@link Component} for the page reload overlays.
  69. *
  70. * FIXME: This is not really an abstract class as some components and functions are very web specific.
  71. */
  72. export default class AbstractPageReloadOverlay<P extends IProps>
  73. extends Component<P, IState> {
  74. /**
  75. * Determines whether this overlay needs to be rendered (according to a
  76. * specific redux state). Called by {@link OverlayContainer}.
  77. *
  78. * @param {Object} state - The redux state.
  79. * @returns {boolean} - If this overlay needs to be rendered, {@code true};
  80. * {@code false}, otherwise.
  81. */
  82. static needsRender(state: IReduxState) {
  83. const { error: conferenceError } = state['features/base/conference'];
  84. const { error: configError } = state['features/base/config'];
  85. const { error: connectionError } = state['features/base/connection'];
  86. const jitsiConnectionError
  87. // @ts-ignore
  88. = connectionError && isFatalJitsiConnectionError(connectionError);
  89. const jitsiConferenceError
  90. = conferenceError && isFatalJitsiConferenceError(conferenceError);
  91. return jitsiConnectionError || jitsiConferenceError || configError;
  92. }
  93. _interval: number | undefined;
  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: P) {
  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. if (typeof APP !== 'undefined' && APP.conference?._room) {
  137. APP.conference._room.sendApplicationLog(JSON.stringify({
  138. name: 'page.reload',
  139. label: this.props.reason
  140. }));
  141. }
  142. sendAnalytics(createPageReloadScheduledEvent(
  143. this.props.reason ?? '',
  144. this.state.timeoutSeconds,
  145. this.props.details));
  146. logger.info(
  147. `The conference will be reloaded after ${
  148. this.state.timeoutSeconds} seconds.`);
  149. this._interval
  150. = window.setInterval(
  151. () => {
  152. if (this.state.timeLeft === 0) {
  153. if (this._interval) {
  154. clearInterval(this._interval);
  155. this._interval = undefined;
  156. }
  157. this.props.dispatch(reloadNow());
  158. } else {
  159. this.setState(prevState => {
  160. return {
  161. timeLeft: prevState.timeLeft - 1
  162. };
  163. });
  164. }
  165. },
  166. 1000);
  167. }
  168. /**
  169. * Clears the timer interval.
  170. *
  171. * @inheritdoc
  172. * @returns {void}
  173. */
  174. componentWillUnmount() {
  175. if (this._interval) {
  176. clearInterval(this._interval);
  177. this._interval = undefined;
  178. }
  179. }
  180. /**
  181. * Renders the button for reloading the page if necessary.
  182. *
  183. * @protected
  184. * @returns {ReactElement|null}
  185. */
  186. _renderButton() {
  187. if (this.props.isNetworkFailure) {
  188. return (
  189. <ReloadButton textKey = 'dialog.rejoinNow' />
  190. );
  191. }
  192. return null;
  193. }
  194. /**
  195. * Renders the progress bar.
  196. *
  197. * @protected
  198. * @returns {ReactElement}
  199. */
  200. _renderProgressBar() {
  201. const { timeLeft, timeoutSeconds } = this.state;
  202. const timeRemaining = timeoutSeconds - timeLeft;
  203. const percentageComplete
  204. = Math.floor((timeRemaining / timeoutSeconds) * 100);
  205. return (
  206. <div
  207. className = 'progress-indicator'
  208. id = 'reloadProgressBar'>
  209. <div
  210. className = 'progress-indicator-fill'
  211. style = {{ width: `${percentageComplete}%` }} />
  212. </div>
  213. );
  214. }
  215. }
  216. /**
  217. * Maps (parts of) the redux state to the associated component's props.
  218. *
  219. * @param {Object} state - The redux state.
  220. * @protected
  221. * @returns {{
  222. * details: Object,
  223. * error: ?Error,
  224. * isNetworkFailure: boolean,
  225. * reason: string
  226. * }}
  227. */
  228. export function abstractMapStateToProps(state: IReduxState) {
  229. const { error: configError } = state['features/base/config'];
  230. const { error: connectionError } = state['features/base/connection'];
  231. const { fatalError } = state['features/overlay'];
  232. let reason = fatalError && (fatalError.message || fatalError.name);
  233. if (!reason) {
  234. const { error: conferenceError } = state['features/base/conference'];
  235. if (conferenceError) {
  236. reason = `error.conference.${conferenceError.name}`;
  237. } else if (configError) {
  238. reason = `error.config.${configError.name}`;
  239. } else if (connectionError) {
  240. reason = `error.connection.${connectionError.name}`;
  241. } else {
  242. logger.error('No reload reason defined!');
  243. }
  244. }
  245. return {
  246. details: fatalError?.details,
  247. error: fatalError,
  248. isNetworkFailure:
  249. fatalError === configError || fatalError === connectionError,
  250. reason
  251. };
  252. }