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.0KB

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