2020-03-30 19:50:27 +00:00
|
|
|
import React, { useState } from 'react';
|
|
|
|
import { Text } from 'react-native';
|
|
|
|
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-10-01 18:12:19 +00:00
|
|
|
import styles from './styles';
|
|
|
|
import { ICreateDiscussionViewSelectUsers } from './interfaces';
|
2022-01-24 20:12:25 +00:00
|
|
|
import { SubscriptionType } from '../../definitions/ISubscription';
|
2021-10-01 18:12:19 +00:00
|
|
|
|
|
|
|
interface IUser {
|
|
|
|
name: string;
|
|
|
|
username: string;
|
|
|
|
}
|
2020-03-30 19:50:27 +00:00
|
|
|
|
|
|
|
const SelectUsers = ({
|
2021-10-01 18:12:19 +00:00
|
|
|
server,
|
|
|
|
token,
|
|
|
|
userId,
|
|
|
|
selected,
|
|
|
|
onUserSelect,
|
|
|
|
blockUnauthenticatedAccess,
|
|
|
|
serverVersion,
|
|
|
|
theme
|
|
|
|
}: ICreateDiscussionViewSelectUsers): JSX.Element => {
|
|
|
|
const [users, setUsers] = useState<any[]>([]);
|
2020-03-30 19:50:27 +00:00
|
|
|
|
2021-10-01 18:12:19 +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-03-15 20:16:34 +00:00
|
|
|
const usersCollection = db.get('users');
|
2020-03-30 19:50:27 +00:00
|
|
|
const res = await RocketChat.search({ text: keyword, filterRooms: false });
|
2021-10-01 18:12:19 +00:00
|
|
|
let items = [
|
|
|
|
...users.filter((u: IUser) => selected.includes(u.name)),
|
|
|
|
...res.filter((r: IUser) => !users.find((u: IUser) => u.name === r.name))
|
|
|
|
];
|
2020-10-30 13:12:02 +00:00
|
|
|
const records = await usersCollection.query(Q.where('username', Q.oneOf(items.map(u => u.name)))).fetch();
|
2021-10-01 18:12:19 +00:00
|
|
|
items = items.map(item => {
|
|
|
|
const index = records.findIndex((r: IUser) => r.username === item.name);
|
2020-10-30 13:12:02 +00:00
|
|
|
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-10-01 18:12:19 +00:00
|
|
|
const getAvatar = (item: any) =>
|
|
|
|
avatarURL({
|
|
|
|
text: RocketChat.getRoomAvatar(item),
|
2022-01-24 20:12:25 +00:00
|
|
|
type: SubscriptionType.DIRECT,
|
2021-10-01 18:12:19 +00:00
|
|
|
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}
|
2021-10-01 18:12:19 +00:00
|
|
|
options={users.map((user: IUser) => ({
|
2020-03-30 19:50:27 +00:00
|
|
|
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
|
|
|
}))}
|
2021-10-01 18:12:19 +00:00
|
|
|
onClose={() => setUsers(users.filter((u: IUser) => selected.includes(u.name)))}
|
|
|
|
placeholder={{ text: `${I18n.t('Select_Users')}...` }}
|
2020-03-30 19:50:27 +00:00
|
|
|
context={BLOCK_CONTEXT.FORM}
|
|
|
|
multiselect
|
|
|
|
/>
|
|
|
|
</>
|
|
|
|
);
|
|
|
|
};
|
|
|
|
|
|
|
|
export default SelectUsers;
|