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.

PageReloadDialog.tsx 5.9KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230
  1. /* eslint-disable lines-around-comment */
  2. // @ts-ignore
  3. import { randomInt } from '@jitsi/js-utils/random';
  4. import React, { Component } from 'react';
  5. import { WithTranslation } from 'react-i18next';
  6. import { connect } from 'react-redux';
  7. import { appNavigate, reloadNow } from '../../../../app/actions.native';
  8. import { IReduxState, IStore } from '../../../../app/types';
  9. import { translate } from '../../../i18n/functions';
  10. import { isFatalJitsiConnectionError } from '../../../lib-jitsi-meet/functions.native';
  11. import { hideDialog } from '../../actions';
  12. // @ts-ignore
  13. import logger from '../../logger';
  14. // @ts-ignore
  15. import ConfirmDialog from './ConfirmDialog';
  16. /**
  17. * The type of the React {@code Component} props of
  18. * {@link PageReloadDialog}.
  19. */
  20. interface IPageReloadDialogProps extends WithTranslation {
  21. dispatch: IStore['dispatch'];
  22. isNetworkFailure: boolean;
  23. reason?: string;
  24. }
  25. /**
  26. * The type of the React {@code Component} state of
  27. * {@link PageReloadDialog}.
  28. */
  29. interface IPageReloadDialogState {
  30. timeLeft: number;
  31. }
  32. /**
  33. * Implements a React Component that is shown before the
  34. * conference is reloaded.
  35. * Shows a warning message and counts down towards the re-load.
  36. */
  37. class PageReloadDialog extends Component<IPageReloadDialogProps, IPageReloadDialogState> {
  38. // @ts-ignore
  39. _interval: IntervalID;
  40. _timeoutSeconds: number;
  41. /**
  42. * Initializes a new PageReloadOverlay instance.
  43. *
  44. * @param {Object} props - The read-only properties with which the new
  45. * instance is to be initialized.
  46. * @public
  47. */
  48. constructor(props: IPageReloadDialogProps) {
  49. super(props);
  50. this._timeoutSeconds = 10 + randomInt(0, 20);
  51. this.state = {
  52. timeLeft: this._timeoutSeconds
  53. };
  54. this._onCancel = this._onCancel.bind(this);
  55. this._onReloadNow = this._onReloadNow.bind(this);
  56. this._onReconnecting = this._onReconnecting.bind(this);
  57. }
  58. /**
  59. * React Component method that executes once component is mounted.
  60. *
  61. * @inheritdoc
  62. * @returns {void}
  63. */
  64. componentDidMount() {
  65. const { timeLeft } = this.state;
  66. logger.info(
  67. `The conference will be reloaded after ${timeLeft} seconds.`
  68. );
  69. this._interval = setInterval(() =>
  70. this._onReconnecting(), 1000);
  71. }
  72. /**
  73. * Clears the timer interval.
  74. *
  75. * @inheritdoc
  76. * @returns {void}
  77. */
  78. componentWillUnmount() {
  79. if (this._interval) {
  80. clearInterval(this._interval);
  81. this._interval = undefined;
  82. }
  83. }
  84. /**
  85. * Handle clicking of the "Cancel" button. It will navigate back to the
  86. * welcome page.
  87. *
  88. * @private
  89. * @returns {boolean}
  90. */
  91. _onCancel() {
  92. const { dispatch } = this.props;
  93. clearInterval(this._interval);
  94. dispatch(appNavigate(undefined));
  95. return true;
  96. }
  97. /**
  98. * Handles automatic reconnection.
  99. *
  100. * @private
  101. * @returns {void}
  102. */
  103. _onReconnecting() {
  104. const { dispatch } = this.props;
  105. const { timeLeft } = this.state;
  106. if (timeLeft === 0) {
  107. if (this._interval) {
  108. dispatch(hideDialog());
  109. this._onReloadNow();
  110. this._interval = undefined;
  111. }
  112. }
  113. this.setState({
  114. timeLeft: timeLeft - 1
  115. });
  116. }
  117. /**
  118. * Handle clicking on the "Reload Now" button. It will navigate to the same
  119. * conference URL as before immediately, without waiting for the timer to
  120. * kick in.
  121. *
  122. * @private
  123. * @returns {boolean}
  124. */
  125. _onReloadNow() {
  126. const { dispatch } = this.props;
  127. clearInterval(this._interval);
  128. dispatch(reloadNow());
  129. return true;
  130. }
  131. /**
  132. * Implements React's {@link Component#render()}.
  133. *
  134. * @inheritdoc
  135. * @returns {ReactElement}
  136. */
  137. render() {
  138. const { isNetworkFailure, t } = this.props;
  139. const { timeLeft } = this.state;
  140. let message, title;
  141. if (isNetworkFailure) {
  142. title = 'dialog.conferenceDisconnectTitle';
  143. message = 'dialog.conferenceDisconnectMsg';
  144. } else {
  145. title = 'dialog.conferenceReloadTitle';
  146. message = 'dialog.conferenceReloadMsg';
  147. }
  148. return (
  149. <ConfirmDialog
  150. cancelLabel = 'dialog.Cancel'
  151. confirmLabel = 'dialog.rejoinNow'
  152. descriptionKey = { `${t(message, { seconds: timeLeft })}` }
  153. onCancel = { this._onCancel }
  154. onSubmit = { this._onReloadNow }
  155. title = { title } />
  156. );
  157. }
  158. }
  159. /**
  160. * Maps (parts of) the redux state to the associated component's props.
  161. *
  162. * @param {Object} state - The redux state.
  163. * @protected
  164. * @returns {{
  165. * isNetworkFailure: boolean,
  166. * reason: string
  167. * }}
  168. */
  169. function mapStateToProps(state: IReduxState) {
  170. const { error: conferenceError } = state['features/base/conference'];
  171. const { error: configError } = state['features/base/config'];
  172. const { error: connectionError } = state['features/base/connection'];
  173. const { fatalError } = state['features/overlay'];
  174. const fatalConnectionError
  175. // @ts-ignore
  176. = connectionError && isFatalJitsiConnectionError(connectionError);
  177. const fatalConfigError = fatalError === configError;
  178. const isNetworkFailure = Boolean(fatalConfigError || fatalConnectionError);
  179. let reason;
  180. if (conferenceError) {
  181. reason = `error.conference.${conferenceError.name}`;
  182. } else if (connectionError) {
  183. reason = `error.conference.${connectionError.name}`;
  184. } else if (configError) {
  185. reason = `error.config.${configError.name}`;
  186. } else {
  187. logger.error('No reload reason defined!');
  188. }
  189. return {
  190. isNetworkFailure,
  191. reason
  192. };
  193. }
  194. export default translate(connect(mapStateToProps)(PageReloadDialog));