2020-08-25 18:33:49 -07:00
|
|
|
import React from 'react';
|
|
|
|
import { connect } from 'react-redux';
|
|
|
|
import PropTypes from 'prop-types';
|
|
|
|
import ImmutablePropTypes from 'react-immutable-proptypes';
|
|
|
|
import { injectIntl } from 'react-intl';
|
|
|
|
import ImmutablePureComponent from 'react-immutable-pure-component';
|
|
|
|
import Avatar from 'soapbox/components/avatar';
|
|
|
|
import { acctFull } from 'soapbox/utils/accounts';
|
|
|
|
import IconButton from 'soapbox/components/icon_button';
|
2020-08-25 19:31:34 -07:00
|
|
|
import { closeChat, toggleChat, fetchChatMessages, sendChatMessage } from 'soapbox/actions/chats';
|
2020-08-26 14:12:42 -07:00
|
|
|
import { List as ImmutableList, OrderedSet as ImmutableOrderedSet } from 'immutable';
|
|
|
|
import ChatMessageList from './chat_message_list';
|
2020-08-25 18:33:49 -07:00
|
|
|
|
|
|
|
const mapStateToProps = (state, { pane }) => ({
|
2020-08-25 22:21:54 -07:00
|
|
|
me: state.get('me'),
|
2020-08-26 14:12:42 -07:00
|
|
|
chatMessageIds: state.getIn(['chat_message_lists', pane.get('chat_id')], ImmutableOrderedSet()),
|
2020-08-25 18:33:49 -07:00
|
|
|
});
|
|
|
|
|
|
|
|
export default @connect(mapStateToProps)
|
|
|
|
@injectIntl
|
|
|
|
class ChatWindow extends ImmutablePureComponent {
|
|
|
|
|
|
|
|
static propTypes = {
|
|
|
|
dispatch: PropTypes.func.isRequired,
|
|
|
|
intl: PropTypes.object.isRequired,
|
|
|
|
pane: ImmutablePropTypes.map.isRequired,
|
|
|
|
idx: PropTypes.number,
|
2020-08-26 14:12:42 -07:00
|
|
|
chatMessageIds: ImmutablePropTypes.orderedSet,
|
2020-08-25 22:21:54 -07:00
|
|
|
me: PropTypes.node,
|
2020-08-25 18:33:49 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
static defaultProps = {
|
|
|
|
chatMessages: ImmutableList(),
|
|
|
|
}
|
|
|
|
|
2020-08-25 19:31:34 -07:00
|
|
|
state = {
|
|
|
|
content: '',
|
|
|
|
}
|
|
|
|
|
2020-08-25 18:33:49 -07:00
|
|
|
handleChatClose = (chatId) => {
|
|
|
|
return (e) => {
|
|
|
|
this.props.dispatch(closeChat(chatId));
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
|
|
|
handleChatToggle = (chatId) => {
|
|
|
|
return (e) => {
|
|
|
|
this.props.dispatch(toggleChat(chatId));
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
2020-08-25 19:31:34 -07:00
|
|
|
handleKeyDown = (chatId) => {
|
|
|
|
return (e) => {
|
|
|
|
if (e.key === 'Enter') {
|
|
|
|
this.props.dispatch(sendChatMessage(chatId, this.state));
|
|
|
|
this.setState({ content: '' });
|
|
|
|
}
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
|
|
|
handleContentChange = (e) => {
|
|
|
|
this.setState({ content: e.target.value });
|
|
|
|
}
|
|
|
|
|
2020-08-25 20:03:53 -07:00
|
|
|
focusInput = () => {
|
|
|
|
if (!this.inputElem) return;
|
|
|
|
this.inputElem.focus();
|
|
|
|
}
|
|
|
|
|
2020-08-25 20:12:08 -07:00
|
|
|
setInputRef = (el) => {
|
|
|
|
const { pane } = this.props;
|
|
|
|
this.inputElem = el;
|
|
|
|
if (pane.get('state') === 'open') this.focusInput();
|
|
|
|
};
|
2020-08-25 19:45:05 -07:00
|
|
|
|
2020-08-25 18:33:49 -07:00
|
|
|
componentDidMount() {
|
|
|
|
const { dispatch, pane, chatMessages } = this.props;
|
|
|
|
if (chatMessages && chatMessages.count() < 1)
|
|
|
|
dispatch(fetchChatMessages(pane.get('chat_id')));
|
|
|
|
}
|
|
|
|
|
2020-08-25 20:03:53 -07:00
|
|
|
componentDidUpdate(prevProps) {
|
|
|
|
const oldState = prevProps.pane.get('state');
|
|
|
|
const newState = this.props.pane.get('state');
|
|
|
|
|
|
|
|
if (oldState !== newState && newState === 'open')
|
|
|
|
this.focusInput();
|
2020-08-25 19:45:05 -07:00
|
|
|
}
|
|
|
|
|
2020-08-25 18:33:49 -07:00
|
|
|
render() {
|
2020-08-26 14:12:42 -07:00
|
|
|
const { pane, idx, chatMessageIds } = this.props;
|
2020-08-25 18:33:49 -07:00
|
|
|
const chat = pane.get('chat');
|
|
|
|
const account = pane.getIn(['chat', 'account']);
|
|
|
|
if (!chat || !account) return null;
|
|
|
|
|
|
|
|
const right = (285 * (idx + 1)) + 20;
|
|
|
|
|
|
|
|
return (
|
|
|
|
<div className={`pane pane--${pane.get('state')}`} style={{ right: `${right}px` }}>
|
|
|
|
<div className='pane__header'>
|
|
|
|
<Avatar account={account} size={18} />
|
|
|
|
<button className='pane__title' onClick={this.handleChatToggle(chat.get('id'))}>
|
|
|
|
@{acctFull(account)}
|
|
|
|
</button>
|
|
|
|
<div className='pane__close'>
|
|
|
|
<IconButton icon='close' title='Close chat' onClick={this.handleChatClose(chat.get('id'))} />
|
|
|
|
</div>
|
|
|
|
</div>
|
|
|
|
<div className='pane__content'>
|
2020-08-26 14:12:42 -07:00
|
|
|
<ChatMessageList chatMessageIds={chatMessageIds} />
|
2020-08-25 18:33:49 -07:00
|
|
|
<div className='pane__actions'>
|
2020-08-25 19:31:34 -07:00
|
|
|
<input
|
|
|
|
type='text'
|
|
|
|
placeholder='Send a message...'
|
|
|
|
onKeyDown={this.handleKeyDown(chat.get('id'))}
|
|
|
|
onChange={this.handleContentChange}
|
|
|
|
value={this.state.content}
|
2020-08-25 20:03:53 -07:00
|
|
|
ref={this.setInputRef}
|
2020-08-25 19:31:34 -07:00
|
|
|
/>
|
2020-08-25 18:33:49 -07:00
|
|
|
</div>
|
|
|
|
</div>
|
|
|
|
</div>
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|