11import { useUser } from 'web/hooks/use-user'
22import { useCompatibilityQuestionsWithAnswerCount , useUserCompatibilityAnswers } from 'web/hooks/use-questions'
3- import { useEffect , useMemo , useState } from 'react'
3+ import { useCallback , useEffect , useMemo , useState } from 'react'
44import { Row } from 'common/supabase/utils'
55import { Question } from 'web/lib/supabase/questions'
66import { Col } from 'web/components/layout/col'
@@ -11,6 +11,7 @@ import {CompatibilityAnswerBlock} from "web/components/answers/compatibility-que
1111import { User } from "common/user" ;
1212import { CompassLoadingIndicator } from "web/components/widgets/loading-indicator" ;
1313import { useIsMobile } from "web/hooks/use-is-mobile" ;
14+ import { LoadMoreUntilNotVisible } from "web/components/widgets/visibility-observer" ;
1415
1516type QuestionWithAnswer = Question & {
1617 answer ?: Row < 'compatibility_answers' >
@@ -145,6 +146,24 @@ function QuestionList({
145146 user : User
146147 refreshCompatibilityAll : ( ) => void
147148} ) {
149+ const BATCH_SIZE = 100
150+ const [ visibleCount , setVisibleCount ] = useState ( BATCH_SIZE )
151+
152+ // Reset pagination when the questions list changes (e.g., switching tabs or refreshed data)
153+ useEffect ( ( ) => {
154+ console . log ( 'resetting pagination' )
155+ setVisibleCount ( BATCH_SIZE )
156+ } , [ questions ] )
157+
158+ const loadMore = useCallback ( async ( ) => {
159+ console . log ( 'start loadMore' )
160+ if ( visibleCount >= questions . length ) return false
161+ console . log ( 'loading more' , visibleCount )
162+ setVisibleCount ( ( prev ) => Math . min ( prev + BATCH_SIZE , questions . length ) )
163+ console . log ( 'end loadMore' )
164+ return true
165+ } , [ visibleCount , questions . length ] ) ;
166+
148167 if ( isLoading ) {
149168 return < CompassLoadingIndicator />
150169 }
@@ -159,9 +178,11 @@ function QuestionList({
159178 )
160179 }
161180
181+ const visibleQuestions = questions . slice ( 0 , visibleCount )
182+
162183 return (
163184 < div className = "space-y-4 p-2" >
164- { questions . map ( ( q ) => (
185+ { visibleQuestions . map ( ( q ) => (
165186 < div
166187 key = { q . id }
167188 className = "bg-canvas-0 border-canvas-100 rounded-lg border px-2 pt-2 shadow-sm transition-colors"
@@ -177,6 +198,7 @@ function QuestionList({
177198 />
178199 </ div >
179200 ) ) }
201+ < LoadMoreUntilNotVisible loadMore = { loadMore } />
180202 </ div >
181203 )
182204}
0 commit comments