Você não pode selecionar mais de 25 tópicos Os tópicos devem começar com uma letra ou um número, podem incluir traços ('-') e podem ter até 35 caracteres.

AbstractWelcomePage.js 6.5KB

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