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

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218
  1. import React, { Component } from 'react';
  2. import { appNavigate } from '../../app';
  3. import { isRoomValid } from '../../base/conference';
  4. import { VideoTrack } from '../../base/media';
  5. import { getLocalVideoTrack } from '../../base/tracks';
  6. import { generateRoomWithoutSeparator } from '../../base/util';
  7. /**
  8. * Base (abstract) class for container component rendering the welcome page.
  9. *
  10. * @abstract
  11. */
  12. export class AbstractWelcomePage extends Component {
  13. /**
  14. * AbstractWelcomePage component's property types.
  15. *
  16. * @static
  17. */
  18. static propTypes = {
  19. _localVideoTrack: React.PropTypes.object,
  20. _room: React.PropTypes.string,
  21. dispatch: React.PropTypes.func
  22. }
  23. /**
  24. * Initializes a new AbstractWelcomePage instance, including the initial
  25. * state of the room name input.
  26. *
  27. * @param {Object} props - Component properties.
  28. */
  29. constructor(props) {
  30. super(props);
  31. /**
  32. * Save room name into component's local state.
  33. *
  34. * @type {Object}
  35. * @property {number|null} animateTimeoutId - Identificator for
  36. * letter animation timeout.
  37. * @property {string} generatedRoomname - Automatically generated
  38. * room name.
  39. * @property {string} room - Room name.
  40. * @property {string} roomPlaceholder - Room placeholder
  41. * that's used as a placeholder for input.
  42. * @property {nubmer|null} updateTimeoutId - Identificator for
  43. * updating generated room name.
  44. */
  45. this.state = {
  46. animateTimeoutId: null,
  47. generatedRoomname: '',
  48. room: '',
  49. roomPlaceholder: '',
  50. updateTimeoutId: null
  51. };
  52. // Bind event handlers so they are only bound once for every instance.
  53. this._animateRoomnameChanging
  54. = this._animateRoomnameChanging.bind(this);
  55. this._onJoin = this._onJoin.bind(this);
  56. this._onRoomChange = this._onRoomChange.bind(this);
  57. this._updateRoomname = this._updateRoomname.bind(this);
  58. }
  59. /**
  60. * This method is executed when component receives new properties.
  61. *
  62. * @inheritdoc
  63. * @param {Object} nextProps - New props component will receive.
  64. */
  65. componentWillReceiveProps(nextProps) {
  66. this.setState({ room: nextProps._room });
  67. }
  68. /**
  69. * This method is executed when method will be unmounted from DOM.
  70. *
  71. * @inheritdoc
  72. */
  73. componentWillUnmount() {
  74. this._clearTimeouts();
  75. }
  76. /**
  77. * Animates the changing of the room name.
  78. *
  79. * @param {string} word - The part of room name that should be added to
  80. * placeholder.
  81. * @private
  82. * @returns {void}
  83. */
  84. _animateRoomnameChanging(word) {
  85. let animateTimeoutId = null;
  86. const roomPlaceholder = this.state.roomPlaceholder + word.substr(0, 1);
  87. if (word.length > 1) {
  88. animateTimeoutId
  89. = setTimeout(
  90. () => {
  91. this._animateRoomnameChanging(
  92. word.substring(1, word.length));
  93. },
  94. 70);
  95. }
  96. this.setState({
  97. animateTimeoutId,
  98. roomPlaceholder
  99. });
  100. }
  101. /**
  102. * Method that clears timeouts for animations and updates of room name.
  103. *
  104. * @private
  105. * @returns {void}
  106. */
  107. _clearTimeouts() {
  108. clearTimeout(this.state.animateTimeoutId);
  109. clearTimeout(this.state.updateTimeoutId);
  110. }
  111. /**
  112. * Determines whether the 'Join' button is (to be) disabled i.e. there's no
  113. * valid room name typed into the respective text input field.
  114. *
  115. * @protected
  116. * @returns {boolean} If the 'Join' button is (to be) disabled, true;
  117. * otherwise, false.
  118. */
  119. _isJoinDisabled() {
  120. return !isRoomValid(this.state.room);
  121. }
  122. /**
  123. * Handles joining. Either by clicking on 'Join' button
  124. * or by pressing 'Enter' in room name input field.
  125. *
  126. * @protected
  127. * @returns {void}
  128. */
  129. _onJoin() {
  130. const room = this.state.room || this.state.generatedRoomname;
  131. if (room) {
  132. this.props.dispatch(appNavigate(room));
  133. }
  134. }
  135. /**
  136. * Handles 'change' event for the room name text input field.
  137. *
  138. * @param {string} value - The text typed into the respective text input
  139. * field.
  140. * @protected
  141. * @returns {void}
  142. */
  143. _onRoomChange(value) {
  144. this.setState({ room: value });
  145. }
  146. /**
  147. * Renders a local video if any.
  148. *
  149. * @protected
  150. * @returns {(ReactElement|null)}
  151. */
  152. _renderLocalVideo() {
  153. return (
  154. <VideoTrack videoTrack = { this.props._localVideoTrack } />
  155. );
  156. }
  157. /**
  158. * Triggers the generation of a new room name and initiates an animation of
  159. * its changing.
  160. *
  161. * @protected
  162. * @returns {void}
  163. */
  164. _updateRoomname() {
  165. const generatedRoomname = generateRoomWithoutSeparator();
  166. const roomPlaceholder = '';
  167. const updateTimeoutId = setTimeout(this._updateRoomname, 10000);
  168. this._clearTimeouts();
  169. this.setState(
  170. {
  171. generatedRoomname,
  172. roomPlaceholder,
  173. updateTimeoutId
  174. },
  175. () => this._animateRoomnameChanging(generatedRoomname));
  176. }
  177. }
  178. /**
  179. * Selects local video track from tracks in state, local participant and room
  180. * and maps them to component props. It seems it's not possible to 'connect'
  181. * base component and then extend from it. So we export this function in order
  182. * to be used in child classes for 'connect'.
  183. *
  184. * @param {Object} state - Redux state.
  185. * @protected
  186. * @returns {{
  187. * _localVideoTrack: (Track|undefined),
  188. * _room: string
  189. * }}
  190. */
  191. export function _mapStateToProps(state) {
  192. const conference = state['features/base/conference'];
  193. const tracks = state['features/base/tracks'];
  194. return {
  195. _localVideoTrack: getLocalVideoTrack(tracks),
  196. _room: conference.room
  197. };
  198. }