2020-03-30 19:50:27 +00:00
|
|
|
import React, { useState } from 'react';
|
|
|
|
import { Text } from 'react-native';
|
|
|
|
import PropTypes from 'prop-types';
|
|
|
|
import { BLOCK_CONTEXT } from '@rocket.chat/ui-kit';
|
2020-10-30 13:12:02 +00:00
|
|
|
import { Q } from '@nozbe/watermelondb';
|
2020-03-30 19:50:27 +00:00
|
|
|
|
|
|
|
import debounce from '../../utils/debounce';
|
|
|
|
import { avatarURL } from '../../utils/avatar';
|
|
|
|
import RocketChat from '../../lib/rocketchat';
|
2020-10-30 13:12:02 +00:00
|
|
|
import database from '../../lib/database';
|
2020-03-30 19:50:27 +00:00
|
|
|
import I18n from '../../i18n';
|
|
|
|
import { MultiSelect } from '../../containers/UIKit/MultiSelect';
|
2020-04-01 12:07:03 +00:00
|
|
|
import { themes } from '../../constants/colors';
|
2021-09-13 20:41:05 +00:00
|
|
|
import styles from './styles';
|
2020-03-30 19:50:27 +00:00
|
|
|
|
2021-09-13 20:41:05 +00:00
|
|
|
const SelectUsers = ({ server, token, userId, selected, onUserSelect, blockUnauthenticatedAccess, serverVersion, theme }) => {
|
2020-03-30 19:50:27 +00:00
|
|
|
const [users, setUsers] = useState([]);
|
|
|
|
|
2021-09-13 20:41:05 +00:00
|
|
|
const getUsers = debounce(async (keyword = '') => {
|
2020-03-30 19:50:27 +00:00
|
|
|
try {
|
2020-10-30 13:12:02 +00:00
|
|
|
const db = database.active;
|
2021-02-26 16:25:51 +00:00
|
|
|
const usersCollection = db.get('users');
|
2020-03-30 19:50:27 +00:00
|
|
|
const res = await RocketChat.search({ text: keyword, filterRooms: false });
|
2020-10-30 13:12:02 +00:00
|
|
|
let items = [...users.filter(u => selected.includes(u.name)), ...res.filter(r => !users.find(u => u.name === r.name))];
|
|
|
|
const records = await usersCollection.query(Q.where('username', Q.oneOf(items.map(u => u.name)))).fetch();
|
2021-09-13 20:41:05 +00:00
|
|
|
items = items.map(item => {
|
2020-10-30 13:12:02 +00:00
|
|
|
const index = records.findIndex(r => r.username === item.name);
|
|
|
|
if (index > -1) {
|
|
|
|
const record = records[index];
|
|
|
|
return {
|
|
|
|
uids: item.uids,
|
|
|
|
usernames: item.usernames,
|
|
|
|
prid: item.prid,
|
|
|
|
fname: item.fname,
|
|
|
|
name: item.name,
|
|
|
|
avatarETag: record.avatarETag
|
|
|
|
};
|
|
|
|
}
|
|
|
|
return item;
|
|
|
|
});
|
|
|
|
setUsers(items);
|
2020-03-30 19:50:27 +00:00
|
|
|
} catch {
|
|
|
|
// do nothing
|
|
|
|
}
|
|
|
|
}, 300);
|
|
|
|
|
2021-09-13 20:41:05 +00:00
|
|
|
const getAvatar = item =>
|
|
|
|
avatarURL({
|
|
|
|
text: RocketChat.getRoomAvatar(item),
|
|
|
|
type: 'd',
|
|
|
|
user: { id: userId, token },
|
|
|
|
server,
|
|
|
|
avatarETag: item.avatarETag,
|
|
|
|
blockUnauthenticatedAccess,
|
|
|
|
serverVersion
|
|
|
|
});
|
2020-03-30 19:50:27 +00:00
|
|
|
|
|
|
|
return (
|
|
|
|
<>
|
2020-04-01 12:07:03 +00:00
|
|
|
<Text style={[styles.label, { color: themes[theme].titleText }]}>{I18n.t('Invite_users')}</Text>
|
2020-03-30 19:50:27 +00:00
|
|
|
<MultiSelect
|
|
|
|
theme={theme}
|
|
|
|
inputStyle={styles.inputStyle}
|
|
|
|
onSearch={getUsers}
|
|
|
|
onChange={onUserSelect}
|
|
|
|
options={users.map(user => ({
|
|
|
|
value: user.name,
|
|
|
|
text: { text: RocketChat.getRoomTitle(user) },
|
2020-10-30 13:12:02 +00:00
|
|
|
imageUrl: getAvatar(user)
|
2020-03-30 19:50:27 +00:00
|
|
|
}))}
|
|
|
|
onClose={() => setUsers(users.filter(u => selected.includes(u.name)))}
|
2021-09-13 20:41:05 +00:00
|
|
|
placeholder={{ text: `${I18n.t('Select_Users')}...` }}
|
2020-03-30 19:50:27 +00:00
|
|
|
context={BLOCK_CONTEXT.FORM}
|
|
|
|
multiselect
|
|
|
|
/>
|
|
|
|
</>
|
|
|
|
);
|
|
|
|
};
|
|
|
|
SelectUsers.propTypes = {
|
|
|
|
server: PropTypes.string,
|
|
|
|
token: PropTypes.string,
|
|
|
|
userId: PropTypes.string,
|
|
|
|
selected: PropTypes.array,
|
|
|
|
onUserSelect: PropTypes.func,
|
2020-10-30 15:54:02 +00:00
|
|
|
blockUnauthenticatedAccess: PropTypes.bool,
|
2020-11-04 16:53:44 +00:00
|
|
|
serverVersion: PropTypes.string,
|
2020-03-30 19:50:27 +00:00
|
|
|
theme: PropTypes.string
|
|
|
|
};
|
|
|
|
|
|
|
|
export default SelectUsers;
|