import React from 'react';
import PropTypes from 'prop-types';
import { View, Text, Image } from 'react-native';
import { connect } from 'react-redux';
import ShareExtension from 'rn-extensions-share';
import { themes } from '../../constants/colors';
import I18n from '../../i18n';
import RocketChat from '../../lib/rocketchat';
import { CustomIcon } from '../../lib/Icons';
import log from '../../utils/log';
import styles from './styles';
import TextInput from '../../containers/TextInput';
import ActivityIndicator from '../../containers/ActivityIndicator';
import { CustomHeaderButtons, Item } from '../../containers/HeaderButton';
import { isBlocked } from '../../utils/room';
import { isReadOnly } from '../../utils/isReadOnly';
import { withTheme } from '../../theme';
class ShareView extends React.Component {
static propTypes = {
navigation: PropTypes.object,
route: PropTypes.object,
theme: PropTypes.string,
user: PropTypes.shape({
id: PropTypes.string.isRequired,
username: PropTypes.string.isRequired,
token: PropTypes.string.isRequired
}),
server: PropTypes.string
};
constructor(props) {
super(props);
const { route } = this.props;
const rid = route.params?.rid;
const name = route.params?.name;
const value = route.params?.value;
const isMedia = route.params?.isMedia ?? false;
const fileInfo = route.params?.fileInfo ?? {};
const room = route.params?.room ?? { rid };
this.state = {
rid,
value,
isMedia,
name,
fileInfo,
room,
loading: false,
readOnly: false,
file: {
name: fileInfo ? fileInfo.name : '',
description: ''
},
canSend: false
};
this.setReadOnly();
this.setHeader();
}
setHeader = () => {
const { canSend } = this.state;
const { navigation } = this.props;
navigation.setOptions({
title: I18n.t('Share'),
headerRight:
() => (canSend
? (
)
: null)
});
}
setReadOnly = async() => {
const { room } = this.state;
const { user } = this.props;
const { username } = user;
const readOnly = await isReadOnly(room, { username });
this.setState({ readOnly, canSend: !(readOnly || isBlocked(room)) }, () => this.setHeader());
}
bytesToSize = bytes => `${ (bytes / 1048576).toFixed(2) }MB`;
sendMessage = async() => {
const { isMedia, loading } = this.state;
if (loading) {
return;
}
this.setState({ loading: true });
if (isMedia) {
await this.sendMediaMessage();
} else {
await this.sendTextMessage();
}
this.setState({ loading: false });
ShareExtension.close();
}
sendMediaMessage = async() => {
const { rid, fileInfo, file } = this.state;
const { server, user } = this.props;
const { name, description } = file;
const fileMessage = {
name,
description,
size: fileInfo.size,
type: fileInfo.mime,
store: 'Uploads',
path: fileInfo.path
};
if (fileInfo && rid !== '') {
try {
await RocketChat.sendFileMessage(rid, fileMessage, undefined, server, user);
} catch (e) {
log(e);
}
}
}
sendTextMessage = async() => {
const { value, rid } = this.state;
const { user } = this.props;
if (value !== '' && rid !== '') {
try {
await RocketChat.sendMessage(rid, value, undefined, user);
} catch (e) {
log(e);
}
}
};
renderPreview = () => {
const { fileInfo } = this.state;
const { theme } = this.props;
const icon = fileInfo.mime.match(/image/)
?
: (
);
return (
{icon}
{fileInfo.name}
{this.bytesToSize(fileInfo.size)}
);
};
renderMediaContent = () => {
const { fileInfo, file } = this.state;
const { theme } = this.props;
const inputStyle = {
backgroundColor: themes[theme].focusedBackground,
borderColor: themes[theme].separatorColor
};
return fileInfo ? (
{this.renderPreview()}
this.setState({ file: { ...file, name } })}
defaultValue={file.name}
containerStyle={styles.inputContainer}
theme={theme}
/>
this.setState({ file: { ...file, description } })}
defaultValue={file.description}
multiline
textAlignVertical='top'
autoFocus
containerStyle={styles.inputContainer}
theme={theme}
/>
) : null;
};
renderInput = () => {
const { value } = this.state;
const { theme } = this.props;
return (
this.setState({ value: handleText })}
defaultValue={value}
multiline
textAlignVertical='top'
autoFocus
theme={theme}
/>
);
}
renderError = () => {
const { room } = this.state;
const { theme } = this.props;
return (
{
isBlocked(room) ? I18n.t('This_room_is_blocked') : I18n.t('This_room_is_read_only')
}
);
}
render() {
const { theme } = this.props;
const {
name, loading, isMedia, room, readOnly
} = this.state;
if (readOnly || isBlocked(room)) {
return this.renderError();
}
return (
{`${ I18n.t('To') }: `}
{`${ name }`}
{isMedia ? this.renderMediaContent() : this.renderInput()}
{ loading ? : null }
);
}
}
const mapStateToProps = (({ share }) => ({
user: {
id: share.user && share.user.id,
username: share.user && share.user.username,
token: share.user && share.user.token
},
server: share.server
}));
export default connect(mapStateToProps)(withTheme(ShareView));