Skip to content

Commit c89722d

Browse files
committed
feat: use infinite query
1 parent 09ccb28 commit c89722d

File tree

1 file changed

+15
-8
lines changed

1 file changed

+15
-8
lines changed

apps/web/src/modules/course-plan-selector/CoursePlanSelector.tsx

Lines changed: 15 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ import { useVirtualizer } from "@tanstack/react-virtual";
55
import { useMutation } from "convex/react";
66
import type { FunctionReturnType } from "convex/server";
77
import { ConvexError } from "convex/values";
8-
import React, { useState } from "react";
8+
import React, { useEffect, useState } from "react";
99
import { toast } from "sonner";
1010
import { Button } from "@/components/ui/button";
1111
import { useSearchParam } from "@/hooks/use-search-param";
@@ -69,6 +69,20 @@ const CoursePlanSelector = ({
6969
gap: 8,
7070
});
7171

72+
// https://tanstack.com/virtual/latest/docs/framework/react/examples/infinite-scroll
73+
// biome-ignore lint/correctness/useExhaustiveDependencies: It's in Tanstack doc
74+
useEffect(() => {
75+
const [lastItem] = [...rowVirtualizer.getVirtualItems()].reverse();
76+
77+
if (!lastItem) {
78+
return;
79+
}
80+
81+
if (lastItem.index >= filteredCourses.length - 1 && status === "CanLoadMore") {
82+
loadMore(200);
83+
}
84+
}, [status, loadMore, filteredCourses.length, rowVirtualizer.getVirtualItems()]);
85+
7286
const handleCourseAdd = async (
7387
courseCode: string,
7488
title: string,
@@ -170,13 +184,6 @@ const CoursePlanSelector = ({
170184
</div>
171185
)}
172186

173-
{status === "CanLoadMore" && (
174-
<div className="flex justify-center py-4 shrink-0">
175-
<Button onClick={() => loadMore(200)} variant="outline">
176-
Load More
177-
</Button>
178-
</div>
179-
)}
180187
{status === "LoadingMore" && (
181188
<div className="flex justify-center py-4 shrink-0">
182189
<p className="text-gray-500">Loading more courses...</p>

0 commit comments

Comments
 (0)