Skip to content

Commit 61c5330

Browse files
committed
Load compat questions progressively to avoid long page load
1 parent 61613af commit 61c5330

File tree

2 files changed

+36
-11
lines changed

2 files changed

+36
-11
lines changed

web/components/widgets/visibility-observer.tsx

Lines changed: 12 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import { useEffect, useRef, useState } from 'react'
2-
import { useEvent } from '../../hooks/use-event'
2+
import { useEvent } from 'web/hooks/use-event'
33

44
export function VisibilityObserver(props: {
55
className?: string
@@ -29,15 +29,18 @@ export function LoadMoreUntilNotVisible(props: {
2929
const { loadMore } = props
3030
const isVisibleRef = useRef(false)
3131
const loadMoreIfVisible = useEvent(async () => {
32+
console.log('loadMoreIfVisible called')
3233
if (isVisibleRef.current && loadMore) {
33-
const hasMoreResults = await loadMore()
34-
if (hasMoreResults) {
35-
setTimeout(() => {
36-
if (isVisibleRef.current) {
37-
loadMoreIfVisible()
38-
}
39-
}, 500)
40-
}
34+
console.log('loadMore calling')
35+
await loadMore()
36+
// const hasMoreResults = await loadMore()
37+
// if (hasMoreResults) {
38+
// setTimeout(() => {
39+
// if (isVisibleRef.current) {
40+
// loadMoreIfVisible()
41+
// }
42+
// }, 500)
43+
// }
4144
}
4245
})
4346

web/pages/compatibility.tsx

Lines changed: 24 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
import {useUser} from 'web/hooks/use-user'
22
import {useCompatibilityQuestionsWithAnswerCount, useUserCompatibilityAnswers} from 'web/hooks/use-questions'
3-
import {useEffect, useMemo, useState} from 'react'
3+
import {useCallback, useEffect, useMemo, useState} from 'react'
44
import {Row} from 'common/supabase/utils'
55
import {Question} from 'web/lib/supabase/questions'
66
import {Col} from 'web/components/layout/col'
@@ -11,6 +11,7 @@ import {CompatibilityAnswerBlock} from "web/components/answers/compatibility-que
1111
import {User} from "common/user";
1212
import {CompassLoadingIndicator} from "web/components/widgets/loading-indicator";
1313
import {useIsMobile} from "web/hooks/use-is-mobile";
14+
import {LoadMoreUntilNotVisible} from "web/components/widgets/visibility-observer";
1415

1516
type 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

Comments
 (0)