Вы не можете выбрать более 25 тем Темы должны начинаться с буквы или цифры, могут содержать дефисы(-) и должны содержать не более 35 символов.

AbstractWelcomePage.js 6.6KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249
  1. // @flow
  2. import { generateRoomWithoutSeparator } from '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';
  7. import { isRoomValid } from '../../base/conference';
  8. /**
  9. * {@code AbstractWelcomePage}'s React {@code Component} prop types.
  10. */
  11. type Props = {
  12. /**
  13. * Room name to join to.
  14. */
  15. _room: string,
  16. /**
  17. * The current settings.
  18. */
  19. _settings: Object,
  20. /**
  21. * The Redux dispatch Function.
  22. */
  23. dispatch: Dispatch<any>
  24. };
  25. /**
  26. * Base (abstract) class for container component rendering the welcome page.
  27. *
  28. * @abstract
  29. */
  30. export class AbstractWelcomePage extends Component<Props, *> {
  31. _mounted: ?boolean;
  32. /**
  33. * Implements React's {@link Component#getDerivedStateFromProps()}.
  34. *
  35. * @inheritdoc
  36. */
  37. static getDerivedStateFromProps(props: Props, state: Object) {
  38. return {
  39. room: props._room || state.room
  40. };
  41. }
  42. /**
  43. * Save room name into component's local state.
  44. *
  45. * @type {Object}
  46. * @property {number|null} animateTimeoutId - Identifier of the letter
  47. * animation timeout.
  48. * @property {string} generatedRoomname - Automatically generated room name.
  49. * @property {string} room - Room name.
  50. * @property {string} roomPlaceholder - Room placeholder that's used as a
  51. * placeholder for input.
  52. * @property {nubmer|null} updateTimeoutId - Identifier of the timeout
  53. * updating the generated room name.
  54. */
  55. state = {
  56. animateTimeoutId: undefined,
  57. generatedRoomname: '',
  58. joining: false,
  59. room: '',
  60. roomPlaceholder: '',
  61. updateTimeoutId: undefined
  62. };
  63. /**
  64. * Initializes a new {@code AbstractWelcomePage} instance.
  65. *
  66. * @param {Props} props - The React {@code Component} props to initialize
  67. * the new {@code AbstractWelcomePage} instance with.
  68. */
  69. constructor(props: Props) {
  70. super(props);
  71. // Bind event handlers so they are only bound once per instance.
  72. this._animateRoomnameChanging
  73. = this._animateRoomnameChanging.bind(this);
  74. this._onJoin = this._onJoin.bind(this);
  75. this._onRoomChange = this._onRoomChange.bind(this);
  76. this._updateRoomname = this._updateRoomname.bind(this);
  77. }
  78. /**
  79. * Implements React's {@link Component#componentDidMount()}. Invoked
  80. * immediately after mounting occurs.
  81. *
  82. * @inheritdoc
  83. */
  84. componentDidMount() {
  85. this._mounted = true;
  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. sendAnalytics(
  155. createWelcomePageEvent('clicked', 'joinButton', {
  156. isGenerated: !this.state.room,
  157. room
  158. }));
  159. if (room) {
  160. this.setState({ joining: true });
  161. // By the time the Promise of appNavigate settles, this component
  162. // may have already been unmounted.
  163. const onAppNavigateSettled
  164. = () => this._mounted && this.setState({ joining: false });
  165. this.props.dispatch(appNavigate(room))
  166. .then(onAppNavigateSettled, onAppNavigateSettled);
  167. }
  168. }
  169. _onRoomChange: (string) => void;
  170. /**
  171. * Handles 'change' event for the room name text input field.
  172. *
  173. * @param {string} value - The text typed into the respective text input
  174. * field.
  175. * @protected
  176. * @returns {void}
  177. */
  178. _onRoomChange(value: string) {
  179. this.setState({ room: value });
  180. }
  181. _updateRoomname: () => void;
  182. /**
  183. * Triggers the generation of a new room name and initiates an animation of
  184. * its changing.
  185. *
  186. * @protected
  187. * @returns {void}
  188. */
  189. _updateRoomname() {
  190. const generatedRoomname = generateRoomWithoutSeparator();
  191. const roomPlaceholder = '';
  192. const updateTimeoutId = setTimeout(this._updateRoomname, 10000);
  193. this._clearTimeouts();
  194. this.setState(
  195. {
  196. generatedRoomname,
  197. roomPlaceholder,
  198. updateTimeoutId
  199. },
  200. () => this._animateRoomnameChanging(generatedRoomname));
  201. }
  202. }
  203. /**
  204. * Maps (parts of) the redux state to the React {@code Component} props of
  205. * {@code AbstractWelcomePage}.
  206. *
  207. * @param {Object} state - The redux state.
  208. * @protected
  209. * @returns {{
  210. * _room: string,
  211. * _settings: Object
  212. * }}
  213. */
  214. export function _mapStateToProps(state: Object) {
  215. return {
  216. _room: state['features/base/conference'].room,
  217. _settings: state['features/base/settings']
  218. };
  219. }