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

PageReloadDialog.tsx 5.9KB

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