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';
|
2017-05-02 21:21:25 +00:00
|
|
|
import debounce from 'lodash/debounce';
|
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' };
|
2017-05-02 21:21:25 +00:00
|
|
|
const threshold = 100;
|
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-05-02 21:21:25 +00:00
|
|
|
if (nextProps.messages !== this.props.messages) {
|
|
|
|
this.list.recomputeRowHeights();
|
|
|
|
}
|
|
|
|
|
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
|
|
|
|
2017-05-02 21:21:25 +00:00
|
|
|
if (nextProps.messages.get(0) !== this.props.messages.get(0)) {
|
|
|
|
if (nextProps.tab === this.props.tab) {
|
|
|
|
const addedMessages = nextProps.messages.size - this.props.messages.size;
|
|
|
|
let addedHeight = 0;
|
|
|
|
for (let i = 0; i < addedMessages; i++) {
|
|
|
|
addedHeight += nextProps.messages.get(i).height;
|
|
|
|
}
|
|
|
|
|
|
|
|
this.nextScrollTop = addedHeight + this.container.scrollTop;
|
|
|
|
}
|
|
|
|
|
|
|
|
this.loading = false;
|
2016-02-16 21:43:25 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-12-28 23:34:32 +00:00
|
|
|
componentDidUpdate() {
|
2017-05-02 21:21:25 +00:00
|
|
|
if (this.nextScrollTop > 0) {
|
|
|
|
this.container.scrollTop = this.nextScrollTop;
|
|
|
|
this.nextScrollTop = 0;
|
|
|
|
} else if (this.bottom) {
|
|
|
|
this.container.scrollTop = this.container.scrollHeight;
|
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-05-02 21:21:25 +00:00
|
|
|
getRowHeight = ({ index }) => {
|
|
|
|
if (index === 0) {
|
|
|
|
if (this.props.hasMoreMessages) {
|
|
|
|
return 100;
|
|
|
|
}
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
return this.props.messages.get(index - 1).height;
|
|
|
|
}
|
2016-02-16 21:43:25 +00:00
|
|
|
|
2017-05-02 21:21:25 +00:00
|
|
|
listRef = el => {
|
|
|
|
this.list = el;
|
|
|
|
// eslint-disable-next-line no-underscore-dangle
|
|
|
|
this.container = el.Grid._scrollingContainer;
|
|
|
|
};
|
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-05-02 21:21:25 +00:00
|
|
|
if (this.container.scrollHeight > this.container.clientHeight) {
|
2017-03-30 02:18:25 +00:00
|
|
|
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-05-02 21:21:25 +00:00
|
|
|
fetchMore = debounce(() => {
|
|
|
|
this.loading = true;
|
|
|
|
this.props.onFetchMore();
|
|
|
|
}, 100);
|
|
|
|
|
2017-03-23 19:38:27 +00:00
|
|
|
handleScroll = ({ scrollTop, clientHeight, scrollHeight }) => {
|
2017-05-02 21:21:25 +00:00
|
|
|
if (this.props.hasMoreMessages &&
|
|
|
|
scrollTop <= threshold &&
|
|
|
|
scrollTop < this.prevScrollTop &&
|
|
|
|
!this.loading) {
|
|
|
|
if (this.mouseDown) {
|
|
|
|
this.shouldFetch = true;
|
|
|
|
} else {
|
|
|
|
this.fetchMore();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-03-23 19:38:27 +00:00
|
|
|
this.bottom = scrollTop + clientHeight >= scrollHeight;
|
2017-05-02 21:21:25 +00:00
|
|
|
this.prevScrollTop = scrollTop;
|
|
|
|
};
|
|
|
|
|
|
|
|
handleMouseDown = () => {
|
|
|
|
this.mouseDown = true;
|
|
|
|
};
|
|
|
|
|
|
|
|
handleMouseUp = () => {
|
|
|
|
this.mouseDown = false;
|
|
|
|
|
|
|
|
if (this.shouldFetch) {
|
|
|
|
this.shouldFetch = false;
|
|
|
|
this.loading = true;
|
|
|
|
this.props.onFetchMore();
|
|
|
|
}
|
2017-03-23 19:38:27 +00:00
|
|
|
};
|
2016-02-16 21:43:25 +00:00
|
|
|
|
2017-04-20 03:32:22 +00:00
|
|
|
renderMessage = ({ index, style }) => {
|
2017-05-02 21:21:25 +00:00
|
|
|
if (index === 0) {
|
|
|
|
if (this.props.hasMoreMessages) {
|
|
|
|
return (
|
|
|
|
<div
|
|
|
|
key="top"
|
|
|
|
className="messagebox-top-indicator"
|
|
|
|
style={style}
|
|
|
|
>
|
|
|
|
Loading messages...
|
|
|
|
</div>
|
|
|
|
);
|
|
|
|
}
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
|
2017-04-19 23:51:55 +00:00
|
|
|
const { messages, onNickClick } = this.props;
|
2017-05-02 21:21:25 +00:00
|
|
|
const message = messages.get(index - 1);
|
2015-12-28 23:34:32 +00:00
|
|
|
|
2016-02-16 21:43:25 +00:00
|
|
|
return (
|
|
|
|
<Message
|
2017-04-20 03:32:22 +00:00
|
|
|
key={message.id}
|
|
|
|
message={message}
|
2017-03-23 19:38:27 +00:00
|
|
|
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 (
|
2017-05-02 21:21:25 +00:00
|
|
|
<div
|
|
|
|
className="messagebox"
|
|
|
|
onMouseDown={this.handleMouseDown}
|
|
|
|
onMouseUp={this.handleMouseUp}
|
|
|
|
>
|
2017-03-23 19:38:27 +00:00
|
|
|
<AutoSizer onResize={this.handleResize}>
|
|
|
|
{({ width, height }) => (
|
|
|
|
<List
|
|
|
|
ref={this.listRef}
|
|
|
|
width={width}
|
|
|
|
height={height - 14}
|
2017-05-02 21:21:25 +00:00
|
|
|
rowCount={this.props.messages.size + 1}
|
2017-03-23 19:38:27 +00:00
|
|
|
rowHeight={this.getRowHeight}
|
|
|
|
rowRenderer={this.renderMessage}
|
|
|
|
onScroll={this.handleScroll}
|
|
|
|
style={listStyle}
|
|
|
|
/>
|
|
|
|
)}
|
|
|
|
</AutoSizer>
|
2015-12-28 23:34:32 +00:00
|
|
|
</div>
|
|
|
|
);
|
|
|
|
}
|
|
|
|
}
|