2018-10-07 21:44:58 +00:00
|
|
|
import React from 'react';
|
|
|
|
import PropTypes from 'prop-types';
|
|
|
|
import ImmutablePropTypes from 'react-immutable-proptypes';
|
|
|
|
import ImmutablePureComponent from 'react-immutable-pure-component';
|
|
|
|
import ConversationContainer from '../containers/conversation_container';
|
|
|
|
import ScrollableList from '../../../components/scrollable_list';
|
|
|
|
import { debounce } from 'lodash';
|
|
|
|
|
|
|
|
export default class ConversationsList extends ImmutablePureComponent {
|
|
|
|
|
|
|
|
static propTypes = {
|
2018-10-25 23:55:08 +00:00
|
|
|
conversations: ImmutablePropTypes.list.isRequired,
|
2020-07-09 13:09:19 +00:00
|
|
|
scrollKey: PropTypes.string.isRequired,
|
2018-10-07 21:44:58 +00:00
|
|
|
hasMore: PropTypes.bool,
|
|
|
|
isLoading: PropTypes.bool,
|
|
|
|
onLoadMore: PropTypes.func,
|
|
|
|
shouldUpdateScroll: PropTypes.func,
|
|
|
|
};
|
|
|
|
|
2018-10-25 23:55:08 +00:00
|
|
|
getCurrentIndex = id => this.props.conversations.findIndex(x => x.get('id') === id)
|
2018-10-07 21:44:58 +00:00
|
|
|
|
|
|
|
handleMoveUp = id => {
|
|
|
|
const elementIndex = this.getCurrentIndex(id) - 1;
|
2019-05-03 04:20:36 +00:00
|
|
|
this._selectChild(elementIndex, true);
|
2018-10-07 21:44:58 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
handleMoveDown = id => {
|
|
|
|
const elementIndex = this.getCurrentIndex(id) + 1;
|
2019-05-03 04:20:36 +00:00
|
|
|
this._selectChild(elementIndex, false);
|
2018-10-07 21:44:58 +00:00
|
|
|
}
|
|
|
|
|
2019-05-03 04:20:36 +00:00
|
|
|
_selectChild (index, align_top) {
|
|
|
|
const container = this.node.node;
|
|
|
|
const element = container.querySelector(`article:nth-of-type(${index + 1}) .focusable`);
|
2018-10-07 21:44:58 +00:00
|
|
|
|
|
|
|
if (element) {
|
2019-05-03 04:20:36 +00:00
|
|
|
if (align_top && container.scrollTop > element.offsetTop) {
|
|
|
|
element.scrollIntoView(true);
|
|
|
|
} else if (!align_top && container.scrollTop + container.clientHeight < element.offsetTop + element.offsetHeight) {
|
|
|
|
element.scrollIntoView(false);
|
|
|
|
}
|
2018-10-07 21:44:58 +00:00
|
|
|
element.focus();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
setRef = c => {
|
|
|
|
this.node = c;
|
|
|
|
}
|
|
|
|
|
|
|
|
handleLoadOlder = debounce(() => {
|
2018-10-25 23:55:08 +00:00
|
|
|
const last = this.props.conversations.last();
|
2018-10-07 21:44:58 +00:00
|
|
|
|
2018-10-25 23:55:08 +00:00
|
|
|
if (last && last.get('last_status')) {
|
|
|
|
this.props.onLoadMore(last.get('last_status'));
|
2018-10-07 21:44:58 +00:00
|
|
|
}
|
|
|
|
}, 300, { leading: true })
|
|
|
|
|
|
|
|
render () {
|
2018-10-25 23:55:08 +00:00
|
|
|
const { conversations, onLoadMore, ...other } = this.props;
|
2018-10-07 21:44:58 +00:00
|
|
|
|
|
|
|
return (
|
2020-07-09 13:09:19 +00:00
|
|
|
<ScrollableList {...other} onLoadMore={onLoadMore && this.handleLoadOlder} ref={this.setRef}>
|
2018-10-25 23:55:08 +00:00
|
|
|
{conversations.map(item => (
|
2018-10-07 21:44:58 +00:00
|
|
|
<ConversationContainer
|
2018-10-25 23:55:08 +00:00
|
|
|
key={item.get('id')}
|
|
|
|
conversationId={item.get('id')}
|
2018-10-07 21:44:58 +00:00
|
|
|
onMoveUp={this.handleMoveUp}
|
|
|
|
onMoveDown={this.handleMoveDown}
|
2020-07-09 13:09:19 +00:00
|
|
|
scrollKey={this.props.scrollKey}
|
2018-10-07 21:44:58 +00:00
|
|
|
/>
|
|
|
|
))}
|
|
|
|
</ScrollableList>
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|