Skip to content
New issue

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

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

Already on GitHub? # to your account

add scroll agent fetch loading #140

Open
wants to merge 8 commits into
base: main
Choose a base branch
from
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,7 @@
"react": "^18.2.0",
"react-activity-calendar": "^2.7.1",
"react-dom": "^18.2.0",
"react-intersection-observer": "^9.15.1",
"react-markdown": "^9.0.1",
"react-player": "^2.16.0",
"recharts": "^2.13.3",
Expand Down
18 changes: 18 additions & 0 deletions pnpm-lock.yaml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

32 changes: 28 additions & 4 deletions src/components/layout/AgentsContent.tsx
Original file line number Diff line number Diff line change
@@ -1,6 +1,10 @@
'use client';

import { useAllAssistants } from '@/hooks/useAssistants';
import { Skeleton } from '../ui/skeleton';
import { useState, useEffect } from 'react';
import { useInView } from 'react-intersection-observer';
import { useSearchParams } from 'next/navigation';
import dynamic from 'next/dynamic';
import PageLoaderSkeleton from './PageLoaderSkeleton';

Expand All @@ -10,11 +14,29 @@ const AllAgentsWithNoSSR = dynamic(
);

const AgentContent = () => {
const { allAgents: data, loading } = useAllAssistants();
const [offset, setOffset] = useState(0);
const [hasMore, setHasMore] = useState(true);
const searchParams = useSearchParams();
const isPlayground = searchParams.get('isPlayground') === 'true';
const { allAgents: data, loading } = useAllAssistants(offset, 30);
const { ref, inView } = useInView();

if (loading) {
return <PageLoaderSkeleton />;
}
useEffect(() => {
if (inView && !loading && hasMore) {
setOffset((prevOffset) => prevOffset + 30);
}
}, [inView, loading, hasMore]);

useEffect(() => {
if (data) {
const totalAgents = isPlayground
? data.unverifiedAgents.length
: data.agents.length;
if (totalAgents < 30) {
setHasMore(false);
}
}
}, [data, isPlayground]);

return (
<div className='relative z-30'>
Expand All @@ -23,6 +45,8 @@ const AgentContent = () => {
filters={data?.filters || []}
unverifiedAgents={data?.unverifiedAgents || []}
/>
{loading && <PageLoaderSkeleton />}
<div ref={ref} />
</div>
);
};
Expand Down
17 changes: 10 additions & 7 deletions src/hooks/useAssistants.ts
Original file line number Diff line number Diff line change
Expand Up @@ -104,7 +104,7 @@ export const useVerifiedAssistants = () => {
return { verifiedAgents: data, loading, error };
};

export const useAllAssistants = () => {
export const useAllAssistants = (offset?: number, limit?: number) => {
const [data, setData] = useState<AgentData | null>(null);
const [loading, setLoading] = useState<boolean>(true);
const [error, setError] = useState<Error | null>(null);
Expand All @@ -113,7 +113,7 @@ export const useAllAssistants = () => {
const fetchUnverifiedAssistants = async () => {
try {
const response = await fetch(
`${MB_URL.REGISTRY_API_BASE}/agents?verifiedOnly=false&limit=100`
`${MB_URL.REGISTRY_API_BASE}/agents?verifiedOnly=false&limit=${limit ? limit : 100}&offset=${offset ? offset : 0}`
);
if (!response.ok) {
throw new Error('Failed to fetch unverified agents');
Expand All @@ -140,16 +140,19 @@ export const useAllAssistants = () => {
),
].filter(Boolean);

setData({
agents: verifiedAgents,
unverifiedAgents: unverifiedAgents,
setData((prevData) => ({
agents: [...(prevData?.agents || []), ...verifiedAgents],
unverifiedAgents: [
...(prevData?.unverifiedAgents || []),
...unverifiedAgents,
],
filters: [
{
label: 'Category',
values: categories as string[],
},
],
});
}));
} catch (err) {
setError(err as Error);
} finally {
Expand All @@ -158,7 +161,7 @@ export const useAllAssistants = () => {
};

fetchUnverifiedAssistants();
}, []);
}, [offset, limit]);

return { allAgents: data, loading, error };
};
Expand Down