-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathApp.tsx
More file actions
173 lines (152 loc) · 4.94 KB
/
App.tsx
File metadata and controls
173 lines (152 loc) · 4.94 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
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
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
import {
useZeroVirtualizer,
useHistoryScrollState,
type GetPageQueryOptions,
type GetSingleQueryOptions,
} from '@rocicorp/zero-virtual/react';
import React, {useCallback, useMemo, useRef, useState} from 'react';
import styles from './App.module.css';
import {ItemCount} from './ItemCount.tsx';
import {ItemDetail} from './ItemDetail.tsx';
import {queries, type ItemStart, type ListContextParams} from './queries.ts';
import type {Item} from './schema.ts';
import {SortControls} from './SortControls.tsx';
import {useHash} from './use-hash.ts';
const ITEM_HEIGHT = 48;
const dateFormatter = new Intl.DateTimeFormat(undefined, {
dateStyle: 'medium',
timeStyle: 'short',
});
function getRowKey(item: Item): string {
return item.id;
}
function toStartRow(item: Item): ItemStart {
return {
id: item.id,
created: item.created,
modified: item.modified,
};
}
function estimateSize(): number {
return ITEM_HEIGHT;
}
function getQueryOptions(settled: boolean) {
return {ttl: settled ? '5m' : 'none'} as const;
}
function getSingleQuery({id, settled}: GetSingleQueryOptions) {
return {
query: queries.item.getSingleQuery({id}),
options: getQueryOptions(settled),
} as const;
}
export function App(): React.ReactNode {
const [hash, setHash] = useHash();
const permalinkID = hash || null;
const [sortField, setSortField] = useState<'created' | 'modified'>(
'modified',
);
const [sortDirection, setSortDirection] = useState<'asc' | 'desc'>('desc');
const toggleSortField = useCallback(() => {
setSortField(f => (f === 'modified' ? 'created' : 'modified'));
}, []);
const toggleSortDirection = useCallback(() => {
setSortDirection(d => (d === 'asc' ? 'desc' : 'asc'));
}, []);
const parentRef = useRef<HTMLDivElement>(null);
const getScrollElement = useCallback(() => parentRef.current, []);
const listContextParams = useMemo<ListContextParams>(
() => ({sortField, sortDirection}),
[sortField, sortDirection],
);
const getPageQuery = useCallback(
({limit, start, dir, settled}: GetPageQueryOptions<ItemStart>) => {
return {
query: queries.item.getPageQuery({
limit,
start,
dir,
listContextParams,
}),
options: getQueryOptions(settled),
};
},
[listContextParams],
);
const [scrollState, onScrollStateChange] = useHistoryScrollState<ItemStart>();
const {virtualizer, rowAt, estimatedTotal, total} = useZeroVirtualizer({
listContextParams,
getScrollElement,
getRowKey,
estimateSize,
getPageQuery,
getSingleQuery,
toStartRow,
permalinkID,
scrollState,
onScrollStateChange,
onSettled: useCallback(() => {
console.log('onSettled');
}, []),
});
const virtualItems = virtualizer.getVirtualItems();
return (
<div className={styles.page}>
<div className={styles.list}>
<div className={styles.header}>
<h1 className={styles.heading}>
<span className={styles.headingText}>Zero Virtual Demo</span>
<ItemCount total={total} estimatedTotal={estimatedTotal} />
</h1>
<SortControls
sortField={sortField}
sortDirection={sortDirection}
onToggleSortField={toggleSortField}
onToggleSortDirection={toggleSortDirection}
/>
</div>
{/* Scrollable viewport */}
<div ref={parentRef} className={styles.viewport}>
{/* Total height spacer */}
<div
style={{height: virtualizer.getTotalSize(), position: 'relative'}}
>
{virtualItems.map(virtualRow => {
const row = rowAt(virtualRow.index);
if (row === undefined) {
// placeholder
return (
<div
key={virtualRow.key}
data-index={virtualRow.index}
className={styles.row}
style={{transform: `translateY(${virtualRow.start}px)`}}
>
<span className={styles.rowLabel}>Loading...</span>
</div>
);
}
return (
<div
key={virtualRow.key}
data-index={virtualRow.index}
className={styles.row}
style={{transform: `translateY(${virtualRow.start}px)`}}
aria-selected={row.id === permalinkID || undefined}
onClick={() => setHash(row.id === permalinkID ? '' : row.id)}
>
<span className={styles.rowLabel}>{row.title}</span>
<span className={styles.rowValue}>
{dateFormatter.format(row[sortField])}
</span>
</div>
);
})}
</div>
</div>
</div>
{permalinkID && (
<ItemDetail id={permalinkID} onClose={() => setHash('')} />
)}
</div>
);
}