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

AbstractWelcomePage.js 6.7KB

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