Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: adds infinite loading #219

Merged
merged 1 commit into from
May 20, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion apps/masterbots.ai/app/(browse)/page.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ export default async function HomePage({ searchParams }: HomePageProps) {
const limit = searchParams.limit ? parseInt(searchParams.limit) : 20
const page = searchParams.page ? parseInt(searchParams.page) : 1
// console.log({ query, limit, page })
const threads = await getThreads({})
const threads = await getThreads({page})

return (
<div>
Expand Down
11 changes: 9 additions & 2 deletions apps/masterbots.ai/app/actions.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,11 +6,18 @@ import { createSupabaseServerClient } from '@/services/supabase'
import { createMessagePairs } from '@/lib/threads'

export async function getThreads({
categoryId
categoryId,
page = 1
}: {
categoryId?: number
page?: number
}): Promise<MB.ThreadFull[]> {
const supabase = await createSupabaseServerClient()
const itemsPerPage = 20
const from = (page - 1) * itemsPerPage
const to = from + itemsPerPage - 1

console.log('page ', page, ' from ', from, ' to ', to)

let threadsQuery = supabase.from('thread_full').select('*')

Expand All @@ -20,7 +27,7 @@ export async function getThreads({
// ])
// }

const { data, error } = await threadsQuery.range(0, 19)
const { data, error } = await threadsQuery.range(from, to)

if (error) return []

Expand Down
42 changes: 42 additions & 0 deletions apps/masterbots.ai/components/shared/thread-list-reload.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
'use client'

import React, { useEffect, useRef, useState } from 'react'
import { useSearchParams, useRouter } from 'next/navigation'

export function ThreadListReload() {
const params = useSearchParams()
const router = useRouter()
const page = parseInt(params.get('page') || '1', 10)
const loadMoreRef = useRef<HTMLDivElement>(null)
const [isLoading, setIsLoading] = useState(false);

useEffect(() => {
if (!loadMoreRef.current) return
const observer = new IntersectionObserver(([entry]) => {

if (entry.isIntersecting && !isLoading) {
setIsLoading(true);

setTimeout(() => {
const newPage = page + 1
const newParams = new URLSearchParams(params)
newParams.set('page', newPage.toString())
router.push(`${window.location.pathname}?${newParams.toString()}`)

setIsLoading(false);
}, 150)
observer.unobserve(entry.target)
}
})

observer.observe(loadMoreRef.current)

return () => {
observer.disconnect()
}
}, [isLoading, page, params, router])

return (
<div ref={loadMoreRef} className='min-h-4' />
)
}
2 changes: 2 additions & 0 deletions apps/masterbots.ai/components/shared/thread-list.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ import { MB } from '@repo/supabase'
import { ThreadDialog } from './thread-dialog'
import { ThreadListAccordion } from './thread-list-accordion'
import { ThreadListChatItem } from './thread-list-chat-item'
import { ThreadListReload } from './thread-list-reload'

export function ThreadList({
initialThreads,
Expand Down Expand Up @@ -32,6 +33,7 @@ export function ThreadList({
/>
))
)}
<ThreadListReload />
</div>
)
}
Expand Down
Loading