/
page.simple.tsx
134 lines (115 loc) 路 3.64 KB
/
page.simple.tsx
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
import type { LinksFunction, LoaderArgs } from "@remix-run/node";
import { json } from "@remix-run/node";
import { useFetcher, useLoaderData, useTransition } from "@remix-run/react";
import { useCallback, useEffect, useRef, useState } from "react";
import { useVirtual } from "react-virtual";
import stylesUrl from "~/styles/index.css";
import { countItems, getItemsPaginated } from "~/utils/backend.server";
export const links: LinksFunction = () => [
{ rel: "stylesheet", href: stylesUrl },
];
const LIMIT = 200;
const DATA_OVERSCAN = 40;
const getPage = (searchParams: URLSearchParams) => ({
page: Number(searchParams.get("page") || "0"),
});
export const loader = async ({ request }: LoaderArgs) => {
const { page } = getPage(new URL(request.url).searchParams);
return json(
{
items: await getItemsPaginated({ page, limit: LIMIT }),
totalItems: await countItems(),
},
{ headers: { "Cache-Control": "public, max-age=120" } },
);
};
export default function Index() {
const data = useLoaderData<typeof loader>();
const [items, setItems] = useState(data.items);
const transition = useTransition();
const fetcher = useFetcher();
const startRef = useRef(0);
const page = useRef(0);
const parentRef = useRef<HTMLDivElement>(null);
const rowVirtualizer = useVirtual({
size: items.length,
parentRef,
estimateSize: useCallback(() => 35, []),
initialRect: { width: 0, height: 800 },
});
const [lastVirtualItem] = [...rowVirtualizer.virtualItems].reverse();
if (!lastVirtualItem) {
throw new Error("this should never happen");
}
let newStart = startRef.current;
const upperBoundary = startRef.current + LIMIT - DATA_OVERSCAN;
if (lastVirtualItem.index > upperBoundary) {
// user is scrolling down. Move the window down
newStart = startRef.current + LIMIT;
}
useEffect(() => {
if (newStart === startRef.current) return;
startRef.current = newStart;
page.current += 1;
fetcher.load(`/page/simple?page=${page.current}`);
}, [page, newStart, fetcher]);
useEffect(() => {
if (fetcher.data) {
setItems((prevItems) => [...prevItems, ...fetcher.data.items]);
}
}, [fetcher.data]);
return (
<main>
<h1>
Simple Infinite Scrolling (pages loaded {page.current + 1}/
{data.totalItems / LIMIT})
</h1>
<div
ref={parentRef}
className="List"
style={{
height: `800px`,
width: `100%`,
overflow: "auto",
}}
>
<div
style={{
height: `${rowVirtualizer.totalSize}px`,
width: "100%",
position: "relative",
}}
>
{rowVirtualizer.virtualItems.map((virtualRow) => {
const item = items[virtualRow.index];
return (
<div
key={virtualRow.key}
className={`list-item ${
virtualRow.index % 2 ? "list-item--odd" : "list-item--even"
}`}
style={{
position: "absolute",
top: 0,
left: 0,
width: "100%",
height: `${virtualRow.size}px`,
transform: `translateY(${virtualRow.start}px)`,
}}
>
<span>{virtualRow.index}</span>
<span>
{item
? item.value
: transition.state === "loading"
? "Loading more..."
: "Nothing to see here..."}
</span>
</div>
);
})}
</div>
</div>
</main>
);
}