@@ -5,7 +5,7 @@ import { useVirtualizer } from "@tanstack/react-virtual";
55import { useMutation } from "convex/react" ;
66import type { FunctionReturnType } from "convex/server" ;
77import { ConvexError } from "convex/values" ;
8- import React , { useState } from "react" ;
8+ import React , { useEffect , useState } from "react" ;
99import { toast } from "sonner" ;
1010import { Button } from "@/components/ui/button" ;
1111import { 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