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 7.8KB

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