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

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