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

AddPeopleDialog.js 18KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629
  1. // @flow
  2. import _ from 'lodash';
  3. import React from 'react';
  4. import {
  5. ActivityIndicator,
  6. FlatList,
  7. Platform,
  8. SafeAreaView,
  9. TextInput,
  10. TouchableOpacity,
  11. View
  12. } from 'react-native';
  13. import { ColorSchemeRegistry } from '../../../../base/color-scheme';
  14. import { AlertDialog, openDialog } from '../../../../base/dialog';
  15. import { translate } from '../../../../base/i18n';
  16. import {
  17. Icon,
  18. IconCancelSelection,
  19. IconCheck,
  20. IconClose,
  21. IconPhone,
  22. IconSearch,
  23. IconShare
  24. } from '../../../../base/icons';
  25. import { JitsiModal, setActiveModalId } from '../../../../base/modal';
  26. import {
  27. AvatarListItem,
  28. type Item
  29. } from '../../../../base/react';
  30. import { connect } from '../../../../base/redux';
  31. import { ColorPalette } from '../../../../base/styles';
  32. import { beginShareRoom } from '../../../../share-room';
  33. import { ADD_PEOPLE_DIALOG_VIEW_ID, INVITE_TYPES } from '../../../constants';
  34. import AbstractAddPeopleDialog, {
  35. type Props as AbstractProps,
  36. type State as AbstractState,
  37. _mapStateToProps as _abstractMapStateToProps
  38. } from '../AbstractAddPeopleDialog';
  39. import styles, {
  40. AVATAR_SIZE,
  41. DARK_GREY
  42. } from './styles';
  43. type Props = AbstractProps & {
  44. /**
  45. * The color schemed style of the Header.
  46. */
  47. _headerStyles: Object,
  48. /**
  49. * True if the invite dialog should be open, false otherwise.
  50. */
  51. _isVisible: boolean,
  52. /**
  53. * Function used to translate i18n labels.
  54. */
  55. t: Function
  56. };
  57. type State = AbstractState & {
  58. /**
  59. * Boolean to show if an extra padding needs to be added to the bottom bar.
  60. */
  61. bottomPadding: boolean,
  62. /**
  63. * State variable to keep track of the search field value.
  64. */
  65. fieldValue: string,
  66. /**
  67. * True if a search is in progress, false otherwise.
  68. */
  69. searchInprogress: boolean,
  70. /**
  71. * An array of items that are selectable on this dialog. This is usually
  72. * populated by an async search.
  73. */
  74. selectableItems: Array<Object>
  75. };
  76. /**
  77. * Implements a special dialog to invite people from a directory service.
  78. */
  79. class AddPeopleDialog extends AbstractAddPeopleDialog<Props, State> {
  80. /**
  81. * Default state object to reset the state to when needed.
  82. */
  83. defaultState = {
  84. addToCallError: false,
  85. addToCallInProgress: false,
  86. bottomPadding: false,
  87. fieldValue: '',
  88. inviteItems: [],
  89. searchInprogress: false,
  90. selectableItems: []
  91. };
  92. /**
  93. * Ref of the search field.
  94. */
  95. inputFieldRef: ?TextInput;
  96. /**
  97. * TimeoutID to delay the search for the time the user is probably typing.
  98. */
  99. searchTimeout: TimeoutID;
  100. /**
  101. * Contrustor of the component.
  102. *
  103. * @inheritdoc
  104. */
  105. constructor(props: Props) {
  106. super(props);
  107. this.state = this.defaultState;
  108. this._keyExtractor = this._keyExtractor.bind(this);
  109. this._renderInvitedItem = this._renderInvitedItem.bind(this);
  110. this._renderItem = this._renderItem.bind(this);
  111. this._renderSeparator = this._renderSeparator.bind(this);
  112. this._onClearField = this._onClearField.bind(this);
  113. this._onInvite = this._onInvite.bind(this);
  114. this._onPressItem = this._onPressItem.bind(this);
  115. this._onShareMeeting = this._onShareMeeting.bind(this);
  116. this._onTypeQuery = this._onTypeQuery.bind(this);
  117. this._renderShareMeetingButton = this._renderShareMeetingButton.bind(this);
  118. this._setFieldRef = this._setFieldRef.bind(this);
  119. }
  120. /**
  121. * Implements {@code Component#componentDidUpdate}.
  122. *
  123. * @inheritdoc
  124. */
  125. componentDidUpdate(prevProps) {
  126. if (prevProps._isVisible !== this.props._isVisible) {
  127. // Clear state
  128. this._clearState();
  129. }
  130. }
  131. /**
  132. * Implements {@code Component#render}.
  133. *
  134. * @inheritdoc
  135. */
  136. render() {
  137. const {
  138. _addPeopleEnabled,
  139. _dialOutEnabled
  140. } = this.props;
  141. const { inviteItems, selectableItems } = this.state;
  142. let placeholderKey = 'searchPlaceholder';
  143. if (!_addPeopleEnabled) {
  144. placeholderKey = 'searchCallOnlyPlaceholder';
  145. } else if (!_dialOutEnabled) {
  146. placeholderKey = 'searchPeopleOnlyPlaceholder';
  147. }
  148. return (
  149. <JitsiModal
  150. footerComponent = { this._renderShareMeetingButton }
  151. headerProps = {{
  152. forwardDisabled: this._isAddDisabled(),
  153. forwardLabelKey: 'inviteDialog.send',
  154. headerLabelKey: 'inviteDialog.header',
  155. onPressForward: this._onInvite
  156. }}
  157. modalId = { ADD_PEOPLE_DIALOG_VIEW_ID }>
  158. <View
  159. style = { styles.searchFieldWrapper }>
  160. <View style = { styles.searchIconWrapper }>
  161. { this.state.searchInprogress
  162. ? <ActivityIndicator
  163. color = { DARK_GREY }
  164. size = 'small' />
  165. : <Icon
  166. src = { IconSearch }
  167. style = { styles.searchIcon } />}
  168. </View>
  169. <TextInput
  170. autoCorrect = { false }
  171. autoFocus = { false }
  172. onBlur = { this._onFocused(false) }
  173. onChangeText = { this._onTypeQuery }
  174. onFocus = { this._onFocused(true) }
  175. placeholder = {
  176. this.props.t(`inviteDialog.${placeholderKey}`)
  177. }
  178. placeholderTextColor = { ColorPalette.lightGrey }
  179. ref = { this._setFieldRef }
  180. style = { styles.searchField }
  181. value = { this.state.fieldValue } />
  182. { this._renderClearButton() }
  183. </View>
  184. { Boolean(inviteItems.length) && <View style = { styles.invitedList }>
  185. <FlatList
  186. data = { inviteItems }
  187. horizontal = { true }
  188. keyExtractor = { this._keyExtractor }
  189. keyboardShouldPersistTaps = 'always'
  190. renderItem = { this._renderInvitedItem } />
  191. </View> }
  192. <View style = { styles.resultList }>
  193. <FlatList
  194. ItemSeparatorComponent = { this._renderSeparator }
  195. data = { selectableItems }
  196. extraData = { inviteItems }
  197. keyExtractor = { this._keyExtractor }
  198. keyboardShouldPersistTaps = 'always'
  199. renderItem = { this._renderItem } />
  200. </View>
  201. </JitsiModal>
  202. );
  203. }
  204. /**
  205. * Clears the dialog content.
  206. *
  207. * @returns {void}
  208. */
  209. _clearState() {
  210. this.setState(this.defaultState);
  211. }
  212. /**
  213. * Returns an object capable of being rendered by an {@code AvatarListItem}.
  214. *
  215. * @param {Object} flatListItem - An item of the data array of the {@code FlatList}.
  216. * @returns {?Object}
  217. */
  218. _getRenderableItem(flatListItem) {
  219. const { item } = flatListItem;
  220. switch (item.type) {
  221. case INVITE_TYPES.PHONE:
  222. return {
  223. avatar: IconPhone,
  224. key: item.number,
  225. title: item.number
  226. };
  227. case INVITE_TYPES.USER:
  228. return {
  229. avatar: item.avatar,
  230. key: item.id || item.user_id,
  231. title: item.name
  232. };
  233. default:
  234. return null;
  235. }
  236. }
  237. _invite: Array<Object> => Promise<Array<Object>>
  238. _isAddDisabled: () => boolean;
  239. _keyExtractor: Object => string
  240. /**
  241. * Key extractor for the flatlist.
  242. *
  243. * @param {Object} item - The flatlist item that we need the key to be
  244. * generated for.
  245. * @returns {string}
  246. */
  247. _keyExtractor(item) {
  248. return item.type === INVITE_TYPES.USER ? item.id || item.user_id : item.number;
  249. }
  250. _onClearField: () => void
  251. /**
  252. * Callback to clear the text field.
  253. *
  254. * @returns {void}
  255. */
  256. _onClearField() {
  257. this.setState({
  258. fieldValue: ''
  259. });
  260. // Clear search results
  261. this._onTypeQuery('');
  262. }
  263. _onFocused: boolean => Function;
  264. /**
  265. * Constructs a callback to be used to update the padding of the field if necessary.
  266. *
  267. * @param {boolean} focused - True of the field is focused.
  268. * @returns {Function}
  269. */
  270. _onFocused(focused) {
  271. return () => {
  272. Platform.OS === 'android' && this.setState({
  273. bottomPadding: focused
  274. });
  275. };
  276. }
  277. _onInvite: () => void
  278. /**
  279. * Invites the selected entries.
  280. *
  281. * @returns {void}
  282. */
  283. _onInvite() {
  284. this._invite(this.state.inviteItems)
  285. .then(invitesLeftToSend => {
  286. if (invitesLeftToSend.length) {
  287. this.setState({
  288. inviteItems: invitesLeftToSend
  289. });
  290. this._showFailedInviteAlert();
  291. } else {
  292. this.props.dispatch(setActiveModalId());
  293. }
  294. });
  295. }
  296. _onPressItem: Item => Function
  297. /**
  298. * Function to prepare a callback for the onPress event of the touchable.
  299. *
  300. * @param {Item} item - The item on which onPress was invoked.
  301. * @returns {Function}
  302. */
  303. _onPressItem(item) {
  304. return () => {
  305. const { inviteItems } = this.state;
  306. const finderKey = item.type === INVITE_TYPES.PHONE ? 'number' : 'user_id';
  307. if (inviteItems.find(
  308. _.matchesProperty(finderKey, item[finderKey]))) {
  309. // Item is already selected, need to unselect it.
  310. this.setState({
  311. inviteItems: inviteItems.filter(
  312. element => item[finderKey] !== element[finderKey])
  313. });
  314. } else {
  315. // Item is not selected yet, need to add to the list.
  316. const items: Array<Object> = inviteItems.concat(item);
  317. this.setState({
  318. inviteItems: _.sortBy(items, [ 'name', 'number' ])
  319. });
  320. }
  321. };
  322. }
  323. _onShareMeeting: () => void
  324. /**
  325. * Shows the system share sheet to share the meeting information.
  326. *
  327. * @returns {void}
  328. */
  329. _onShareMeeting() {
  330. if (this.state.inviteItems.length > 0) {
  331. // The use probably intended to invite people.
  332. this._onInvite();
  333. } else {
  334. this.props.dispatch(beginShareRoom());
  335. }
  336. }
  337. _onTypeQuery: string => void
  338. /**
  339. * Handles the typing event of the text field on the dialog and performs the
  340. * search.
  341. *
  342. * @param {string} query - The query that is typed in the field.
  343. * @returns {void}
  344. */
  345. _onTypeQuery(query) {
  346. this.setState({
  347. fieldValue: query
  348. });
  349. clearTimeout(this.searchTimeout);
  350. this.searchTimeout = setTimeout(() => {
  351. this.setState({
  352. searchInprogress: true
  353. }, () => {
  354. this._performSearch(query);
  355. });
  356. }, 500);
  357. }
  358. /**
  359. * Performs the actual search.
  360. *
  361. * @param {string} query - The query to search for.
  362. * @returns {void}
  363. */
  364. _performSearch(query) {
  365. this._query(query).then(results => {
  366. this.setState({
  367. selectableItems: _.sortBy(results, [ 'name', 'number' ])
  368. });
  369. })
  370. .finally(() => {
  371. this.setState({
  372. searchInprogress: false
  373. }, () => {
  374. this.inputFieldRef && this.inputFieldRef.focus();
  375. });
  376. });
  377. }
  378. _query: (string) => Promise<Array<Object>>;
  379. /**
  380. * Renders a button to clear the text field.
  381. *
  382. * @returns {React#Element<*>}
  383. */
  384. _renderClearButton() {
  385. if (!this.state.fieldValue.length) {
  386. return null;
  387. }
  388. return (
  389. <TouchableOpacity
  390. onPress = { this._onClearField }
  391. style = { styles.clearButton }>
  392. <View style = { styles.clearIconContainer }>
  393. <Icon
  394. src = { IconClose }
  395. style = { styles.clearIcon } />
  396. </View>
  397. </TouchableOpacity>
  398. );
  399. }
  400. _renderInvitedItem: Object => React$Element<any> | null
  401. /**
  402. * Renders a single item in the invited {@code FlatList}.
  403. *
  404. * @param {Object} flatListItem - An item of the data array of the
  405. * {@code FlatList}.
  406. * @param {number} index - The index of the currently rendered item.
  407. * @returns {?React$Element<any>}
  408. */
  409. _renderInvitedItem(flatListItem, index): React$Element<any> | null {
  410. const { item } = flatListItem;
  411. const renderableItem = this._getRenderableItem(flatListItem);
  412. return (
  413. <TouchableOpacity onPress = { this._onPressItem(item) } >
  414. <View
  415. pointerEvents = 'box-only'
  416. style = { styles.itemWrapper }>
  417. <AvatarListItem
  418. avatarOnly = { true }
  419. avatarSize = { AVATAR_SIZE }
  420. avatarStatus = { item.status }
  421. avatarStyle = { styles.avatar }
  422. avatarTextStyle = { styles.avatarText }
  423. item = { renderableItem }
  424. key = { index }
  425. linesStyle = { styles.itemLinesStyle }
  426. titleStyle = { styles.itemText } />
  427. <Icon
  428. src = { IconCancelSelection }
  429. style = { styles.unselectIcon } />
  430. </View>
  431. </TouchableOpacity>
  432. );
  433. }
  434. _renderItem: Object => React$Element<any> | null
  435. /**
  436. * Renders a single item in the search result {@code FlatList}.
  437. *
  438. * @param {Object} flatListItem - An item of the data array of the
  439. * {@code FlatList}.
  440. * @param {number} index - The index of the currently rendered item.
  441. * @returns {?React$Element<*>}
  442. */
  443. _renderItem(flatListItem, index): React$Element<any> | null {
  444. const { item } = flatListItem;
  445. const { inviteItems } = this.state;
  446. let selected = false;
  447. const renderableItem = this._getRenderableItem(flatListItem);
  448. if (!renderableItem) {
  449. return null;
  450. }
  451. switch (item.type) {
  452. case INVITE_TYPES.PHONE:
  453. selected = inviteItems.find(_.matchesProperty('number', item.number));
  454. break;
  455. case INVITE_TYPES.USER:
  456. selected = item.id
  457. ? inviteItems.find(_.matchesProperty('id', item.id))
  458. : inviteItems.find(_.matchesProperty('user_id', item.user_id));
  459. break;
  460. default:
  461. return null;
  462. }
  463. return (
  464. <TouchableOpacity onPress = { this._onPressItem(item) } >
  465. <View
  466. pointerEvents = 'box-only'
  467. style = { styles.itemWrapper }>
  468. <AvatarListItem
  469. avatarSize = { AVATAR_SIZE }
  470. avatarStatus = { item.status }
  471. avatarStyle = { styles.avatar }
  472. avatarTextStyle = { styles.avatarText }
  473. item = { renderableItem }
  474. key = { index }
  475. linesStyle = { styles.itemLinesStyle }
  476. titleStyle = { styles.itemText } />
  477. { selected && <Icon
  478. src = { IconCheck }
  479. style = { styles.selectedIcon } /> }
  480. </View>
  481. </TouchableOpacity>
  482. );
  483. }
  484. _renderSeparator: () => React$Element<*> | null
  485. /**
  486. * Renders the item separator.
  487. *
  488. * @returns {?React$Element<*>}
  489. */
  490. _renderSeparator() {
  491. return (
  492. <View style = { styles.separator } />
  493. );
  494. }
  495. _renderShareMeetingButton: () => React$Element<any>;
  496. /**
  497. * Renders a button to share the meeting info.
  498. *
  499. * @returns {React#Element<*>}
  500. */
  501. _renderShareMeetingButton() {
  502. const { _headerStyles } = this.props;
  503. return (
  504. <SafeAreaView
  505. style = { [
  506. styles.bottomBar,
  507. _headerStyles.headerOverlay,
  508. this.state.bottomPadding ? styles.extraBarPadding : null
  509. ] }>
  510. <TouchableOpacity
  511. onPress = { this._onShareMeeting }>
  512. <Icon
  513. src = { IconShare }
  514. style = { [ _headerStyles.headerButtonText, styles.shareIcon ] } />
  515. </TouchableOpacity>
  516. </SafeAreaView>
  517. );
  518. }
  519. _setFieldRef: ?TextInput => void
  520. /**
  521. * Sets a reference to the input field for later use.
  522. *
  523. * @param {?TextInput} input - The reference to the input field.
  524. * @returns {void}
  525. */
  526. _setFieldRef(input) {
  527. this.inputFieldRef = input;
  528. }
  529. /**
  530. * Shows an alert telling the user that some invitees were failed to be
  531. * invited.
  532. *
  533. * NOTE: We're using an Alert here because we're on a modal and it makes
  534. * using our dialogs a tad more difficult.
  535. *
  536. * @returns {void}
  537. */
  538. _showFailedInviteAlert() {
  539. this.props.dispatch(openDialog(AlertDialog, {
  540. contentKey: {
  541. key: 'inviteDialog.alertText'
  542. }
  543. }));
  544. }
  545. }
  546. /**
  547. * Maps part of the Redux state to the props of this component.
  548. *
  549. * @param {Object} state - The Redux state.
  550. * @returns {{
  551. * _isVisible: boolean
  552. * }}
  553. */
  554. function _mapStateToProps(state: Object) {
  555. return {
  556. ..._abstractMapStateToProps(state),
  557. _headerStyles: ColorSchemeRegistry.get(state, 'Header'),
  558. _isVisible: state['features/base/modal'].activeModalId === ADD_PEOPLE_DIALOG_VIEW_ID
  559. };
  560. }
  561. export default translate(connect(_mapStateToProps)(AddPeopleDialog));