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

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