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