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';
|
2018-04-05 19:13:32 +00:00
|
|
|
import { getScrollPos, saveScrollPos } from 'utils/scrollPosition';
|
2016-02-16 21:43:25 +00:00
|
|
|
import Message from './Message';
|
2015-12-28 23:34:32 +00:00
|
|
|
|
2017-06-23 01:40:53 +00:00
|
|
|
const fetchThreshold = 600;
|
|
|
|
// The amount of time in ms that needs to pass without any
|
|
|
|
// scroll events happening before adding messages to the top,
|
|
|
|
// this is done to prevent the scroll from jumping all over the place
|
|
|
|
const scrollbackDebounce = 100;
|
2015-12-28 23:34:32 +00:00
|
|
|
|
2017-03-23 19:38:27 +00:00
|
|
|
export default class MessageBox extends PureComponent {
|
2017-05-07 20:19:15 +00:00
|
|
|
componentWillMount() {
|
2017-05-03 20:56:06 +00:00
|
|
|
this.loadScrollPos();
|
|
|
|
}
|
|
|
|
|
2017-05-07 20:19:15 +00:00
|
|
|
componentDidMount() {
|
2017-05-08 21:22:53 +00:00
|
|
|
this.mounted = true;
|
2017-05-07 20:19:15 +00:00
|
|
|
this.scrollTop = -1;
|
|
|
|
}
|
|
|
|
|
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) {
|
2017-05-03 20:56:06 +00:00
|
|
|
this.saveScrollPos();
|
2017-05-07 20:19:15 +00:00
|
|
|
this.bottom = false;
|
2017-03-23 19:38:27 +00:00
|
|
|
}
|
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) {
|
2018-04-05 23:46:22 +00:00
|
|
|
const addedMessages =
|
|
|
|
nextProps.messages.size - this.props.messages.size;
|
2017-05-02 21:21:25 +00:00
|
|
|
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;
|
2017-06-06 22:04:37 +00:00
|
|
|
this.ready = false;
|
2016-02-16 21:43:25 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-05-03 20:56:06 +00:00
|
|
|
componentDidUpdate(prevProps) {
|
|
|
|
if (prevProps.tab !== this.props.tab) {
|
2017-05-07 20:19:15 +00:00
|
|
|
this.loadScrollPos(true);
|
2017-05-03 20:56:06 +00:00
|
|
|
}
|
|
|
|
|
2017-05-02 21:21:25 +00:00
|
|
|
if (this.nextScrollTop > 0) {
|
|
|
|
this.container.scrollTop = this.nextScrollTop;
|
|
|
|
this.nextScrollTop = 0;
|
|
|
|
} else if (this.bottom) {
|
2017-05-03 20:56:06 +00:00
|
|
|
this.list.scrollToRow(this.props.messages.size);
|
2015-12-28 23:34:32 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-05-03 20:56:06 +00:00
|
|
|
componentWillUnmount() {
|
|
|
|
this.saveScrollPos();
|
|
|
|
}
|
|
|
|
|
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;
|
2017-05-07 20:19:15 +00:00
|
|
|
};
|
2016-02-16 21:43:25 +00:00
|
|
|
|
2017-05-02 21:21:25 +00:00
|
|
|
listRef = el => {
|
|
|
|
this.list = el;
|
2017-05-03 20:56:06 +00:00
|
|
|
if (el) {
|
|
|
|
// eslint-disable-next-line no-underscore-dangle
|
|
|
|
this.container = el.Grid._scrollingContainer;
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
updateScrollKey = () => {
|
|
|
|
const { tab } = this.props;
|
|
|
|
this.scrollKey = `msg:${tab.server}:${tab.name}`;
|
|
|
|
return this.scrollKey;
|
2017-05-07 20:19:15 +00:00
|
|
|
};
|
2017-05-03 20:56:06 +00:00
|
|
|
|
2017-05-07 20:19:15 +00:00
|
|
|
loadScrollPos = scroll => {
|
2017-05-03 20:56:06 +00:00
|
|
|
const pos = getScrollPos(this.updateScrollKey());
|
|
|
|
if (pos >= 0) {
|
|
|
|
this.bottom = false;
|
2017-05-07 20:19:15 +00:00
|
|
|
if (scroll) {
|
|
|
|
this.list.scrollToPosition(pos);
|
|
|
|
} else {
|
|
|
|
this.scrollTop = pos;
|
|
|
|
}
|
2017-05-03 20:56:06 +00:00
|
|
|
} else {
|
|
|
|
this.bottom = true;
|
2017-05-07 20:19:15 +00:00
|
|
|
if (scroll) {
|
|
|
|
this.list.scrollToRow(this.props.messages.size);
|
|
|
|
}
|
2017-05-03 20:56:06 +00:00
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
saveScrollPos = () => {
|
|
|
|
if (this.bottom) {
|
|
|
|
saveScrollPos(this.scrollKey, -1);
|
|
|
|
} else {
|
|
|
|
saveScrollPos(this.scrollKey, this.container.scrollTop);
|
|
|
|
}
|
2016-01-06 23:28:11 +00:00
|
|
|
};
|
2015-12-28 23:34:32 +00:00
|
|
|
|
2017-06-06 22:04:37 +00:00
|
|
|
fetchMore = () => {
|
2017-05-02 21:21:25 +00:00
|
|
|
this.loading = true;
|
|
|
|
this.props.onFetchMore();
|
2017-06-06 22:04:37 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
addMore = debounce(() => {
|
|
|
|
const { tab, onAddMore } = this.props;
|
|
|
|
this.ready = true;
|
|
|
|
onAddMore(tab.server, tab.name);
|
2017-06-23 01:40:53 +00:00
|
|
|
}, scrollbackDebounce);
|
2017-05-02 21:21:25 +00:00
|
|
|
|
2017-03-23 19:38:27 +00:00
|
|
|
handleScroll = ({ scrollTop, clientHeight, scrollHeight }) => {
|
2017-05-08 21:22:53 +00:00
|
|
|
if (this.mounted) {
|
2018-04-05 23:46:22 +00:00
|
|
|
if (
|
|
|
|
!this.loading &&
|
2017-06-06 22:04:37 +00:00
|
|
|
this.props.hasMoreMessages &&
|
2017-05-08 21:22:53 +00:00
|
|
|
scrollTop <= fetchThreshold &&
|
2018-04-05 23:46:22 +00:00
|
|
|
scrollTop < this.prevScrollTop
|
|
|
|
) {
|
2017-06-06 22:04:37 +00:00
|
|
|
this.fetchMore();
|
|
|
|
}
|
|
|
|
|
|
|
|
if (this.loading && !this.ready) {
|
2017-05-08 21:22:53 +00:00
|
|
|
if (this.mouseDown) {
|
2017-06-06 22:04:37 +00:00
|
|
|
this.ready = true;
|
|
|
|
this.shouldAdd = true;
|
2017-05-08 21:22:53 +00:00
|
|
|
} else {
|
2017-06-06 22:04:37 +00:00
|
|
|
this.addMore();
|
2017-05-08 21:22:53 +00:00
|
|
|
}
|
2017-05-02 21:21:25 +00:00
|
|
|
}
|
|
|
|
|
2017-05-08 21:22:53 +00:00
|
|
|
this.bottom = scrollTop + clientHeight >= scrollHeight - 10;
|
|
|
|
this.prevScrollTop = scrollTop;
|
|
|
|
}
|
2017-05-02 21:21:25 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
handleMouseDown = () => {
|
|
|
|
this.mouseDown = true;
|
|
|
|
};
|
|
|
|
|
|
|
|
handleMouseUp = () => {
|
|
|
|
this.mouseDown = false;
|
|
|
|
|
2017-06-06 22:04:37 +00:00
|
|
|
if (this.shouldAdd) {
|
|
|
|
const { tab, onAddMore } = this.props;
|
|
|
|
this.shouldAdd = false;
|
|
|
|
onAddMore(tab.server, tab.name);
|
2017-05-02 21:21:25 +00:00
|
|
|
}
|
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 (
|
2018-04-05 23:46:22 +00:00
|
|
|
<div key="top" className="messagebox-top-indicator" style={style}>
|
2017-05-02 21:21:25 +00:00
|
|
|
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() {
|
2017-05-07 20:19:15 +00:00
|
|
|
const props = {};
|
|
|
|
if (this.bottom) {
|
|
|
|
props.scrollToIndex = this.props.messages.size;
|
|
|
|
} else if (this.scrollTop >= 0) {
|
|
|
|
props.scrollTop = this.scrollTop;
|
|
|
|
}
|
|
|
|
|
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-05-07 20:19:15 +00:00
|
|
|
<AutoSizer>
|
2017-03-23 19:38:27 +00:00
|
|
|
{({ 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}
|
2017-05-07 20:19:15 +00:00
|
|
|
className="rvlist-messages"
|
|
|
|
{...props}
|
2017-03-23 19:38:27 +00:00
|
|
|
/>
|
|
|
|
)}
|
|
|
|
</AutoSizer>
|
2015-12-28 23:34:32 +00:00
|
|
|
</div>
|
|
|
|
);
|
|
|
|
}
|
|
|
|
}
|