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-30 02:18:25 +00:00
|
|
|
import { measureScrollBarWidth } from '../util';
|
2015-12-28 23:34:32 +00:00
|
|
|
|
2017-03-30 02:18:25 +00:00
|
|
|
const scrollBarWidth = measureScrollBarWidth();
|
2017-03-23 19:38:27 +00:00
|
|
|
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 {
|
2017-03-30 02:37:55 +00:00
|
|
|
componentWillUpdate(nextProps) {
|
2017-03-23 19:38:27 +00:00
|
|
|
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
|
|
|
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) => {
|
2017-04-19 23:51:55 +00:00
|
|
|
const { tab, 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) {
|
2017-04-19 23:51:55 +00:00
|
|
|
if (tab.isChannel() && window.innerWidth > 600) {
|
2017-03-28 21:55:50 +00:00
|
|
|
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
|
2017-03-30 02:18:25 +00:00
|
|
|
const container = this.list.Grid._scrollingContainer;
|
|
|
|
if (container.scrollHeight > container.clientHeight) {
|
|
|
|
wrapWidth -= scrollBarWidth;
|
2017-03-23 19:38:27 +00:00
|
|
|
}
|
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 }) => {
|
2017-04-19 23:51:55 +00:00
|
|
|
const { messages, onNickClick } = 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)}
|
|
|
|
style={style}
|
2017-04-19 23:51:55 +00:00
|
|
|
onNickClick={onNickClick}
|
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>
|
|
|
|
);
|
|
|
|
}
|
|
|
|
}
|