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

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281
  1. // @ts-expect-error
  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. = connectionError && isFatalJitsiConnectionError(connectionError);
  88. const jitsiConferenceError
  89. = conferenceError && isFatalJitsiConferenceError(conferenceError);
  90. return jitsiConnectionError || jitsiConferenceError || configError;
  91. }
  92. _interval: number | undefined;
  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: P) {
  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: We should dispatch action for this.
  132. if (typeof APP !== 'undefined' && APP.conference?._room) {
  133. APP.conference._room.sendApplicationLog(JSON.stringify({
  134. name: 'page.reload',
  135. label: this.props.reason
  136. }));
  137. }
  138. sendAnalytics(createPageReloadScheduledEvent(
  139. this.props.reason ?? '',
  140. this.state.timeoutSeconds,
  141. this.props.details));
  142. logger.info(
  143. `The conference will be reloaded after ${
  144. this.state.timeoutSeconds} seconds.`);
  145. this._interval
  146. = window.setInterval(
  147. () => {
  148. if (this.state.timeLeft === 0) {
  149. if (this._interval) {
  150. clearInterval(this._interval);
  151. this._interval = undefined;
  152. }
  153. this.props.dispatch(reloadNow());
  154. } else {
  155. this.setState(prevState => {
  156. return {
  157. timeLeft: prevState.timeLeft - 1
  158. };
  159. });
  160. }
  161. },
  162. 1000);
  163. }
  164. /**
  165. * Clears the timer interval.
  166. *
  167. * @inheritdoc
  168. * @returns {void}
  169. */
  170. componentWillUnmount() {
  171. if (this._interval) {
  172. clearInterval(this._interval);
  173. this._interval = undefined;
  174. }
  175. }
  176. /**
  177. * Renders the button for reloading the page if necessary.
  178. *
  179. * @protected
  180. * @returns {ReactElement|null}
  181. */
  182. _renderButton() {
  183. if (this.props.isNetworkFailure) {
  184. return (
  185. <ReloadButton textKey = 'dialog.rejoinNow' />
  186. );
  187. }
  188. return null;
  189. }
  190. /**
  191. * Renders the progress bar.
  192. *
  193. * @protected
  194. * @returns {ReactElement}
  195. */
  196. _renderProgressBar() {
  197. const { timeLeft, timeoutSeconds } = this.state;
  198. const timeRemaining = timeoutSeconds - timeLeft;
  199. const percentageComplete
  200. = Math.floor((timeRemaining / timeoutSeconds) * 100);
  201. return (
  202. <div
  203. className = 'progress-indicator'
  204. id = 'reloadProgressBar'>
  205. <div
  206. className = 'progress-indicator-fill'
  207. style = {{ width: `${percentageComplete}%` }} />
  208. </div>
  209. );
  210. }
  211. }
  212. /**
  213. * Maps (parts of) the redux state to the associated component's props.
  214. *
  215. * @param {Object} state - The redux state.
  216. * @protected
  217. * @returns {{
  218. * details: Object,
  219. * error: ?Error,
  220. * isNetworkFailure: boolean,
  221. * reason: string
  222. * }}
  223. */
  224. export function abstractMapStateToProps(state: IReduxState) {
  225. const { error: configError } = state['features/base/config'];
  226. const { error: connectionError } = state['features/base/connection'];
  227. const { error: conferenceError } = state['features/base/conference'];
  228. const error = configError || connectionError || conferenceError;
  229. let reason;
  230. if (conferenceError) {
  231. reason = `error.conference.${conferenceError.name}`;
  232. } else if (configError) {
  233. reason = `error.config.${configError.name}`;
  234. } else if (connectionError) {
  235. reason = `error.connection.${connectionError.name}`;
  236. } else {
  237. logger.error('No reload reason defined!');
  238. }
  239. return {
  240. details: undefined, // TODO: revisit this.
  241. error,
  242. isNetworkFailure: Boolean(configError || connectionError),
  243. reason
  244. };
  245. }