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.

AbstractWelcomePage.ts 8.6KB

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