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';
|
|
|
|
|
|
|
|
import debounce from '../../utils/debounce';
|
|
|
|
import { avatarURL } from '../../utils/avatar';
|
|
|
|
import RocketChat from '../../lib/rocketchat';
|
|
|
|
import I18n from '../../i18n';
|
|
|
|
import { MultiSelect } from '../../containers/UIKit/MultiSelect';
|
2022-04-07 14:10:03 +00:00
|
|
|
import { themes } from '../../lib/constants';
|
2021-09-13 20:41:05 +00:00
|
|
|
import styles from './styles';
|
2021-09-15 19:58:14 +00:00
|
|
|
import { ICreateDiscussionViewSelectUsers } from './interfaces';
|
2022-01-12 12:54:04 +00:00
|
|
|
import { SubscriptionType } from '../../definitions/ISubscription';
|
2020-03-30 19:50:27 +00:00
|
|
|
|
2021-09-15 19:58:14 +00:00
|
|
|
interface IUser {
|
|
|
|
name: string;
|
|
|
|
username: string;
|
|
|
|
}
|
|
|
|
|
|
|
|
const SelectUsers = ({
|
|
|
|
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-09-13 20:41:05 +00:00
|
|
|
const getUsers = debounce(async (keyword = '') => {
|
2020-03-30 19:50:27 +00:00
|
|
|
try {
|
|
|
|
const res = await RocketChat.search({ text: keyword, filterRooms: false });
|
2022-04-26 18:15:29 +00:00
|
|
|
const selectedUsers = users.filter((u: IUser) => selected.includes(u.name));
|
|
|
|
const filteredUsers = res.filter(r => !users.find((u: IUser) => u.name === r.name));
|
|
|
|
const items = [...selectedUsers, ...filteredUsers];
|
|
|
|
setUsers(items);
|
2020-03-30 19:50:27 +00:00
|
|
|
} catch {
|
|
|
|
// do nothing
|
|
|
|
}
|
|
|
|
}, 300);
|
|
|
|
|
2021-09-15 19:58:14 +00:00
|
|
|
const getAvatar = (item: any) =>
|
2021-09-13 20:41:05 +00:00
|
|
|
avatarURL({
|
|
|
|
text: RocketChat.getRoomAvatar(item),
|
2022-01-12 12:54:04 +00:00
|
|
|
type: SubscriptionType.DIRECT,
|
2021-09-13 20:41:05 +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
|
|
|
|
inputStyle={styles.inputStyle}
|
|
|
|
onSearch={getUsers}
|
|
|
|
onChange={onUserSelect}
|
2021-09-15 19:58:14 +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-09-15 19:58:14 +00:00
|
|
|
onClose={() => setUsers(users.filter((u: IUser) => 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
|
|
|
|
/>
|
|
|
|
</>
|
|
|
|
);
|
|
|
|
};
|
|
|
|
|
|
|
|
export default SelectUsers;
|