Вы не можете выбрать более 25 тем Темы должны начинаться с буквы или цифры, могут содержать дефисы(-) и должны содержать не более 35 символов.

AbstractWelcomePage.ts 8.6KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297
  1. // @ts-expect-error
  2. import { generateRoomWithoutSeparator } from '@jitsi/js-utils/random';
  3. import { Component } from 'react';
  4. import { WithTranslation } from 'react-i18next';
  5. import { createWelcomePageEvent } from '../../analytics/AnalyticsEvents';
  6. import { sendAnalytics } from '../../analytics/functions';
  7. import { appNavigate } from '../../app/actions';
  8. import { IReduxState, IStore } from '../../app/types';
  9. import { IDeeplinkingConfig } from '../../base/config/configType';
  10. import isInsecureRoomName from '../../base/util/isInsecureRoomName';
  11. import { isCalendarEnabled } from '../../calendar-sync/functions';
  12. import { isRecentListEnabled } from '../../recent-list/functions';
  13. /**
  14. * {@code AbstractWelcomePage}'s React {@code Component} prop types.
  15. */
  16. export interface IProps extends WithTranslation {
  17. /**
  18. * Whether the calendar functionality is enabled or not.
  19. */
  20. _calendarEnabled: boolean;
  21. /**
  22. * The deeplinking config.
  23. */
  24. _deeplinkingCfg: IDeeplinkingConfig;
  25. /**
  26. * Whether the insecure room name functionality is enabled or not.
  27. */
  28. _enableInsecureRoomNameWarning: boolean;
  29. /**
  30. * URL for the moderated rooms microservice, if available.
  31. */
  32. _moderatedRoomServiceUrl?: string;
  33. /**
  34. * Whether the recent list is enabled.
  35. */
  36. _recentListEnabled: Boolean;
  37. /**
  38. * Room name to join to.
  39. */
  40. _room: string;
  41. /**
  42. * The current settings.
  43. */
  44. _settings: Object;
  45. /**
  46. * The Redux dispatch Function.
  47. */
  48. dispatch: IStore['dispatch'];
  49. }
  50. interface IState {
  51. _fieldFocused?: boolean;
  52. animateTimeoutId?: number;
  53. generateRoomNames?: string;
  54. generatedRoomName: string;
  55. hintBoxAnimation?: any;
  56. insecureRoomName: boolean;
  57. isSettingsScreenFocused?: boolean;
  58. joining: boolean;
  59. room: string;
  60. roomNameInputAnimation?: any;
  61. roomPlaceholder: string;
  62. updateTimeoutId?: number;
  63. }
  64. /**
  65. * Base (abstract) class for container component rendering the welcome page.
  66. *
  67. * @abstract
  68. */
  69. export class AbstractWelcomePage<P extends IProps> extends Component<P, IState> {
  70. _mounted: boolean | undefined;
  71. /**
  72. * Save room name into component's local state.
  73. *
  74. * @type {Object}
  75. * @property {number|null} animateTimeoutId - Identifier of the letter
  76. * animation timeout.
  77. * @property {string} generatedRoomName - Automatically generated room name.
  78. * @property {string} room - Room name.
  79. * @property {string} roomPlaceholder - Room placeholder that's used as a
  80. * placeholder for input.
  81. * @property {number|null} updateTimeoutId - Identifier of the timeout
  82. * updating the generated room name.
  83. */
  84. state: IState = {
  85. animateTimeoutId: undefined,
  86. generatedRoomName: '',
  87. generateRoomNames: undefined,
  88. insecureRoomName: false,
  89. joining: false,
  90. room: '',
  91. roomPlaceholder: '',
  92. updateTimeoutId: undefined,
  93. _fieldFocused: false,
  94. isSettingsScreenFocused: false,
  95. roomNameInputAnimation: 0,
  96. hintBoxAnimation: 0
  97. };
  98. /**
  99. * Initializes a new {@code AbstractWelcomePage} instance.
  100. *
  101. * @param {Props} props - The React {@code Component} props to initialize
  102. * the new {@code AbstractWelcomePage} instance with.
  103. */
  104. constructor(props: P) {
  105. super(props);
  106. // Bind event handlers so they are only bound once per instance.
  107. this._animateRoomNameChanging
  108. = this._animateRoomNameChanging.bind(this);
  109. this._onJoin = this._onJoin.bind(this);
  110. this._onRoomChange = this._onRoomChange.bind(this);
  111. this._renderInsecureRoomNameWarning = this._renderInsecureRoomNameWarning.bind(this);
  112. this._updateRoomName = this._updateRoomName.bind(this);
  113. }
  114. /**
  115. * Implements React's {@link Component#componentDidMount()}. Invoked
  116. * immediately after mounting occurs.
  117. *
  118. * @inheritdoc
  119. */
  120. componentDidMount() {
  121. this._mounted = true;
  122. sendAnalytics(createWelcomePageEvent('viewed', undefined, { value: 1 }));
  123. }
  124. /**
  125. * Implements React's {@link Component#componentWillUnmount()}. Invoked
  126. * immediately before this component is unmounted and destroyed.
  127. *
  128. * @inheritdoc
  129. */
  130. componentWillUnmount() {
  131. this._clearTimeouts();
  132. this._mounted = false;
  133. }
  134. /**
  135. * Animates the changing of the room name.
  136. *
  137. * @param {string} word - The part of room name that should be added to
  138. * placeholder.
  139. * @private
  140. * @returns {void}
  141. */
  142. _animateRoomNameChanging(word: string) {
  143. let animateTimeoutId;
  144. const roomPlaceholder = this.state.roomPlaceholder + word.substr(0, 1);
  145. if (word.length > 1) {
  146. animateTimeoutId
  147. = window.setTimeout(
  148. () => {
  149. this._animateRoomNameChanging(
  150. word.substring(1, word.length));
  151. },
  152. 70);
  153. }
  154. this.setState({
  155. animateTimeoutId,
  156. roomPlaceholder
  157. });
  158. }
  159. /**
  160. * Method that clears timeouts for animations and updates of room name.
  161. *
  162. * @private
  163. * @returns {void}
  164. */
  165. _clearTimeouts() {
  166. this.state.animateTimeoutId && clearTimeout(this.state.animateTimeoutId);
  167. this.state.updateTimeoutId && clearTimeout(this.state.updateTimeoutId);
  168. }
  169. /**
  170. * Renders the insecure room name warning.
  171. *
  172. * @returns {ReactElement}
  173. */
  174. _doRenderInsecureRoomNameWarning(): JSX.Element | null {
  175. return null;
  176. }
  177. /**
  178. * Handles joining. Either by clicking on 'Join' button
  179. * or by pressing 'Enter' in room name input field.
  180. *
  181. * @protected
  182. * @returns {void}
  183. */
  184. _onJoin() {
  185. const room = this.state.room || this.state.generatedRoomName;
  186. sendAnalytics(
  187. createWelcomePageEvent('clicked', 'joinButton', {
  188. isGenerated: !this.state.room,
  189. room
  190. }));
  191. if (room) {
  192. this.setState({ joining: true });
  193. // By the time the Promise of appNavigate settles, this component
  194. // may have already been unmounted.
  195. const onAppNavigateSettled
  196. = () => this._mounted && this.setState({ joining: false });
  197. this.props.dispatch(appNavigate(room))
  198. .then(onAppNavigateSettled, onAppNavigateSettled);
  199. }
  200. }
  201. /**
  202. * Handles 'change' event for the room name text input field.
  203. *
  204. * @param {string} value - The text typed into the respective text input
  205. * field.
  206. * @protected
  207. * @returns {void}
  208. */
  209. _onRoomChange(value: string) {
  210. this.setState({
  211. room: value,
  212. insecureRoomName: Boolean(this.props._enableInsecureRoomNameWarning && value && isInsecureRoomName(value))
  213. });
  214. }
  215. /**
  216. * Renders the insecure room name warning if needed.
  217. *
  218. * @returns {ReactElement}
  219. */
  220. _renderInsecureRoomNameWarning() {
  221. if (this.props._enableInsecureRoomNameWarning && this.state.insecureRoomName) {
  222. return this._doRenderInsecureRoomNameWarning();
  223. }
  224. return null;
  225. }
  226. /**
  227. * Triggers the generation of a new room name and initiates an animation of
  228. * its changing.
  229. *
  230. * @protected
  231. * @returns {void}
  232. */
  233. _updateRoomName() {
  234. const generatedRoomName = generateRoomWithoutSeparator();
  235. const roomPlaceholder = '';
  236. const updateTimeoutId = window.setTimeout(this._updateRoomName, 10000);
  237. this._clearTimeouts();
  238. this.setState(
  239. {
  240. generatedRoomName,
  241. roomPlaceholder,
  242. updateTimeoutId
  243. },
  244. () => this._animateRoomNameChanging(generatedRoomName));
  245. }
  246. }
  247. /**
  248. * Maps (parts of) the redux state to the React {@code Component} props of
  249. * {@code AbstractWelcomePage}.
  250. *
  251. * @param {Object} state - The redux state.
  252. * @protected
  253. * @returns {IProps}
  254. */
  255. export function _mapStateToProps(state: IReduxState) {
  256. return {
  257. _calendarEnabled: isCalendarEnabled(state),
  258. _deeplinkingCfg: state['features/base/config'].deeplinking || {},
  259. _enableInsecureRoomNameWarning: state['features/base/config'].enableInsecureRoomNameWarning || false,
  260. _moderatedRoomServiceUrl: state['features/base/config'].moderatedRoomServiceUrl,
  261. _recentListEnabled: isRecentListEnabled(),
  262. _room: state['features/base/conference'].room ?? '',
  263. _settings: state['features/base/settings']
  264. };
  265. }