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

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