Skip to content

Commit c8c93b5

Browse files
authored
fix pagination logic for list types and use pages for clarity (#10)
1 parent a815ea8 commit c8c93b5

File tree

1 file changed

+13
-10
lines changed

1 file changed

+13
-10
lines changed

src/components/databrowser/hooks/use-fetch-list-items.tsx

Lines changed: 13 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -31,15 +31,17 @@ export const useFetchListItems = ({ dataKey, type }: { dataKey: string; type: Li
3131
enabled: type === "zset",
3232
queryKey: [FETCH_LIST_ITEMS_QUERY_KEY, dataKey, "zset"],
3333
initialPageParam: 0,
34-
queryFn: async ({ pageParam: lastIndex }) => {
35-
const res = await redis.zrange(dataKey, lastIndex, lastIndex + LIST_DISPLAY_PAGE_SIZE - 1, {
34+
queryFn: async ({ pageParam: page }) => {
35+
const start = page * LIST_DISPLAY_PAGE_SIZE
36+
const end = start + LIST_DISPLAY_PAGE_SIZE - 1
37+
38+
const res = await redis.zrange(dataKey, start, end, {
3639
withScores: true,
3740
rev: true,
3841
})
3942

4043
return {
41-
cursor:
42-
res.length < LIST_DISPLAY_PAGE_SIZE ? undefined : lastIndex + LIST_DISPLAY_PAGE_SIZE,
44+
cursor: res.length < LIST_DISPLAY_PAGE_SIZE ? undefined : page + 1,
4345
keys: transformArray(res as any),
4446
}
4547
},
@@ -69,15 +71,16 @@ export const useFetchListItems = ({ dataKey, type }: { dataKey: string; type: Li
6971
enabled: type === "list",
7072
queryKey: [FETCH_LIST_ITEMS_QUERY_KEY, dataKey, "list"],
7173
initialPageParam: 0,
72-
queryFn: async ({ pageParam }) => {
73-
const lastIndex = Number(pageParam)
74-
const values = await redis.lrange(dataKey, lastIndex, lastIndex + LIST_DISPLAY_PAGE_SIZE)
74+
queryFn: async ({ pageParam: page }) => {
75+
const start = page * LIST_DISPLAY_PAGE_SIZE
76+
const end = start + LIST_DISPLAY_PAGE_SIZE - 1
77+
78+
const values = await redis.lrange(dataKey, start, end)
7579

7680
return {
77-
cursor:
78-
values.length < LIST_DISPLAY_PAGE_SIZE ? undefined : lastIndex + LIST_DISPLAY_PAGE_SIZE,
81+
cursor: values.length < LIST_DISPLAY_PAGE_SIZE ? undefined : page + 1,
7982
keys: values.map((value, i) => ({
80-
key: (lastIndex + i).toString(),
83+
key: (start + i).toString(),
8184
value,
8285
})),
8386
}

0 commit comments

Comments
 (0)