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

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226
  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 type { Dispatch } from 'redux';
  7. import { appNavigate, reloadNow } from '../../../../app/actions.native';
  8. import { IReduxState } from '../../../../app/types';
  9. import { translate } from '../../../i18n/functions';
  10. import { isFatalJitsiConnectionError } from '../../../lib-jitsi-meet/functions.native';
  11. import { connect } from '../../../redux/functions';
  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: Dispatch<any>;
  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. message: string;
  31. timeLeft: number;
  32. timeoutSeconds: number;
  33. title: string;
  34. }
  35. /**
  36. * Implements a React Component that is shown before the
  37. * conference is reloaded.
  38. * Shows a warning message and counts down towards the re-load.
  39. */
  40. class PageReloadDialog extends Component<IPageReloadDialogProps, IPageReloadDialogState> {
  41. // @ts-ignore
  42. _interval: IntervalID;
  43. /**
  44. * Initializes a new PageReloadOverlay instance.
  45. *
  46. * @param {Object} props - The read-only properties with which the new
  47. * instance is to be initialized.
  48. * @public
  49. */
  50. constructor(props: IPageReloadDialogProps) {
  51. super(props);
  52. const timeoutSeconds = 10 + randomInt(0, 20);
  53. let message, title;
  54. if (this.props.isNetworkFailure) {
  55. title = 'dialog.conferenceDisconnectTitle';
  56. message = 'dialog.conferenceDisconnectMsg';
  57. } else {
  58. title = 'dialog.conferenceReloadTitle';
  59. message = 'dialog.conferenceReloadMsg';
  60. }
  61. this.state = {
  62. message,
  63. timeLeft: timeoutSeconds,
  64. timeoutSeconds,
  65. title
  66. };
  67. this._onCancel = this._onCancel.bind(this);
  68. this._onReloadNow = this._onReloadNow.bind(this);
  69. }
  70. /**
  71. * React Component method that executes once component is mounted.
  72. *
  73. * @inheritdoc
  74. * @returns {void}
  75. */
  76. componentDidMount() {
  77. const { dispatch } = this.props;
  78. const { timeLeft } = this.state;
  79. logger.info(
  80. `The conference will be reloaded after ${
  81. this.state.timeoutSeconds} seconds.`);
  82. this._interval
  83. = setInterval(
  84. () => {
  85. if (timeLeft === 0) {
  86. if (this._interval) {
  87. clearInterval(this._interval);
  88. this._interval = undefined;
  89. }
  90. dispatch(reloadNow());
  91. } else {
  92. this.setState(prevState => {
  93. return {
  94. timeLeft: prevState.timeLeft - 1
  95. };
  96. });
  97. }
  98. },
  99. 1000);
  100. }
  101. /**
  102. * Clears the timer interval.
  103. *
  104. * @inheritdoc
  105. * @returns {void}
  106. */
  107. componentWillUnmount() {
  108. if (this._interval) {
  109. clearInterval(this._interval);
  110. this._interval = undefined;
  111. }
  112. }
  113. /**
  114. * Handle clicking of the "Cancel" button. It will navigate back to the
  115. * welcome page.
  116. *
  117. * @private
  118. * @returns {boolean}
  119. */
  120. _onCancel() {
  121. clearInterval(this._interval);
  122. this.props.dispatch(appNavigate(undefined));
  123. return true;
  124. }
  125. /**
  126. * Handle clicking on the "Reload Now" button. It will navigate to the same
  127. * conference URL as before immediately, without waiting for the timer to
  128. * kick in.
  129. *
  130. * @private
  131. * @returns {boolean}
  132. */
  133. _onReloadNow() {
  134. clearInterval(this._interval);
  135. this.props.dispatch(reloadNow());
  136. return true;
  137. }
  138. /**
  139. * Implements React's {@link Component#render()}.
  140. *
  141. * @inheritdoc
  142. * @returns {ReactElement}
  143. */
  144. render() {
  145. const { t } = this.props;
  146. const { message, timeLeft, title } = this.state;
  147. return (
  148. <ConfirmDialog
  149. cancelLabel = 'dialog.Cancel'
  150. confirmLabel = 'dialog.rejoinNow'
  151. descriptionKey = { `${t(message, { seconds: timeLeft })}` }
  152. onCancel = { this._onCancel }
  153. onSubmit = { this._onReloadNow }
  154. title = { title } />
  155. );
  156. }
  157. }
  158. /**
  159. * Maps (parts of) the redux state to the associated component's props.
  160. *
  161. * @param {Object} state - The redux state.
  162. * @protected
  163. * @returns {{
  164. * message: string,
  165. * reason: string,
  166. * title: 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 = 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));