dispatch/client/src/js/components/MessageBox.js

111 lines
2.4 KiB
JavaScript
Raw Normal View History

2015-12-28 23:34:32 +00:00
import React, { Component } from 'react';
2016-02-16 21:43:25 +00:00
import { VirtualScroll } from 'react-virtualized';
2015-12-28 23:34:32 +00:00
import pure from 'pure-render-decorator';
2016-02-16 21:43:25 +00:00
import Message from './Message';
2015-12-28 23:34:32 +00:00
@pure
export default class MessageBox extends Component {
state = {
height: window.innerHeight - 100
};
2015-12-28 23:34:32 +00:00
componentDidMount() {
this.updateWidth();
window.addEventListener('resize', this.handleResize);
}
2016-02-16 21:43:25 +00:00
componentWillReceiveProps() {
2017-02-16 02:55:50 +00:00
const el = this.list.refs.scrollingContainer;
2015-12-28 23:34:32 +00:00
this.autoScroll = el.scrollTop + el.offsetHeight === el.scrollHeight;
}
2016-02-16 21:43:25 +00:00
componentWillUpdate(nextProps) {
if (nextProps.messages !== this.props.messages) {
2017-02-16 02:55:50 +00:00
this.list.recomputeRowHeights();
2016-02-16 21:43:25 +00:00
}
}
2015-12-28 23:34:32 +00:00
componentDidUpdate() {
2016-02-16 21:43:25 +00:00
this.updateWidth();
2015-12-28 23:34:32 +00:00
if (this.autoScroll) {
2017-02-16 02:55:50 +00:00
const el = this.list.refs.scrollingContainer;
2015-12-28 23:34:32 +00:00
el.scrollTop = el.scrollHeight;
}
}
componentWillUnmount() {
window.removeEventListener('resize', this.handleResize);
}
2016-02-16 21:43:25 +00:00
getRowHeight = index => {
const { messages } = this.props;
if (index === 0 || index === messages.size + 1) {
return 7;
}
return messages.get(index - 1).height;
};
updateWidth = resize => {
const { isChannel, setWrapWidth, updateMessageHeight } = this.props;
2017-02-16 02:55:50 +00:00
if (this.list) {
let width = this.list.refs.scrollingContainer.clientWidth - 30;
2016-02-16 21:43:25 +00:00
if (isChannel) {
width += 200;
}
2015-12-28 23:34:32 +00:00
if (this.width !== width) {
this.width = width;
setWrapWidth(width);
2016-02-16 21:43:25 +00:00
if (resize) {
updateMessageHeight();
}
2015-12-28 23:34:32 +00:00
}
}
};
2015-12-28 23:34:32 +00:00
listRef = el => { this.list = el; };
2015-12-28 23:34:32 +00:00
handleResize = () => {
2016-02-16 21:43:25 +00:00
this.updateWidth(true);
2015-12-28 23:34:32 +00:00
this.setState({ height: window.innerHeight - 100 });
};
2015-12-28 23:34:32 +00:00
2016-02-16 21:43:25 +00:00
renderMessage = index => {
const { messages } = this.props;
2015-12-28 23:34:32 +00:00
2016-02-16 21:43:25 +00:00
if (index === 0 || index === messages.size + 1) {
2017-02-16 02:55:50 +00:00
return <span style={{ height: '7px' }} />;
2016-02-16 21:43:25 +00:00
}
const { select, openPrivateChat } = this.props;
const message = messages.get(index - 1);
2015-12-28 23:34:32 +00:00
2016-02-16 21:43:25 +00:00
return (
<Message
message={message}
select={select}
openPrivateChat={openPrivateChat}
/>
);
};
render() {
2015-12-28 23:34:32 +00:00
return (
<div className="messagebox">
2016-02-16 21:43:25 +00:00
<VirtualScroll
ref={this.listRef}
2016-02-16 21:43:25 +00:00
height={this.state.height}
rowsCount={this.props.messages.size + 2}
rowHeight={this.getRowHeight}
rowRenderer={this.renderMessage}
/>
2015-12-28 23:34:32 +00:00
</div>
);
}
}