2017-02-17 00:46:39 +00:00
|
|
|
import React, { PureComponent } from 'react';
|
2017-03-23 19:38:27 +00:00
|
|
|
import { List } from 'react-virtualized/dist/commonjs/List';
|
|
|
|
import { AutoSizer } from 'react-virtualized/dist/commonjs/AutoSizer';
|
2016-02-16 21:43:25 +00:00
|
|
|
import Message from './Message';
|
2017-03-23 19:38:27 +00:00
|
|
|
import { scrollBarWidth } from '../util';
|
2015-12-28 23:34:32 +00:00
|
|
|
|
2017-03-23 19:38:27 +00:00
|
|
|
const sbWidth = scrollBarWidth();
|
|
|
|
const listStyle = { padding: '7px 0', boxSizing: 'content-box' };
|
2015-12-28 23:34:32 +00:00
|
|
|
|
2017-03-23 19:38:27 +00:00
|
|
|
export default class MessageBox extends PureComponent {
|
|
|
|
componentWillReceiveProps(nextProps) {
|
|
|
|
if (nextProps.tab !== this.props.tab) {
|
|
|
|
this.bottom = true;
|
|
|
|
}
|
2015-12-28 23:34:32 +00:00
|
|
|
}
|
|
|
|
|
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() {
|
2017-03-23 19:38:27 +00:00
|
|
|
if (this.bottom) {
|
|
|
|
this.list.scrollToRow(this.props.messages.size);
|
2015-12-28 23:34:32 +00:00
|
|
|
}
|
|
|
|
|
2017-03-23 19:38:27 +00:00
|
|
|
this.updateWidth();
|
2015-12-28 23:34:32 +00:00
|
|
|
}
|
|
|
|
|
2017-03-23 19:38:27 +00:00
|
|
|
getRowHeight = ({ index }) => this.props.messages.get(index).height;
|
2016-02-16 21:43:25 +00:00
|
|
|
|
2017-03-23 19:38:27 +00:00
|
|
|
listRef = el => { this.list = el; };
|
2016-02-16 21:43:25 +00:00
|
|
|
|
2017-03-28 21:55:50 +00:00
|
|
|
updateWidth = (width) => {
|
2016-02-16 21:43:25 +00:00
|
|
|
const { isChannel, setWrapWidth, updateMessageHeight } = this.props;
|
2017-03-28 21:55:50 +00:00
|
|
|
let wrapWidth = width || this.width;
|
2016-02-16 21:43:25 +00:00
|
|
|
|
2017-03-28 21:55:50 +00:00
|
|
|
if (width) {
|
|
|
|
if (isChannel && window.innerWidth > 600) {
|
|
|
|
wrapWidth += 200;
|
|
|
|
}
|
|
|
|
|
|
|
|
this.width = wrapWidth;
|
2017-03-23 19:38:27 +00:00
|
|
|
}
|
2016-02-16 21:43:25 +00:00
|
|
|
|
2017-03-23 19:38:27 +00:00
|
|
|
// eslint-disable-next-line no-underscore-dangle
|
|
|
|
const c = this.list.Grid._scrollingContainer;
|
|
|
|
if (c.scrollHeight > c.clientHeight) {
|
|
|
|
wrapWidth -= sbWidth;
|
|
|
|
}
|
2016-02-16 21:43:25 +00:00
|
|
|
|
2017-03-23 19:38:27 +00:00
|
|
|
if (this.wrapWidth !== wrapWidth) {
|
|
|
|
this.wrapWidth = wrapWidth;
|
|
|
|
setWrapWidth(wrapWidth);
|
|
|
|
updateMessageHeight();
|
2015-12-28 23:34:32 +00:00
|
|
|
}
|
2016-01-06 23:28:11 +00:00
|
|
|
};
|
2015-12-28 23:34:32 +00:00
|
|
|
|
2017-03-23 19:38:27 +00:00
|
|
|
handleResize = size => {
|
2017-03-28 21:55:50 +00:00
|
|
|
this.updateWidth(size.width - 30);
|
2016-01-06 23:28:11 +00:00
|
|
|
};
|
2015-12-28 23:34:32 +00:00
|
|
|
|
2017-03-23 19:38:27 +00:00
|
|
|
handleScroll = ({ scrollTop, clientHeight, scrollHeight }) => {
|
|
|
|
this.bottom = scrollTop + clientHeight >= scrollHeight;
|
|
|
|
};
|
2016-02-16 21:43:25 +00:00
|
|
|
|
2017-03-23 19:38:27 +00:00
|
|
|
renderMessage = ({ index, style, key }) => {
|
|
|
|
const { messages, select, openPrivateChat } = this.props;
|
2015-12-28 23:34:32 +00:00
|
|
|
|
2016-02-16 21:43:25 +00:00
|
|
|
return (
|
|
|
|
<Message
|
2017-03-23 19:38:27 +00:00
|
|
|
key={key}
|
|
|
|
message={messages.get(index)}
|
2016-02-16 21:43:25 +00:00
|
|
|
select={select}
|
|
|
|
openPrivateChat={openPrivateChat}
|
2017-03-23 19:38:27 +00:00
|
|
|
style={style}
|
2016-02-16 21:43:25 +00:00
|
|
|
/>
|
|
|
|
);
|
|
|
|
};
|
|
|
|
|
|
|
|
render() {
|
2015-12-28 23:34:32 +00:00
|
|
|
return (
|
|
|
|
<div className="messagebox">
|
2017-03-23 19:38:27 +00:00
|
|
|
<AutoSizer onResize={this.handleResize}>
|
|
|
|
{({ width, height }) => (
|
|
|
|
<List
|
|
|
|
ref={this.listRef}
|
|
|
|
width={width}
|
|
|
|
height={height - 14}
|
|
|
|
rowCount={this.props.messages.size}
|
|
|
|
rowHeight={this.getRowHeight}
|
|
|
|
rowRenderer={this.renderMessage}
|
|
|
|
onScroll={this.handleScroll}
|
|
|
|
style={listStyle}
|
|
|
|
/>
|
|
|
|
)}
|
|
|
|
</AutoSizer>
|
2015-12-28 23:34:32 +00:00
|
|
|
</div>
|
|
|
|
);
|
|
|
|
}
|
|
|
|
}
|