Nelze vybrat více než 25 témat Téma musí začínat písmenem nebo číslem, může obsahovat pomlčky („-“) a může být dlouhé až 35 znaků.

AbstractWelcomePage.ts 7.9KB

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