Skip to content

Commit 48505f9

Browse files
committed
use existing "poor-man's virtualization" for lists
1 parent a1bd248 commit 48505f9

File tree

3 files changed

+57
-37
lines changed

3 files changed

+57
-37
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
// @ts-check
2+
import { useState, cloneElement } from 'react';
3+
import { Visible } from './visible';
4+
5+
const INITIAL_SIZE = 20;
6+
const GROW_BLOCK_SIZE = 29;
7+
8+
/**
9+
* @template ItemType
10+
* @param {{ items: Array<ItemType>, renderItem(item: ItemType): import('react').ReactElement }} props
11+
* Given a list of items, only renders the first 20 by default,
12+
* and then more whenever the last item is close to being on screen
13+
*/
14+
export function ProgressiveRender(props) {
15+
const [listSize, setListSize] = useState(INITIAL_SIZE);
16+
const showSize = Math.min(props.items.length, listSize);
17+
18+
return (
19+
<>
20+
{props.items.slice(0, showSize).map((item, index) => {
21+
const entry = cloneElement(props.renderItem(item), { key: index });
22+
23+
return index < showSize - 1 ? (
24+
entry
25+
) : (
26+
<Visible
27+
key={index}
28+
rootMargin="0px 0px 300px 0px"
29+
onVisible={handleBottomVisible}
30+
>
31+
{entry}
32+
</Visible>
33+
);
34+
})}
35+
</>
36+
);
37+
38+
function handleBottomVisible() {
39+
const incrementListSize = listSize + GROW_BLOCK_SIZE;
40+
setListSize(incrementListSize);
41+
if (incrementListSize > props.items.length) {
42+
// TODO: control fetch more from here?
43+
}
44+
}
45+
}

src/detail-panels/block-panel-generic/list-view.jsx

+5-31
Original file line numberDiff line numberDiff line change
@@ -1,50 +1,24 @@
11
// @ts-check
22
/// <reference path="../../types.d.ts" />
33
import { FormatTimestamp } from '../../common-components/format-timestamp';
4-
import { Visible } from '../../common-components/visible';
5-
import { useState } from 'react';
4+
import { ProgressiveRender } from '../../common-components/progressive-render';
65
import { AccountShortEntry } from '../../common-components/account-short-entry';
76
import { localise } from '../../localisation';
87

9-
const INITIAL_SIZE = 20;
10-
const GROW_BLOCK_SIZE = 29;
11-
128
/**
139
* @param {{
1410
* blocklist: (BlockedByRecord | { did: string; blocked_date: string })[];
1511
* }} _
1612
*/
1713
export function ListView({ blocklist }) {
18-
const [listSize, setListSize] = useState(INITIAL_SIZE);
19-
const showSize = Math.min(blocklist.length, listSize);
20-
2114
return (
2215
<ul className="block-list">
23-
{blocklist.slice(0, showSize).map((block, index) => {
24-
const entry = <ListViewEntry key={index} {...block} />;
25-
26-
return index < showSize - 1 ? (
27-
entry
28-
) : (
29-
<Visible
30-
key={index}
31-
rootMargin="0px 0px 300px 0px"
32-
onVisible={handleBottomVisible}
33-
>
34-
{entry}
35-
</Visible>
36-
);
37-
})}
16+
<ProgressiveRender
17+
items={blocklist}
18+
renderItem={(item) => <ListViewEntry {...item} />}
19+
/>
3820
</ul>
3921
);
40-
41-
function handleBottomVisible() {
42-
const incrementListSize = listSize + GROW_BLOCK_SIZE;
43-
setListSize(incrementListSize);
44-
if (incrementListSize > blocklist.length) {
45-
// TODO: fetch more
46-
}
47-
}
4822
}
4923

5024
/**

src/detail-panels/lists/list-view.jsx

+7-6
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ import { AccountShortEntry } from '../../common-components/account-short-entry';
55
import { FormatTimestamp } from '../../common-components/format-timestamp';
66
import { useListSize } from '../../api/lists';
77
import './list-view.css';
8+
import { ProgressiveRender } from '../../common-components/progressive-render';
89

910
/**
1011
* @param {{
@@ -15,21 +16,21 @@ import './list-view.css';
1516
export function ListView({ className, list }) {
1617
return (
1718
<ul className={'lists-as-list-view ' + (className || '')}>
18-
{(list || []).map((entry, i) => (
19-
<ListViewEntry key={i} entry={entry} style={{}} />
20-
))}
19+
<ProgressiveRender
20+
items={list || []}
21+
renderItem={(entry) => <ListViewEntry entry={entry} />}
22+
/>
2123
</ul>
2224
);
2325
}
2426

2527
/**
2628
* @param {{
2729
* className?: string,
28-
* entry: AccountListEntry,
29-
* style:any
30+
* entry: AccountListEntry
3031
* }} _
3132
*/
32-
export function ListViewEntry({ className, entry, style }) {
33+
export function ListViewEntry({ className, entry }) {
3334
const { data: sizeData, isLoading } = useListSize(entry?.url);
3435
const count = sizeData?.count?.toLocaleString() || '';
3536

0 commit comments

Comments
 (0)