-
Notifications
You must be signed in to change notification settings - Fork 11
/
Copy pathinfinite-scroller.tsx
136 lines (128 loc) · 4.19 KB
/
infinite-scroller.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
135
136
"use client"
import { useEffect, useRef, useState } from "react"
import type { Show } from "~/lib/types"
import { getMyShowsInfinite } from "~/actions"
import { getShows } from "~/lib/client-fetchers"
import { ERR } from "~/lib/utils"
import { Button } from "~/components/ui/button"
import Link from "next/link"
export function ShowScroller({
initialShows,
initialHasNextPage,
limit,
}: {
initialShows: Show[]
initialHasNextPage: boolean
limit: number
}) {
const [myShows, setMyShows] = useState(initialShows)
const [simulatedShows, setSimulatedShows] = useState<Show[]>()
const getShowsReturnRef = useRef<Awaited<ReturnType<typeof getShows>>>()
const hasNextPageRef = useRef(initialHasNextPage)
const indexRef = useRef(0)
const observerTarget = useRef(null)
const shows = simulatedShows ?? myShows
async function fetchNextPage() {
indexRef.current += 1
const { data } = await getMyShowsInfinite({
index: indexRef.current,
limit,
})
if (!data) throw new Error(ERR.db)
setMyShows((prev) => [...prev, ...data.shows])
hasNextPageRef.current = data.hasNextPage
}
async function getSimulatedShows() {
if (getShowsReturnRef.current) {
window.scrollTo(0, 0)
getShowsReturnRef.current = undefined
hasNextPageRef.current = initialHasNextPage
setSimulatedShows(undefined)
return
}
window.scrollTo(0, 0)
const data = await getShows("movie")
getShowsReturnRef.current = data
hasNextPageRef.current = true
setSimulatedShows([
...new Map(
[...data.trending, ...data.topRated].map((item) => [item.id, item]),
).values(),
])
}
useEffect(() => {
const observer = new IntersectionObserver(
(entries) => {
if (!hasNextPageRef.current) return
if (entries[0]?.isIntersecting) {
if (getShowsReturnRef.current) {
setTimeout(
() =>
setSimulatedShows((prev) => [
...new Map(
[
...prev!,
...getShowsReturnRef.current!.actionThriller,
...getShowsReturnRef.current!.comedy,
].map((item) => [item.id, item]),
).values(),
]),
1000,
)
hasNextPageRef.current = false
} else void fetchNextPage()
}
},
{ threshold: 1 },
)
if (observerTarget.current) {
observer.observe(observerTarget.current)
}
return () => observer.disconnect()
}, [])
return (
<main className="space-y-1.5 [overflow-anchor:none]">
<ul className="grid grid-cols-[repeat(auto-fill,_minmax(160px,_1fr))] gap-5 md:grid-cols-[repeat(auto-fill,_minmax(240px,_1fr))]">
{shows.map((show) => (
<Link
href={`/show/${show.id}?mediaType=${show.title ? "movie" : "tv"}`}
scroll={false}
key={show.id}
>
{/* eslint-disable-next-line @next/next/no-img-element */}
<img
src={`https://image.tmdb.org/t/p/w300${
show.backdrop_path ?? show.poster_path
}`}
alt="show-backdrop"
width={300}
height={169}
className="aspect-video max-w-full cursor-pointer object-cover transition-transform hover:scale-110"
/>
</Link>
))}
</ul>
<div ref={observerTarget}></div>
<section className="flex flex-col items-center gap-4">
{hasNextPageRef.current ? (
<Button
variant="outline"
className="w-full animate-pulse"
// eslint-disable-next-line @typescript-eslint/no-misused-promises
onClick={fetchNextPage}
>
Loading...
</Button>
) : (
<Button variant="outline" className="w-full cursor-auto">
You have reached the end of your saved shows
</Button>
)}
{/* eslint-disable-next-line @typescript-eslint/no-misused-promises */}
<Button onClick={getSimulatedShows}>
{simulatedShows ? "Turn off simulation" : "Simulate many saved shows"}
</Button>
</section>
</main>
)
}