您最多选择25个主题 主题必须以字母或数字开头,可以包含连字符 (-),并且长度不得超过35个字符

AbstractWelcomePage.js 6.8KB

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