2020-08-28 11:17:19 -07:00
|
|
|
import React from 'react';
|
|
|
|
import { connect } from 'react-redux';
|
|
|
|
import PropTypes from 'prop-types';
|
|
|
|
import ImmutablePropTypes from 'react-immutable-proptypes';
|
|
|
|
import { injectIntl, defineMessages } from 'react-intl';
|
|
|
|
import ImmutablePureComponent from 'react-immutable-pure-component';
|
|
|
|
import {
|
|
|
|
sendChatMessage,
|
|
|
|
markChatRead,
|
|
|
|
} from 'soapbox/actions/chats';
|
|
|
|
import { OrderedSet as ImmutableOrderedSet } from 'immutable';
|
|
|
|
import ChatMessageList from './chat_message_list';
|
2020-09-05 19:48:25 -07:00
|
|
|
import UploadButton from 'soapbox/features/compose/components/upload_button';
|
2020-09-06 10:37:38 -07:00
|
|
|
import { uploadMedia } from 'soapbox/actions/media';
|
2020-09-06 11:55:24 -07:00
|
|
|
import { Map as ImmutableMap } from 'immutable';
|
2020-08-28 11:17:19 -07:00
|
|
|
|
|
|
|
const messages = defineMessages({
|
|
|
|
placeholder: { id: 'chat_box.input.placeholder', defaultMessage: 'Send a message…' },
|
|
|
|
});
|
|
|
|
|
|
|
|
const mapStateToProps = (state, { chatId }) => ({
|
|
|
|
me: state.get('me'),
|
|
|
|
chat: state.getIn(['chats', chatId]),
|
|
|
|
chatMessageIds: state.getIn(['chat_message_lists', chatId], ImmutableOrderedSet()),
|
|
|
|
});
|
|
|
|
|
|
|
|
export default @connect(mapStateToProps)
|
|
|
|
@injectIntl
|
|
|
|
class ChatBox extends ImmutablePureComponent {
|
|
|
|
|
|
|
|
static propTypes = {
|
|
|
|
dispatch: PropTypes.func.isRequired,
|
|
|
|
intl: PropTypes.object.isRequired,
|
|
|
|
chatId: PropTypes.string.isRequired,
|
|
|
|
chatMessageIds: ImmutablePropTypes.orderedSet,
|
|
|
|
chat: ImmutablePropTypes.map,
|
|
|
|
onSetInputRef: PropTypes.func,
|
|
|
|
me: PropTypes.node,
|
|
|
|
}
|
|
|
|
|
2020-09-06 11:55:24 -07:00
|
|
|
initialParams = {
|
2020-08-28 11:17:19 -07:00
|
|
|
content: '',
|
2020-09-06 10:37:38 -07:00
|
|
|
media_id: undefined,
|
2020-08-28 11:17:19 -07:00
|
|
|
}
|
|
|
|
|
2020-09-06 11:55:24 -07:00
|
|
|
state = {
|
|
|
|
params: ImmutableMap(this.initialParams),
|
|
|
|
}
|
|
|
|
|
|
|
|
setParams = newParams => {
|
|
|
|
const { params } = this.state;
|
|
|
|
this.setState({ params: params.merge(newParams) });
|
|
|
|
}
|
|
|
|
|
|
|
|
clearParams = () => {
|
|
|
|
this.setState({ params: ImmutableMap(this.initialParams) });
|
|
|
|
}
|
|
|
|
|
2020-09-03 12:11:33 -07:00
|
|
|
sendMessage = () => {
|
2020-08-28 11:17:19 -07:00
|
|
|
const { chatId } = this.props;
|
2020-09-06 11:55:24 -07:00
|
|
|
const { params } = this.state;
|
|
|
|
|
|
|
|
const conds = [
|
|
|
|
params.get('content', '').length > 0,
|
|
|
|
params.get('media_id'),
|
|
|
|
];
|
|
|
|
|
|
|
|
if (conds.some(c => c)) {
|
|
|
|
this.props.dispatch(sendChatMessage(chatId, params.toJS()));
|
|
|
|
this.clearParams();
|
|
|
|
}
|
2020-09-03 12:11:33 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
insertLine = () => {
|
2020-09-06 11:55:24 -07:00
|
|
|
const { params } = this.state;
|
|
|
|
this.setParams({ content: params.get('content') + '\n' });
|
2020-09-03 12:11:33 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
handleKeyDown = (e) => {
|
|
|
|
if (e.key === 'Enter' && e.shiftKey) {
|
|
|
|
this.insertLine();
|
|
|
|
e.preventDefault();
|
|
|
|
} else if (e.key === 'Enter') {
|
|
|
|
this.sendMessage();
|
2020-08-28 11:17:19 -07:00
|
|
|
e.preventDefault();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
handleContentChange = (e) => {
|
2020-09-06 11:55:24 -07:00
|
|
|
this.setParams({ content: e.target.value });
|
2020-08-28 11:17:19 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
markRead = () => {
|
|
|
|
const { dispatch, chatId } = this.props;
|
|
|
|
dispatch(markChatRead(chatId));
|
|
|
|
}
|
|
|
|
|
|
|
|
handleHover = () => {
|
|
|
|
this.markRead();
|
|
|
|
}
|
|
|
|
|
|
|
|
setInputRef = (el) => {
|
|
|
|
const { onSetInputRef } = this.props;
|
|
|
|
this.inputElem = el;
|
|
|
|
onSetInputRef(el);
|
|
|
|
};
|
|
|
|
|
|
|
|
componentDidUpdate(prevProps) {
|
|
|
|
const markReadConditions = [
|
|
|
|
() => this.props.chat !== undefined,
|
|
|
|
() => document.activeElement === this.inputElem,
|
|
|
|
() => this.props.chat.get('unread') > 0,
|
|
|
|
];
|
|
|
|
|
|
|
|
if (markReadConditions.every(c => c() === true))
|
|
|
|
this.markRead();
|
|
|
|
}
|
|
|
|
|
2020-09-05 19:48:25 -07:00
|
|
|
handleFiles = (files) => {
|
2020-09-06 10:37:38 -07:00
|
|
|
const data = new FormData();
|
|
|
|
data.append('file', files[0]);
|
|
|
|
this.props.dispatch(uploadMedia(data)).then(response => {
|
2020-09-06 11:55:24 -07:00
|
|
|
this.setParams({ media_id: response.data.id });
|
2020-09-06 10:37:38 -07:00
|
|
|
}).catch(() => {});
|
2020-09-05 19:48:25 -07:00
|
|
|
}
|
|
|
|
|
2020-08-28 11:17:19 -07:00
|
|
|
render() {
|
2020-09-03 17:23:00 -07:00
|
|
|
const { chatMessageIds, chatId, intl } = this.props;
|
2020-09-06 11:55:24 -07:00
|
|
|
const { params } = this.state;
|
2020-08-28 11:17:19 -07:00
|
|
|
if (!chatMessageIds) return null;
|
|
|
|
|
|
|
|
return (
|
|
|
|
<div className='chat-box' onMouseOver={this.handleHover}>
|
2020-09-03 17:23:00 -07:00
|
|
|
<ChatMessageList chatMessageIds={chatMessageIds} chatId={chatId} />
|
2020-08-28 11:17:19 -07:00
|
|
|
<div className='chat-box__actions simple_form'>
|
2020-09-05 19:48:25 -07:00
|
|
|
<UploadButton onSelectFile={this.handleFiles} />
|
2020-08-28 11:17:19 -07:00
|
|
|
<textarea
|
|
|
|
rows={1}
|
|
|
|
placeholder={intl.formatMessage(messages.placeholder)}
|
|
|
|
onKeyDown={this.handleKeyDown}
|
|
|
|
onChange={this.handleContentChange}
|
2020-09-06 11:55:24 -07:00
|
|
|
value={params.get('content', '')}
|
2020-08-28 11:17:19 -07:00
|
|
|
ref={this.setInputRef}
|
|
|
|
/>
|
|
|
|
</div>
|
|
|
|
</div>
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|