|
1
|
import { QueryKey, useInfiniteQuery } from "@tanstack/react-query" |
|
2
|
import { ReactNode, useEffect, useMemo, useRef } from "react" |
|
3
|
import { toast } from "@medusajs/ui" |
|
4
|
import { Spinner } from "@medusajs/icons" |
|
5
|
|
|
6
|
type InfiniteListProps<TResponse, TEntity, TParams> = { |
|
7
|
queryKey: QueryKey |
|
8
|
queryFn: (params: TParams) => Promise<TResponse> |
|
9
|
queryOptions?: { enabled?: boolean } |
|
10
|
renderItem: (item: TEntity) => ReactNode |
|
11
|
renderEmpty: () => ReactNode |
|
12
|
responseKey: keyof TResponse |
|
13
|
pageSize?: number |
|
14
|
} |
|
15
|
|
|
16
|
export const InfiniteList = < |
|
17
|
TResponse extends { count: number; offset: number; limit: number }, |
|
18
|
TEntity extends { id: string }, |
|
19
|
TParams extends { offset?: number; limit?: number } |
|
20
|
>({ |
|
21
|
queryKey, |
|
22
|
queryFn, |
|
23
|
queryOptions, |
|
24
|
renderItem, |
|
25
|
renderEmpty, |
|
26
|
responseKey, |
|
27
|
pageSize = 20, |
|
28
|
}: InfiniteListProps<TResponse, TEntity, TParams>) => { |
|
29
|
const { |
|
30
|
data, |
|
31
|
error, |
|
32
|
fetchNextPage, |
|
33
|
fetchPreviousPage, |
|
34
|
hasPreviousPage, |
|
35
|
hasNextPage, |
|
36
|
isFetching, |
|
37
|
isPending, |
|
38
|
} = useInfiniteQuery({ |
|
39
|
queryKey: queryKey, |
|
40
|
queryFn: async ({ pageParam = 0 }) => { |
|
41
|
return await queryFn({ |
|
42
|
limit: pageSize, |
|
43
|
offset: pageParam, |
|
44
|
} as TParams) |
|
45
|
}, |
|
46
|
initialPageParam: 0, |
|
47
|
maxPages: 5, |
|
48
|
getNextPageParam: (lastPage) => { |
|
49
|
const moreItemsExist = lastPage.count > lastPage.offset + lastPage.limit |
|
50
|
return moreItemsExist ? lastPage.offset + lastPage.limit : undefined |
|
51
|
}, |
|
52
|
getPreviousPageParam: (firstPage) => { |
|
53
|
const moreItemsExist = firstPage.offset !== 0 |
|
54
|
return moreItemsExist |
|
55
|
? Math.max(firstPage.offset - firstPage.limit, 0) |
|
56
|
: undefined |
|
57
|
}, |
|
58
|
...queryOptions, |
|
59
|
}) |
|
60
|
|
|
61
|
const items = useMemo(() => { |
|
62
|
return data?.pages.flatMap((p) => p[responseKey] as TEntity[]) ?? [] |
|
63
|
}, [data, responseKey]) |
|
64
|
|
|
65
|
const parentRef = useRef<HTMLDivElement>(null) |
|
66
|
const startObserver = useRef<IntersectionObserver>() |
|
67
|
const endObserver = useRef<IntersectionObserver>() |
|
68
|
const fetchNextPageRef = useRef(fetchNextPage) |
|
69
|
const fetchPreviousPageRef = useRef(fetchPreviousPage) |
|
70
|
|
|
71
|
useEffect(() => { |
|
72
|
fetchNextPageRef.current = fetchNextPage |
|
73
|
fetchPreviousPageRef.current = fetchPreviousPage |
|
74
|
}, [fetchNextPage, fetchPreviousPage]) |
|
75
|
|
|
76
|
useEffect(() => { |
|
77
|
if (isPending) { |
|
78
|
return |
|
79
|
} |
|
80
|
|
|
81
|
// Define the new observers after we stop fetching |
|
82
|
if (!isFetching) { |
|
83
|
// Define the new observers after paginating |
|
84
|
startObserver.current = new IntersectionObserver( |
|
85
|
(entries) => { |
|
86
|
if (entries[0].isIntersecting && hasPreviousPage) { |
|
87
|
startObserver.current?.disconnect() |
|
88
|
fetchPreviousPageRef.current() |
|
89
|
} |
|
90
|
}, |
|
91
|
{ |
|
92
|
threshold: 0.5, |
|
93
|
} |
|
94
|
) |
|
95
|
|
|
96
|
endObserver.current = new IntersectionObserver( |
|
97
|
(entries) => { |
|
98
|
if (entries[0].isIntersecting && hasNextPage) { |
|
99
|
endObserver.current?.disconnect() |
|
100
|
fetchNextPageRef.current() |
|
101
|
} |
|
102
|
}, |
|
103
|
{ |
|
104
|
threshold: 0.5, |
|
105
|
} |
|
106
|
) |
|
107
|
|
|
108
|
// Register the new observers to observe the new first and last children |
|
109
|
if (parentRef.current?.firstChild) { |
|
110
|
startObserver.current?.observe(parentRef.current.firstChild as Element) |
|
111
|
} |
|
112
|
if (parentRef.current?.lastChild) { |
|
113
|
endObserver.current?.observe(parentRef.current.lastChild as Element) |
|
114
|
} |
|
115
|
} |
|
116
|
|
|
117
|
// Clear the old observers |
|
118
|
return () => { |
|
119
|
startObserver.current?.disconnect() |
|
120
|
endObserver.current?.disconnect() |
|
121
|
} |
|
122
|
}, [hasNextPage, hasPreviousPage, isFetching, isPending]) |
|
123
|
|
|
124
|
useEffect(() => { |
|
125
|
if (error) { |
|
126
|
toast.error(error.message) |
|
127
|
} |
|
128
|
}, [error]) |
|
129
|
|
|
130
|
if (isPending) { |
|
131
|
return ( |
|
132
|
<div className="flex h-full flex-col items-center justify-center"> |
|
133
|
<Spinner className="animate-spin" /> |
|
134
|
</div> |
|
135
|
) |
|
136
|
} |
|
137
|
|
|
138
|
return ( |
|
139
|
<div ref={parentRef} className="h-full"> |
|
140
|
{items?.length |
|
141
|
? items.map((item) => <div key={item.id}>{renderItem(item)}</div>) |
|
142
|
: renderEmpty()} |
|
143
|
|
|
144
|
{isFetching && ( |
|
145
|
<div className="flex flex-col items-center justify-center py-4"> |
|
146
|
<Spinner className="animate-spin" /> |
|
147
|
</div> |
|
148
|
)} |
|
149
|
</div> |
|
150
|
) |
|
151
|
} |