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

AbstractWelcomePage.js 7.5KB

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