/
StakePoolsList.js
157 lines (149 loc) · 5.28 KB
/
StakePoolsList.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
// @flow
import { times } from 'lodash-es/util';
import { observer } from 'mobx-react';
import React, { useEffect, useState } from 'react';
import type { ElementRef } from 'react';
import { AutoSizer, List, WindowScroller } from 'react-virtualized';
import { hideAll } from 'tippy.js';
import LoadingSpinner from '../../widgets/LoadingSpinner';
import styles from './StakePoolsList.scss';
import StakePool from '../../../domains/StakePool';
import { ThumbPool } from '../widgets/ThumbPool';
// Maximum number of stake pools for which we do not need to use the preloading
const PRELOADER_THRESHOLD = 100;
const POOL_THUMB_SIZE = 80;
const POOL_THUMB_GRID_GAP = 10;
/**
* Utility function to programmatically hide all pop overs
* This is used to hide the pool tooltips on scrolling the list
*/
function hideAllPopOvers() {
hideAll();
}
/**
* The StakePoolsList either renders a loading spinner when there are
* more than PRELOADER_THRESHOLD stake pools to be loaded (to increase
* initial rendering performance) or StakePoolTiles (if there are only
* a few stake pools OR if the simulated "preloading" is done)
*/
type StakePoolsListProps = {
stakePoolsList: Array<StakePool>,
onOpenExternalLink: Function,
currentTheme: string,
highlightOnHover?: boolean,
highlightWithDelay?: boolean,
onSelect?: (poolId: string) => void,
selectOnClick?: boolean,
showWithSelectButton?: boolean,
containerClassName: string,
numberOfRankedStakePools: number,
selectedPoolId?: ?string,
disabledStakePoolId?: ?string,
isGridRewardsView?: boolean,
scrollElementRef?: ?ElementRef<*>,
};
export const StakePoolsList = observer((props: StakePoolsListProps) => {
const [isLoading, setIsLoading] = useState(true);
useEffect(() => {
// Feature: Hide pool pop overs if the list scroll container is scrolled
// Note: do not use window here otherwise the pool description cannot be
// scrolled anymore because it closes the pop over immediately.
const scrollContainer = props.scrollElementRef
? props.scrollElementRef.current
: null;
if (scrollContainer !== null) {
scrollContainer.addEventListener('scroll', hideAllPopOvers, true);
}
setTimeout(() => setIsLoading(false));
return () => {
if (scrollContainer !== null) {
scrollContainer.removeEventListener('scroll', hideAllPopOvers);
}
};
});
if (props.stakePoolsList.length > PRELOADER_THRESHOLD && isLoading) {
return (
<div className={styles.preloadingBlockWrapper}>
<LoadingSpinner big />
</div>
);
}
const stakePoolsCount = props.stakePoolsList.length;
function rowRenderer(itemsPerRow, { index, key, style }) {
const startIndex = itemsPerRow * index;
const endIndex = startIndex + itemsPerRow;
const stakePools = props.stakePoolsList.slice(startIndex, endIndex);
const numberOfMissingRowItems = itemsPerRow - stakePools.length;
return (
<div key={key} style={style}>
<div className={styles.tiles}>
{stakePools.map((stakePool) => (
<ThumbPool
key={stakePool.id}
stakePool={stakePool}
onOpenExternalLink={props.onOpenExternalLink}
highlightOnHover={props.highlightOnHover}
highlightWithDelay={props.highlightWithDelay}
showWithSelectButton={props.showWithSelectButton}
onSelect={props.onSelect}
selectOnClick={props.selectOnClick}
isSelected={props.selectedPoolId === stakePool.id}
currentTheme={props.currentTheme}
containerClassName={props.containerClassName}
numberOfRankedStakePools={props.numberOfRankedStakePools}
disabledStakePoolId={props.disabledStakePoolId}
isGridRewardsView={props.isGridRewardsView}
/>
))}
{numberOfMissingRowItems > 0
? times(numberOfMissingRowItems, (i) => (
<div key={`${key}-${i}`} className={styles.rowFillerItem} />
))
: null}
</div>
</div>
);
}
return (
<WindowScroller
scrollElement={
props.scrollElementRef && props.scrollElementRef.current
? props.scrollElementRef.current
: window
}
>
{({ height, scrollTop, registerChild }) => (
<AutoSizer disableHeight>
{({ width }) => {
if (!stakePoolsCount || !width) {
return null;
}
const itemsPerRow = Math.floor(
width / (POOL_THUMB_SIZE + POOL_THUMB_GRID_GAP)
);
const rowCount = Math.ceil(stakePoolsCount / itemsPerRow);
return (
<div ref={(el) => registerChild(el)}>
<List
autoHeight
className={styles.list}
width={width}
height={height}
scrollTop={scrollTop}
rowHeight={POOL_THUMB_SIZE}
rowCount={rowCount}
rowRenderer={(args) => rowRenderer(itemsPerRow, args)}
overscanRowCount={3}
/>
</div>
);
}}
</AutoSizer>
)}
</WindowScroller>
);
});
StakePoolsList.defaultProps = {
showWithSelectButton: false,
highlightWithDelay: false,
};