Nevar pievienot vairāk kā 25 tēmas Tēmai ir jāsākas ar burtu vai ciparu, tā var saturēt domu zīmes ('-') un var būt līdz 35 simboliem gara.

AbstractWelcomePage.js 6.9KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258
  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. sendAnalytics(createWelcomePageEvent('viewed', undefined, { value: 1 }));
  92. }
  93. /**
  94. * Implements React's {@link Component#componentWillUnmount()}. Invoked
  95. * immediately before this component is unmounted and destroyed.
  96. *
  97. * @inheritdoc
  98. */
  99. componentWillUnmount() {
  100. this._clearTimeouts();
  101. this._mounted = false;
  102. }
  103. _animateRoomnameChanging: (string) => void;
  104. /**
  105. * Animates the changing of the room name.
  106. *
  107. * @param {string} word - The part of room name that should be added to
  108. * placeholder.
  109. * @private
  110. * @returns {void}
  111. */
  112. _animateRoomnameChanging(word: string) {
  113. let animateTimeoutId;
  114. const roomPlaceholder = this.state.roomPlaceholder + word.substr(0, 1);
  115. if (word.length > 1) {
  116. animateTimeoutId
  117. = setTimeout(
  118. () => {
  119. this._animateRoomnameChanging(
  120. word.substring(1, word.length));
  121. },
  122. 70);
  123. }
  124. this.setState({
  125. animateTimeoutId,
  126. roomPlaceholder
  127. });
  128. }
  129. /**
  130. * Method that clears timeouts for animations and updates of room name.
  131. *
  132. * @private
  133. * @returns {void}
  134. */
  135. _clearTimeouts() {
  136. clearTimeout(this.state.animateTimeoutId);
  137. clearTimeout(this.state.updateTimeoutId);
  138. }
  139. /**
  140. * Determines whether the 'Join' button is (to be) disabled i.e. There's no
  141. * valid room name typed into the respective text input field.
  142. *
  143. * @protected
  144. * @returns {boolean} If the 'Join' button is (to be) disabled, true;
  145. * otherwise, false.
  146. */
  147. _isJoinDisabled() {
  148. return this.state.joining || !isRoomValid(this.state.room);
  149. }
  150. _onJoin: () => void;
  151. /**
  152. * Handles joining. Either by clicking on 'Join' button
  153. * or by pressing 'Enter' in room name input field.
  154. *
  155. * @protected
  156. * @returns {void}
  157. */
  158. _onJoin() {
  159. const room = this.state.room || this.state.generatedRoomname;
  160. sendAnalytics(
  161. createWelcomePageEvent('clicked', 'joinButton', {
  162. isGenerated: !this.state.room,
  163. room
  164. }));
  165. if (room) {
  166. this.setState({ joining: true });
  167. // By the time the Promise of appNavigate settles, this component
  168. // may have already been unmounted.
  169. const onAppNavigateSettled
  170. = () => this._mounted && this.setState({ joining: false });
  171. this.props.dispatch(appNavigate(room))
  172. .then(onAppNavigateSettled, onAppNavigateSettled);
  173. }
  174. }
  175. _onRoomChange: (string) => void;
  176. /**
  177. * Handles 'change' event for the room name text input field.
  178. *
  179. * @param {string} value - The text typed into the respective text input
  180. * field.
  181. * @protected
  182. * @returns {void}
  183. */
  184. _onRoomChange(value: string) {
  185. this.setState({ room: value });
  186. }
  187. _updateRoomname: () => void;
  188. /**
  189. * Triggers the generation of a new room name and initiates an animation of
  190. * its changing.
  191. *
  192. * @protected
  193. * @returns {void}
  194. */
  195. _updateRoomname() {
  196. const generatedRoomname = generateRoomWithoutSeparator();
  197. const roomPlaceholder = '';
  198. const updateTimeoutId = setTimeout(this._updateRoomname, 10000);
  199. this._clearTimeouts();
  200. this.setState(
  201. {
  202. generatedRoomname,
  203. roomPlaceholder,
  204. updateTimeoutId
  205. },
  206. () => this._animateRoomnameChanging(generatedRoomname));
  207. }
  208. }
  209. /**
  210. * Maps (parts of) the redux state to the React {@code Component} props of
  211. * {@code AbstractWelcomePage}.
  212. *
  213. * @param {Object} state - The redux state.
  214. * @protected
  215. * @returns {{
  216. * _calendarEnabled: boolean,
  217. * _room: string,
  218. * _settings: Object
  219. * }}
  220. */
  221. export function _mapStateToProps(state: Object) {
  222. return {
  223. _calendarEnabled: isCalendarEnabled(state),
  224. _room: state['features/base/conference'].room,
  225. _settings: state['features/base/settings']
  226. };
  227. }