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 6.5KB

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