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

InviteContactsForm.js 15KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523
  1. // @flow
  2. import InlineMessage from '@atlaskit/inline-message';
  3. import React from 'react';
  4. import type { Dispatch } from 'redux';
  5. import { Avatar } from '../../../../base/avatar';
  6. import { translate, translateToHTML } from '../../../../base/i18n';
  7. import { Icon, IconPhone } from '../../../../base/icons';
  8. import { getLocalParticipant } from '../../../../base/participants';
  9. import { MultiSelectAutocomplete } from '../../../../base/react';
  10. import { connect } from '../../../../base/redux';
  11. import { hideAddPeopleDialog } from '../../../actions';
  12. import AbstractAddPeopleDialog, {
  13. type Props as AbstractProps,
  14. type State,
  15. _mapStateToProps as _abstractMapStateToProps
  16. } from '../AbstractAddPeopleDialog';
  17. declare var interfaceConfig: Object;
  18. type Props = AbstractProps & {
  19. /**
  20. * The {@link JitsiMeetConference} which will be used to invite "room" participants.
  21. */
  22. _conference: Object,
  23. /**
  24. * Whether to show a footer text after the search results as a last element.
  25. */
  26. _footerTextEnabled: boolean,
  27. /**
  28. * The redux {@code dispatch} function.
  29. */
  30. dispatch: Dispatch<any>,
  31. /**
  32. * Invoked to obtain translated strings.
  33. */
  34. t: Function,
  35. };
  36. /**
  37. * Form that enables inviting others to the call.
  38. */
  39. class InviteContactsForm extends AbstractAddPeopleDialog<Props, State> {
  40. _multiselect = null;
  41. _resourceClient: Object;
  42. state = {
  43. addToCallError: false,
  44. addToCallInProgress: false,
  45. inviteItems: []
  46. };
  47. /**
  48. * Initializes a new {@code AddPeopleDialog} instance.
  49. *
  50. * @param {Object} props - The read-only properties with which the new
  51. * instance is to be initialized.
  52. */
  53. constructor(props: Props) {
  54. super(props);
  55. // Bind event handlers so they are only bound once per instance.
  56. this._onClearItems = this._onClearItems.bind(this);
  57. this._onItemSelected = this._onItemSelected.bind(this);
  58. this._onSelectionChange = this._onSelectionChange.bind(this);
  59. this._onSubmit = this._onSubmit.bind(this);
  60. this._parseQueryResults = this._parseQueryResults.bind(this);
  61. this._setMultiSelectElement = this._setMultiSelectElement.bind(this);
  62. this._renderFooterText = this._renderFooterText.bind(this);
  63. this._onKeyDown = this._onKeyDown.bind(this);
  64. this._resourceClient = {
  65. makeQuery: this._query,
  66. parseResults: this._parseQueryResults
  67. };
  68. }
  69. /**
  70. * React Component method that executes once component is updated.
  71. *
  72. * @param {Object} prevProps - The state object before the update.
  73. * @param {Object} prevState - The state object before the update.
  74. * @returns {void}
  75. */
  76. componentDidUpdate(prevProps, prevState) {
  77. /**
  78. * Clears selected items from the multi select component on successful
  79. * invite.
  80. */
  81. if (prevState.addToCallError
  82. && !this.state.addToCallInProgress
  83. && !this.state.addToCallError
  84. && this._multiselect) {
  85. this._multiselect.setSelectedItems([]);
  86. }
  87. }
  88. /**
  89. * Renders the content of this component.
  90. *
  91. * @returns {ReactElement}
  92. */
  93. render() {
  94. const {
  95. _addPeopleEnabled,
  96. _dialOutEnabled,
  97. t
  98. } = this.props;
  99. const footerText = this._renderFooterText();
  100. let isMultiSelectDisabled = this.state.addToCallInProgress;
  101. let placeholder;
  102. let loadingMessage;
  103. let noMatches;
  104. if (_addPeopleEnabled && _dialOutEnabled) {
  105. loadingMessage = 'addPeople.loading';
  106. noMatches = 'addPeople.noResults';
  107. placeholder = 'addPeople.searchPeopleAndNumbers';
  108. } else if (_addPeopleEnabled) {
  109. loadingMessage = 'addPeople.loadingPeople';
  110. noMatches = 'addPeople.noResults';
  111. placeholder = 'addPeople.searchPeople';
  112. } else if (_dialOutEnabled) {
  113. loadingMessage = 'addPeople.loadingNumber';
  114. noMatches = 'addPeople.noValidNumbers';
  115. placeholder = 'addPeople.searchNumbers';
  116. } else {
  117. isMultiSelectDisabled = true;
  118. noMatches = 'addPeople.noResults';
  119. placeholder = 'addPeople.disabled';
  120. }
  121. return (
  122. <div
  123. className = 'add-people-form-wrap'
  124. onKeyDown = { this._onKeyDown }>
  125. { this._renderErrorMessage() }
  126. <MultiSelectAutocomplete
  127. footer = { footerText }
  128. isDisabled = { isMultiSelectDisabled }
  129. loadingMessage = { t(loadingMessage) }
  130. noMatchesFound = { t(noMatches) }
  131. onItemSelected = { this._onItemSelected }
  132. onSelectionChange = { this._onSelectionChange }
  133. placeholder = { t(placeholder) }
  134. ref = { this._setMultiSelectElement }
  135. resourceClient = { this._resourceClient }
  136. shouldFitContainer = { true }
  137. shouldFocus = { true } />
  138. { this._renderFormActions() }
  139. </div>
  140. );
  141. }
  142. _invite: Array<Object> => Promise<*>
  143. _isAddDisabled: () => boolean;
  144. _onItemSelected: (Object) => Object;
  145. /**
  146. * Callback invoked when a selection has been made but before it has been
  147. * set as selected.
  148. *
  149. * @param {Object} item - The item that has just been selected.
  150. * @private
  151. * @returns {Object} The item to display as selected in the input.
  152. */
  153. _onItemSelected(item) {
  154. if (item.item.type === 'phone') {
  155. item.content = item.item.number;
  156. }
  157. return item;
  158. }
  159. _onSelectionChange: (Map<*, *>) => void;
  160. /**
  161. * Handles a selection change.
  162. *
  163. * @param {Array} selectedItems - The list of selected items.
  164. * @private
  165. * @returns {void}
  166. */
  167. _onSelectionChange(selectedItems) {
  168. this.setState({
  169. inviteItems: selectedItems
  170. });
  171. }
  172. _onSubmit: () => void;
  173. /**
  174. * Submits the selection for inviting.
  175. *
  176. * @private
  177. * @returns {void}
  178. */
  179. _onSubmit() {
  180. const { inviteItems } = this.state;
  181. const invitees = inviteItems.map(({ item }) => item);
  182. this._invite(invitees)
  183. .then(invitesLeftToSend => {
  184. if (invitesLeftToSend.length) {
  185. const unsentInviteIDs
  186. = invitesLeftToSend.map(invitee =>
  187. invitee.id || invitee.user_id || invitee.number);
  188. const itemsToSelect
  189. = inviteItems.filter(({ item }) =>
  190. unsentInviteIDs.includes(item.id || item.user_id || item.number));
  191. if (this._multiselect) {
  192. this._multiselect.setSelectedItems(itemsToSelect);
  193. }
  194. } else {
  195. this.props.dispatch(hideAddPeopleDialog());
  196. }
  197. });
  198. }
  199. _onKeyDown: (Object) => void;
  200. /**
  201. * Handles 'Enter' key in the form to trigger the invite.
  202. *
  203. * @param {Object} event - The key event.
  204. * @returns {void}
  205. */
  206. _onKeyDown(event) {
  207. const { inviteItems } = this.state;
  208. if (event.key === 'Enter') {
  209. event.preventDefault();
  210. if (!this._isAddDisabled() && inviteItems.length) {
  211. this._onSubmit();
  212. }
  213. }
  214. }
  215. _parseQueryResults: (?Array<Object>) => Array<Object>;
  216. /**
  217. * Returns the avatar component for a user.
  218. *
  219. * @param {Object} user - The user.
  220. * @param {string} className - The CSS class for the avatar component.
  221. * @private
  222. * @returns {ReactElement}
  223. */
  224. _getAvatar(user, className = 'avatar-small') {
  225. return (
  226. <Avatar
  227. className = { className }
  228. status = { user.status }
  229. url = { user.avatar } />
  230. );
  231. }
  232. /**
  233. * Processes results from requesting available numbers and people by munging
  234. * each result into a format {@code MultiSelectAutocomplete} can use for
  235. * display.
  236. *
  237. * @param {Array} response - The response object from the server for the
  238. * query.
  239. * @private
  240. * @returns {Object[]} Configuration objects for items to display in the
  241. * search autocomplete.
  242. */
  243. _parseQueryResults(response = []) {
  244. const { t, _dialOutEnabled } = this.props;
  245. const users = response.filter(item => item.type !== 'phone');
  246. const userDisplayItems = [];
  247. for (const user of users) {
  248. const { name, phone } = user;
  249. const tagAvatar = this._getAvatar(user, 'avatar-xsmall');
  250. const elemAvatar = this._getAvatar(user);
  251. userDisplayItems.push({
  252. content: name,
  253. elemBefore: elemAvatar,
  254. item: user,
  255. tag: {
  256. elemBefore: tagAvatar
  257. },
  258. value: user.id || user.user_id
  259. });
  260. if (phone && _dialOutEnabled) {
  261. userDisplayItems.push({
  262. filterValues: [ name, phone ],
  263. content: `${phone} (${name})`,
  264. elemBefore: elemAvatar,
  265. item: {
  266. type: 'phone',
  267. number: phone
  268. },
  269. tag: {
  270. elemBefore: tagAvatar
  271. },
  272. value: phone
  273. });
  274. }
  275. }
  276. const numbers = response.filter(item => item.type === 'phone');
  277. const telephoneIcon = this._renderTelephoneIcon();
  278. const numberDisplayItems = numbers.map(number => {
  279. const numberNotAllowedMessage
  280. = number.allowed ? '' : t('addPeople.countryNotSupported');
  281. const countryCodeReminder = number.showCountryCodeReminder
  282. ? t('addPeople.countryReminder') : '';
  283. const description
  284. = `${numberNotAllowedMessage} ${countryCodeReminder}`.trim();
  285. return {
  286. filterValues: [
  287. number.originalEntry,
  288. number.number
  289. ],
  290. content: t('addPeople.telephone', { number: number.number }),
  291. description,
  292. isDisabled: !number.allowed,
  293. elemBefore: telephoneIcon,
  294. item: number,
  295. tag: {
  296. elemBefore: telephoneIcon
  297. },
  298. value: number.number
  299. };
  300. });
  301. return [
  302. ...userDisplayItems,
  303. ...numberDisplayItems
  304. ];
  305. }
  306. _query: (string) => Promise<Array<Object>>;
  307. _renderFooterText: () => Object;
  308. /**
  309. * Sets up the rendering of the footer text, if enabled.
  310. *
  311. * @returns {Object | undefined}
  312. */
  313. _renderFooterText() {
  314. const { _footerTextEnabled, t } = this.props;
  315. let footerText;
  316. if (_footerTextEnabled) {
  317. footerText = {
  318. content: <div className = 'footer-text-wrap'>
  319. <div>
  320. <span className = 'footer-telephone-icon'>
  321. <Icon src = { IconPhone } />
  322. </span>
  323. </div>
  324. { translateToHTML(t, 'addPeople.footerText') }
  325. </div>
  326. };
  327. }
  328. return footerText;
  329. }
  330. _onClearItems: () => void;
  331. /**
  332. * Clears the selected items from state and form.
  333. *
  334. * @returns {void}
  335. */
  336. _onClearItems() {
  337. if (this._multiselect) {
  338. this._multiselect.setSelectedItems([]);
  339. }
  340. this.setState({ inviteItems: [] });
  341. }
  342. /**
  343. * Renders the add/cancel actions for the form.
  344. *
  345. * @returns {ReactElement|null}
  346. */
  347. _renderFormActions() {
  348. const { inviteItems } = this.state;
  349. const { t } = this.props;
  350. if (!inviteItems.length) {
  351. return null;
  352. }
  353. return (
  354. <div className = { `invite-more-dialog invite-buttons${this._isAddDisabled() ? ' disabled' : ''}` }>
  355. <a
  356. className = 'invite-more-dialog invite-buttons-cancel'
  357. onClick = { this._onClearItems }>
  358. {t('dialog.Cancel')}
  359. </a>
  360. <a
  361. className = 'invite-more-dialog invite-buttons-add'
  362. onClick = { this._onSubmit }>
  363. {t('addPeople.add')}
  364. </a>
  365. </div>
  366. );
  367. }
  368. /**
  369. * Renders the error message if the add doesn't succeed.
  370. *
  371. * @private
  372. * @returns {ReactElement|null}
  373. */
  374. _renderErrorMessage() {
  375. if (!this.state.addToCallError) {
  376. return null;
  377. }
  378. const { t } = this.props;
  379. const supportString = t('inlineDialogFailure.supportMsg');
  380. const supportLink = interfaceConfig.SUPPORT_URL;
  381. if (!supportLink) {
  382. return null;
  383. }
  384. const supportLinkContent = (
  385. <span>
  386. <span>
  387. { supportString.padEnd(supportString.length + 1) }
  388. </span>
  389. <span>
  390. <a
  391. href = { supportLink }
  392. rel = 'noopener noreferrer'
  393. target = '_blank'>
  394. { t('inlineDialogFailure.support') }
  395. </a>
  396. </span>
  397. <span>.</span>
  398. </span>
  399. );
  400. return (
  401. <div className = 'modal-dialog-form-error'>
  402. <InlineMessage
  403. title = { t('addPeople.failedToAdd') }
  404. type = 'error'>
  405. { supportLinkContent }
  406. </InlineMessage>
  407. </div>
  408. );
  409. }
  410. /**
  411. * Renders a telephone icon.
  412. *
  413. * @private
  414. * @returns {ReactElement}
  415. */
  416. _renderTelephoneIcon() {
  417. return (
  418. <span className = 'add-telephone-icon'>
  419. <Icon src = { IconPhone } />
  420. </span>
  421. );
  422. }
  423. _setMultiSelectElement: (React$ElementRef<*> | null) => void;
  424. /**
  425. * Sets the instance variable for the multi select component
  426. * element so it can be accessed directly.
  427. *
  428. * @param {Object} element - The DOM element for the component's dialog.
  429. * @private
  430. * @returns {void}
  431. */
  432. _setMultiSelectElement(element) {
  433. this._multiselect = element;
  434. }
  435. }
  436. /**
  437. * Maps (parts of) the Redux state to the associated
  438. * {@code AddPeopleDialog}'s props.
  439. *
  440. * @param {Object} state - The Redux state.
  441. * @private
  442. * @returns {Props}
  443. */
  444. function _mapStateToProps(state) {
  445. const { enableFeaturesBasedOnToken } = state['features/base/config'];
  446. let footerTextEnabled = false;
  447. if (enableFeaturesBasedOnToken) {
  448. const { features = {} } = getLocalParticipant(state);
  449. if (String(features['outbound-call']) !== 'true') {
  450. footerTextEnabled = true;
  451. }
  452. }
  453. return {
  454. ..._abstractMapStateToProps(state),
  455. _footerTextEnabled: footerTextEnabled
  456. };
  457. }
  458. export default translate(connect(_mapStateToProps)(InviteContactsForm));