2020-03-30 19:50:27 +00:00
|
|
|
import React, { useState } from 'react';
|
|
|
|
import { Text } from 'react-native';
|
|
|
|
|
2022-04-07 14:10:03 +00:00
|
|
|
import { themes } from '../../lib/constants';
|
2022-03-08 14:24:21 +00:00
|
|
|
import { MultiSelect } from '../../containers/UIKit/MultiSelect';
|
|
|
|
import { ISearchLocal } from '../../definitions';
|
|
|
|
import I18n from '../../i18n';
|
|
|
|
import RocketChat from '../../lib/rocketchat';
|
|
|
|
import { avatarURL } from '../../utils/avatar';
|
|
|
|
import debounce from '../../utils/debounce';
|
2021-09-15 19:58:14 +00:00
|
|
|
import { ICreateDiscussionViewSelectChannel } from './interfaces';
|
2022-03-08 14:24:21 +00:00
|
|
|
import styles from './styles';
|
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 => {
|
2022-03-08 14:24:21 +00:00
|
|
|
const [channels, setChannels] = useState<ISearchLocal[]>([]);
|
2020-03-30 19:50:27 +00:00
|
|
|
|
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
|
|
|
|
inputStyle={styles.inputStyle}
|
|
|
|
onChange={onChannelSelect}
|
|
|
|
onSearch={getChannels}
|
|
|
|
value={initial && [initial]}
|
2022-03-29 20:06:50 +00:00
|
|
|
disabled={!!initial}
|
2020-03-30 19:50:27 +00:00
|
|
|
options={channels.map(channel => ({
|
2022-04-08 22:53:48 +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;
|