2020-03-30 19:50:27 +00:00
|
|
|
import React, { useState } from 'react';
|
|
|
|
import { Text } from 'react-native';
|
|
|
|
|
|
|
|
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';
|
2020-04-01 12:07:03 +00:00
|
|
|
import { themes } from '../../constants/colors';
|
2020-03-30 19:50:27 +00:00
|
|
|
import styles from './styles';
|
2021-09-15 19:58:14 +00:00
|
|
|
import { ICreateDiscussionViewSelectChannel } from './interfaces';
|
2020-03-30 19:50:27 +00:00
|
|
|
|
2021-09-15 19:58:14 +00:00
|
|
|
const SelectChannel = ({
|
|
|
|
server,
|
|
|
|
token,
|
|
|
|
userId,
|
|
|
|
onChannelSelect,
|
|
|
|
initial,
|
|
|
|
blockUnauthenticatedAccess,
|
|
|
|
serverVersion,
|
|
|
|
theme
|
|
|
|
}: ICreateDiscussionViewSelectChannel): JSX.Element => {
|
2020-03-30 19:50:27 +00:00
|
|
|
const [channels, setChannels] = useState([]);
|
|
|
|
|
2021-09-13 20:41:05 +00:00
|
|
|
const getChannels = debounce(async (keyword = '') => {
|
2020-03-30 19:50:27 +00:00
|
|
|
try {
|
2021-01-20 17:34:01 +00:00
|
|
|
const res = await RocketChat.localSearch({ text: keyword });
|
2020-03-30 19:50:27 +00:00
|
|
|
setChannels(res);
|
|
|
|
} 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),
|
|
|
|
type: item.t,
|
|
|
|
user: { id: userId, token },
|
|
|
|
server,
|
|
|
|
avatarETag: item.avatarETag,
|
|
|
|
rid: item.rid,
|
|
|
|
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('Parent_channel_or_group')}</Text>
|
2020-03-30 19:50:27 +00:00
|
|
|
<MultiSelect
|
|
|
|
theme={theme}
|
|
|
|
inputStyle={styles.inputStyle}
|
|
|
|
onChange={onChannelSelect}
|
|
|
|
onSearch={getChannels}
|
|
|
|
value={initial && [initial]}
|
|
|
|
disabled={initial}
|
|
|
|
options={channels.map(channel => ({
|
2021-01-20 17:34:01 +00:00
|
|
|
value: channel,
|
2020-03-30 19:50:27 +00:00
|
|
|
text: { text: RocketChat.getRoomTitle(channel) },
|
2020-10-30 13:51:04 +00:00
|
|
|
imageUrl: getAvatar(channel)
|
2020-03-30 19:50:27 +00:00
|
|
|
}))}
|
|
|
|
onClose={() => setChannels([])}
|
2021-09-13 20:41:05 +00:00
|
|
|
placeholder={{ text: `${I18n.t('Select_a_Channel')}...` }}
|
2020-03-30 19:50:27 +00:00
|
|
|
/>
|
|
|
|
</>
|
|
|
|
);
|
|
|
|
};
|
|
|
|
|
|
|
|
export default SelectChannel;
|