您最多选择25个主题 主题必须以字母或数字开头,可以包含连字符 (-),并且长度不得超过35个字符

PageReloadDialog.tsx 5.8KB

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