-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathApp.tsx
More file actions
119 lines (112 loc) · 3.3 KB
/
Copy pathApp.tsx
File metadata and controls
119 lines (112 loc) · 3.3 KB
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
import {
useHistoryScrollState,
useStickToBottom,
useZeroVirtualizer,
} from '@rocicorp/zero-virtual/react';
import React, {useCallback, useRef} from 'react';
import styles from '../shared/App.module.css';
import {DevPanel} from './DevPanel.tsx';
import {ItemDetail} from './ItemDetail.tsx';
import {ItemRow} from './ItemRow.tsx';
import {ListHeader} from './ListHeader.tsx';
import {
getRowKey,
getSingleQuery,
toStartRow,
useDemoControls,
useEstimateSize,
useGetPageQuery,
useSortState,
} from './list-shared.ts';
import type {ItemStart} from '../shared/queries.ts';
import {useHash} from './use-hash.ts';
export function App(): React.ReactNode {
const [hash, setHash] = useHash();
const permalinkID = hash || null;
const {
sortField,
sortDirection,
toggleSortField,
toggleSortDirection,
listContextParams,
} = useSortState();
const {
heightMode,
setHeightMode,
anchoring,
setAnchoring,
count,
follow,
setFollow,
} = useDemoControls();
const parentRef = useRef<HTMLDivElement>(null);
const getScrollElement = useCallback(() => parentRef.current, []);
const estimateSize = useEstimateSize(heightMode);
const getPageQuery = useGetPageQuery(listContextParams);
const [scrollState, onScrollStateChange] = useHistoryScrollState<ItemStart>();
const virtualizer = useZeroVirtualizer({
listContextParams,
getScrollElement,
anchoring,
count,
getRowKey,
estimateSize,
getPageQuery,
getSingleQuery,
toStartRow,
permalinkID,
scrollState,
onScrollStateChange,
onSettled: useCallback(() => {
console.log('onSettled');
}, []),
});
const {items, spaceBefore, spaceAfter, estimatedTotal, total} = virtualizer;
useStickToBottom(virtualizer, {enabled: follow === 'bottom'});
return (
<div className={styles.page}>
<div className={styles.list}>
<ListHeader
total={total}
estimatedTotal={estimatedTotal}
sortField={sortField}
sortDirection={sortDirection}
onToggleSortField={toggleSortField}
onToggleSortDirection={toggleSortDirection}
/>
{/* Scrollable viewport. Rows render in normal flow inside a content
wrapper. `spaceBefore` / `spaceAfter` stand in for the unloaded rows
above and below, rendered as spacer elements so scroll anchoring
keeps the viewport stable across paging. */}
<div ref={parentRef} className={styles.viewport}>
<div>
<div style={{height: spaceBefore}} />
{items.map(item => (
<ItemRow
key={item.key}
item={item}
heightMode={heightMode}
sortField={sortField}
permalinkID={permalinkID}
/>
))}
<div style={{height: spaceAfter}} />
</div>
</div>
</div>
{permalinkID && (
<ItemDetail id={permalinkID} onClose={() => setHash('')} />
)}
<DevPanel
getScrollElement={getScrollElement}
heightMode={heightMode}
onHeightModeChange={setHeightMode}
sortDirection={sortDirection}
anchoring={anchoring}
onAnchoringChange={setAnchoring}
follow={follow}
onFollowChange={setFollow}
/>
</div>
);
}