2020-03-30 19:50:27 +00:00
|
|
|
import React, { useState } from 'react';
|
|
|
|
import { Text } from 'react-native';
|
|
|
|
import PropTypes from 'prop-types';
|
|
|
|
|
|
|
|
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';
|
|
|
|
|
|
|
|
const SelectChannel = ({
|
2020-11-04 16:53:44 +00:00
|
|
|
server, token, userId, onChannelSelect, initial, blockUnauthenticatedAccess, serverVersion, theme
|
2020-03-30 19:50:27 +00:00
|
|
|
}) => {
|
|
|
|
const [channels, setChannels] = useState([]);
|
|
|
|
|
|
|
|
const getChannels = debounce(async(keyword = '') => {
|
|
|
|
try {
|
2021-01-25 17:14:45 +00:00
|
|
|
const res = await RocketChat.localSearch({ text: keyword });
|
2020-03-30 19:50:27 +00:00
|
|
|
setChannels(res);
|
|
|
|
} catch {
|
|
|
|
// do nothing
|
|
|
|
}
|
|
|
|
}, 300);
|
|
|
|
|
2020-10-30 13:51:04 +00:00
|
|
|
const getAvatar = item => avatarURL({
|
|
|
|
text: RocketChat.getRoomAvatar(item),
|
|
|
|
type: item.t,
|
|
|
|
user: { id: userId, token },
|
|
|
|
server,
|
|
|
|
avatarETag: item.avatarETag,
|
2020-10-30 15:54:02 +00:00
|
|
|
rid: item.rid,
|
2020-11-04 16:53:44 +00:00
|
|
|
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-25 17:14:45 +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([])}
|
|
|
|
placeholder={{ text: `${ I18n.t('Select_a_Channel') }...` }}
|
|
|
|
/>
|
|
|
|
</>
|
|
|
|
);
|
|
|
|
};
|
|
|
|
SelectChannel.propTypes = {
|
|
|
|
server: PropTypes.string,
|
|
|
|
token: PropTypes.string,
|
|
|
|
userId: PropTypes.string,
|
|
|
|
initial: PropTypes.object,
|
|
|
|
onChannelSelect: 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 SelectChannel;
|