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.

AbstractLobbyScreen.tsx 12KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448
  1. import React, { PureComponent } from 'react';
  2. import { IReduxState } from '../../app/types';
  3. import { conferenceWillJoin } from '../../base/conference/actions';
  4. import { getConferenceName } from '../../base/conference/functions';
  5. import { IJitsiConference } from '../../base/conference/reducer';
  6. import { getSecurityUiConfig } from '../../base/config/functions.any';
  7. import { INVITE_ENABLED } from '../../base/flags/constants';
  8. import { getFeatureFlag } from '../../base/flags/functions';
  9. import { getLocalParticipant } from '../../base/participants/functions';
  10. import { getFieldValue } from '../../base/react/functions';
  11. import { updateSettings } from '../../base/settings/actions';
  12. import { isDeviceStatusVisible } from '../../prejoin/functions';
  13. import { cancelKnocking, joinWithPassword, onSendMessage, setPasswordJoinFailed, startKnocking } from '../actions';
  14. export const SCREEN_STATES = {
  15. EDIT: 1,
  16. PASSWORD: 2,
  17. VIEW: 3
  18. };
  19. export interface IProps {
  20. /**
  21. * Indicates whether the device status should be visible.
  22. */
  23. _deviceStatusVisible: boolean;
  24. /**
  25. * True if moderator initiated a chat session with the participant.
  26. */
  27. _isLobbyChatActive: boolean;
  28. /**
  29. * True if knocking is already happening, so we're waiting for a response.
  30. */
  31. _knocking: boolean;
  32. /**
  33. * Lobby messages between moderator and the participant.
  34. */
  35. _lobbyChatMessages: Object;
  36. /**
  37. * Name of the lobby chat recipient.
  38. */
  39. _lobbyMessageRecipient: string;
  40. /**
  41. * The name of the meeting we're about to join.
  42. */
  43. _meetingName: string;
  44. /**
  45. * The members only conference if any,.
  46. */
  47. _membersOnlyConference: IJitsiConference;
  48. /**
  49. * The email of the participant about to knock/join.
  50. */
  51. _participantEmail: string;
  52. /**
  53. * The id of the participant about to knock/join. This is the participant ID in the lobby room, at this point.
  54. */
  55. _participantId: string;
  56. /**
  57. * The name of the participant about to knock/join.
  58. */
  59. _participantName: string;
  60. /**
  61. * True if a recent attempt to join with password failed.
  62. */
  63. _passwordJoinFailed: boolean;
  64. /**
  65. * True if the password field should be available for lobby participants.
  66. */
  67. _renderPassword: boolean;
  68. /**
  69. * The Redux dispatch function.
  70. */
  71. dispatch: Function;
  72. /**
  73. * Indicates whether the copy url button should be shown.
  74. */
  75. showCopyUrlButton: boolean;
  76. /**
  77. * Function to be used to translate i18n labels.
  78. */
  79. t: Function;
  80. }
  81. interface IState {
  82. /**
  83. * The display name value entered into the field.
  84. */
  85. displayName: string;
  86. /**
  87. * The email value entered into the field.
  88. */
  89. email: string;
  90. /**
  91. * True if lobby chat widget is open.
  92. */
  93. isChatOpen: boolean;
  94. /**
  95. * The password value entered into the field.
  96. */
  97. password: string;
  98. /**
  99. * True if a recent attempt to join with password failed.
  100. */
  101. passwordJoinFailed: boolean;
  102. /**
  103. * The state of the screen. One of {@code SCREEN_STATES[*]}.
  104. */
  105. screenState: number;
  106. }
  107. /**
  108. * Abstract class to encapsulate the platform common code of the {@code LobbyScreen}.
  109. */
  110. export default class AbstractLobbyScreen<P extends IProps = IProps> extends PureComponent<P, IState> {
  111. /**
  112. * Instantiates a new component.
  113. *
  114. * @inheritdoc
  115. */
  116. constructor(props: P) {
  117. super(props);
  118. this.state = {
  119. displayName: props._participantName || '',
  120. email: props._participantEmail || '',
  121. isChatOpen: true,
  122. password: '',
  123. passwordJoinFailed: false,
  124. screenState: props._participantName ? SCREEN_STATES.VIEW : SCREEN_STATES.EDIT
  125. };
  126. this._onAskToJoin = this._onAskToJoin.bind(this);
  127. this._onCancel = this._onCancel.bind(this);
  128. this._onChangeDisplayName = this._onChangeDisplayName.bind(this);
  129. this._onChangeEmail = this._onChangeEmail.bind(this);
  130. this._onChangePassword = this._onChangePassword.bind(this);
  131. this._onEnableEdit = this._onEnableEdit.bind(this);
  132. this._onJoinWithPassword = this._onJoinWithPassword.bind(this);
  133. this._onSendMessage = this._onSendMessage.bind(this);
  134. this._onSwitchToKnockMode = this._onSwitchToKnockMode.bind(this);
  135. this._onSwitchToPasswordMode = this._onSwitchToPasswordMode.bind(this);
  136. this._onToggleChat = this._onToggleChat.bind(this);
  137. }
  138. /**
  139. * Implements {@code PureComponent.getDerivedStateFromProps}.
  140. *
  141. * @inheritdoc
  142. */
  143. static getDerivedStateFromProps(props: IProps, state: IState) {
  144. if (props._passwordJoinFailed && !state.passwordJoinFailed) {
  145. return {
  146. password: '',
  147. passwordJoinFailed: true
  148. };
  149. }
  150. return null;
  151. }
  152. /**
  153. * Returns the screen title.
  154. *
  155. * @returns {string}
  156. */
  157. _getScreenTitleKey() {
  158. const { screenState } = this.state;
  159. const passwordPrompt = screenState === SCREEN_STATES.PASSWORD;
  160. return !passwordPrompt && this.props._knocking
  161. ? this.props._isLobbyChatActive ? 'lobby.lobbyChatStartedTitle' : 'lobby.joiningTitle'
  162. : passwordPrompt ? 'lobby.enterPasswordTitle' : 'lobby.joinTitle';
  163. }
  164. /**
  165. * Callback to be invoked when the user submits the joining request.
  166. *
  167. * @returns {void}
  168. */
  169. _onAskToJoin() {
  170. this.setState({
  171. password: ''
  172. });
  173. this.props.dispatch(startKnocking());
  174. return false;
  175. }
  176. /**
  177. * Callback to be invoked when the user cancels the dialog.
  178. *
  179. * @private
  180. * @returns {boolean}
  181. */
  182. _onCancel() {
  183. this.props.dispatch(cancelKnocking());
  184. return true;
  185. }
  186. /**
  187. * Callback to be invoked when the user changes its display name.
  188. *
  189. * @param {SyntheticEvent} event - The SyntheticEvent instance of the change.
  190. * @returns {void}
  191. */
  192. _onChangeDisplayName(event: { target: { value: string; }; } | string) {
  193. const displayName = getFieldValue(event);
  194. this.setState({
  195. displayName
  196. }, () => {
  197. this.props.dispatch(updateSettings({
  198. displayName
  199. }));
  200. });
  201. }
  202. /**
  203. * Callback to be invoked when the user changes its email.
  204. *
  205. * @param {SyntheticEvent} event - The SyntheticEvent instance of the change.
  206. * @returns {void}
  207. */
  208. _onChangeEmail(event: { target: { value: string; }; } | string) {
  209. const email = getFieldValue(event);
  210. this.setState({
  211. email
  212. }, () => {
  213. this.props.dispatch(updateSettings({
  214. email
  215. }));
  216. });
  217. }
  218. /**
  219. * Callback to be invoked when the user changes the password.
  220. *
  221. * @param {SyntheticEvent} event - The SyntheticEvent instance of the change.
  222. * @returns {void}
  223. */
  224. _onChangePassword(event: { target: { value: string; }; } | string) {
  225. this.setState({
  226. password: getFieldValue(event)
  227. });
  228. }
  229. /**
  230. * Callback to be invoked for the edit button.
  231. *
  232. * @returns {void}
  233. */
  234. _onEnableEdit() {
  235. this.setState({
  236. screenState: SCREEN_STATES.EDIT
  237. });
  238. }
  239. /**
  240. * Callback to be invoked when the user tries to join using a preset password.
  241. *
  242. * @returns {void}
  243. */
  244. _onJoinWithPassword() {
  245. this.setState({
  246. passwordJoinFailed: false
  247. });
  248. this.props.dispatch(joinWithPassword(this.state.password));
  249. }
  250. /**
  251. * Callback to be invoked for sending lobby chat messages.
  252. *
  253. * @param {string} message - Message to be sent.
  254. * @returns {void}
  255. */
  256. _onSendMessage(message: string) {
  257. this.props.dispatch(onSendMessage(message));
  258. }
  259. /**
  260. * Callback to be invoked for the enter (go back to) knocking mode button.
  261. *
  262. * @returns {void}
  263. */
  264. _onSwitchToKnockMode() {
  265. this.setState({
  266. password: '',
  267. screenState: this.state.displayName ? SCREEN_STATES.VIEW : SCREEN_STATES.EDIT
  268. });
  269. this.props.dispatch(setPasswordJoinFailed(false));
  270. // let's return to the correct state after password failed
  271. this.props.dispatch(conferenceWillJoin(this.props._membersOnlyConference));
  272. }
  273. /**
  274. * Callback to be invoked for the enter password button.
  275. *
  276. * @returns {void}
  277. */
  278. _onSwitchToPasswordMode() {
  279. this.setState({
  280. screenState: SCREEN_STATES.PASSWORD
  281. });
  282. }
  283. /**
  284. * Callback to be invoked for toggling lobby chat visibility.
  285. *
  286. * @returns {void}
  287. */
  288. _onToggleChat() {
  289. this.setState(_prevState => {
  290. return {
  291. isChatOpen: !_prevState.isChatOpen
  292. };
  293. });
  294. }
  295. /**
  296. * Renders the content of the dialog.
  297. *
  298. * @returns {React$Element}
  299. */
  300. _renderContent() {
  301. const { _knocking } = this.props;
  302. const { screenState } = this.state;
  303. if (screenState !== SCREEN_STATES.PASSWORD && _knocking) {
  304. return this._renderJoining();
  305. }
  306. return (
  307. <>
  308. { screenState === SCREEN_STATES.VIEW && this._renderParticipantInfo() }
  309. { screenState === SCREEN_STATES.EDIT && this._renderParticipantForm() }
  310. { screenState === SCREEN_STATES.PASSWORD && this._renderPasswordForm() }
  311. { (screenState === SCREEN_STATES.VIEW || screenState === SCREEN_STATES.EDIT)
  312. && this._renderStandardButtons() }
  313. { screenState === SCREEN_STATES.PASSWORD && this._renderPasswordJoinButtons() }
  314. </>
  315. );
  316. }
  317. /**
  318. * Renders the joining (waiting) fragment of the screen.
  319. *
  320. * @returns {React$Element}
  321. */
  322. _renderJoining: () => React.ReactElement;
  323. /**
  324. * Renders the participant form to let the knocking participant enter its details.
  325. *
  326. * @returns {React$Element}
  327. */
  328. _renderParticipantForm: () => React.ReactElement;
  329. /**
  330. * Renders the participant info fragment when we have all the required details of the user.
  331. *
  332. * @returns {React$Element}
  333. */
  334. _renderParticipantInfo: () => React.ReactElement;
  335. /**
  336. * Renders the password form to let the participant join by using a password instead of knocking.
  337. *
  338. * @returns {React$Element}
  339. */
  340. _renderPasswordForm: () => React.ReactElement;
  341. /**
  342. * Renders the password join button (set).
  343. *
  344. * @returns {React$Element}
  345. */
  346. _renderPasswordJoinButtons: () => React.ReactElement;
  347. /**
  348. * Renders the standard (pre-knocking) button set.
  349. *
  350. * @returns {React$Element}
  351. */
  352. _renderStandardButtons: () => React.ReactElement;
  353. }
  354. /**
  355. * Maps part of the Redux state to the props of this component.
  356. *
  357. * @param {Object} state - The Redux state.
  358. * @returns {IProps}
  359. */
  360. export function _mapStateToProps(state: IReduxState) {
  361. const localParticipant = getLocalParticipant(state);
  362. const participantId = localParticipant?.id;
  363. const inviteEnabledFlag = getFeatureFlag(state, INVITE_ENABLED, true);
  364. const { disableInviteFunctions } = state['features/base/config'];
  365. const { knocking, passwordJoinFailed } = state['features/lobby'];
  366. const { iAmSipGateway } = state['features/base/config'];
  367. const { disableLobbyPassword } = getSecurityUiConfig(state);
  368. const showCopyUrlButton = inviteEnabledFlag || !disableInviteFunctions;
  369. const deviceStatusVisible = isDeviceStatusVisible(state);
  370. const { membersOnly } = state['features/base/conference'];
  371. const { isLobbyChatActive, lobbyMessageRecipient, messages } = state['features/chat'];
  372. return {
  373. _deviceStatusVisible: deviceStatusVisible,
  374. _knocking: knocking,
  375. _lobbyChatMessages: messages,
  376. _lobbyMessageRecipient: lobbyMessageRecipient?.name,
  377. _isLobbyChatActive: isLobbyChatActive,
  378. _meetingName: getConferenceName(state),
  379. _membersOnlyConference: membersOnly,
  380. _participantEmail: localParticipant?.email,
  381. _participantId: participantId,
  382. _participantName: localParticipant?.name,
  383. _passwordJoinFailed: passwordJoinFailed,
  384. _renderPassword: !iAmSipGateway && !disableLobbyPassword,
  385. showCopyUrlButton
  386. };
  387. }