dispatch/client/js/components/pages/Chat/UserList.js

96 lines
2.1 KiB
JavaScript
Raw Normal View History

import React, { PureComponent, createRef } from 'react';
import { VariableSizeList as List } from 'react-window';
import AutoSizer from 'react-virtualized-auto-sizer';
import classnames from 'classnames';
2015-12-28 23:34:32 +00:00
import UserListItem from './UserListItem';
2017-03-23 19:38:27 +00:00
export default class UserList extends PureComponent {
list = createRef();
getSnapshotBeforeUpdate(prevProps) {
if (this.list.current) {
const { users } = this.props;
if (prevProps.users.length !== users.length) {
this.list.current.resetAfterIndex(
Math.min(prevProps.users.length, users.length) + 1
);
} else {
this.list.current.forceUpdate();
}
2016-02-16 21:43:25 +00:00
}
return null;
2016-02-16 21:43:25 +00:00
}
getItemHeight = index => {
const { users } = this.props;
if (index === 0) {
return 12;
}
if (index === users.length + 1) {
return 10;
}
return 24;
2018-04-05 23:46:22 +00:00
};
getItemKey = index => {
const { users } = this.props;
if (index === 0) {
return 'top';
}
if (index === users.length + 1) {
return 'bottom';
}
return index;
};
renderUser = ({ index, style }) => {
2018-10-15 06:56:17 +00:00
const { users, coloredNicks, onNickClick } = this.props;
2016-02-06 00:54:21 +00:00
if (index === 0 || index === users.length + 1) {
return null;
}
2016-02-06 00:54:21 +00:00
return (
<UserListItem
user={users[index - 1]}
2018-10-15 06:56:17 +00:00
coloredNick={coloredNicks}
2017-03-23 19:38:27 +00:00
style={style}
onClick={onNickClick}
2016-02-06 00:54:21 +00:00
/>
);
};
2015-12-28 23:34:32 +00:00
render() {
const { users, showUserList } = this.props;
const className = classnames('userlist', {
'off-canvas': showUserList
});
2015-12-28 23:34:32 +00:00
return (
<div className={className}>
2017-03-23 19:38:27 +00:00
<AutoSizer disableWidth>
{({ height }) => (
<List
ref={this.list}
2017-03-23 19:38:27 +00:00
width={200}
height={height}
itemCount={users.length + 2}
itemKey={this.getItemKey}
itemSize={this.getItemHeight}
estimatedItemSize={24}
2018-11-14 07:33:01 +00:00
overscanCount={5}
>
{this.renderUser}
</List>
2017-03-23 19:38:27 +00:00
)}
</AutoSizer>
2015-12-28 23:34:32 +00:00
</div>
);
}
}