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.js 7.7KB

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