diff --git a/autogpt_platform/frontend/src/components/agptui/composite/PublishAgentPopout.tsx b/autogpt_platform/frontend/src/components/agptui/composite/PublishAgentPopout.tsx index 717b53a874..4d4d86be6b 100644 --- a/autogpt_platform/frontend/src/components/agptui/composite/PublishAgentPopout.tsx +++ b/autogpt_platform/frontend/src/components/agptui/composite/PublishAgentPopout.tsx @@ -69,12 +69,43 @@ export const PublishAgentPopout: React.FC = ({ >(null); const [open, setOpen] = React.useState(false); const [currentPage, setCurrentPage] = React.useState(1); - const [totalPages, setTotalPages] = React.useState(1); const [loading, setLoading] = React.useState(false); + const [loadingMore, setLoadingMore] = React.useState(false); + const [hasMore, setHasMore] = React.useState(true); + + const api = useBackendAPI(); + + const fetchMyAgents = React.useCallback( + async (page: number, append = false) => { + try { + append ? setLoadingMore(true) : setLoading(true); + const response = await api.getMyAgents({ + page, + page_size: 20, + }); + setCurrentPage(response.pagination.current_page); + setHasMore( + response.pagination.current_page < response.pagination.total_pages, + ); + setMyAgents((prev) => + append && prev + ? { + agents: [...prev.agents, ...response.agents], + pagination: response.pagination, + } + : response, + ); + } catch (error) { + console.error("Failed to load my agents:", error); + } finally { + append ? setLoadingMore(false) : setLoading(false); + } + }, + [api], + ); const popupId = React.useId(); const router = useRouter(); - const api = useBackendAPI(); const { toast } = useToast(); @@ -87,31 +118,15 @@ export const PublishAgentPopout: React.FC = ({ React.useEffect(() => { if (open) { setCurrentPage(1); + setHasMore(true); } }, [open]); React.useEffect(() => { if (open) { - const loadMyAgents = async () => { - try { - setLoading(true); - const response = await api.getMyAgents({ - page: currentPage, - page_size: 20, - }); - setMyAgents(response); - setCurrentPage(response.pagination.current_page); - setTotalPages(response.pagination.total_pages); - } catch (error) { - console.error("Failed to load my agents:", error); - } finally { - setLoading(false); - } - }; - - loadMyAgents(); + fetchMyAgents(1); } - }, [open, currentPage, api]); + }, [open, fetchMyAgents]); const handleClose = () => { setStep("select"); @@ -222,13 +237,27 @@ export const PublishAgentPopout: React.FC = ({ } }; + const handleScroll = React.useCallback( + (e: React.UIEvent) => { + const { scrollTop, scrollHeight, clientHeight } = e.currentTarget; + if ( + hasMore && + !loadingMore && + scrollTop + clientHeight >= scrollHeight - 20 + ) { + fetchMyAgents(currentPage + 1, true); + } + }, + [hasMore, loadingMore, currentPage, fetchMyAgents], + ); + const renderContent = () => { switch (step) { case "select": return (
-
+
{loading ? (
Loading agents... @@ -259,29 +288,11 @@ export const PublishAgentPopout: React.FC = ({ onClose={handleClose} onOpenBuilder={() => router.push("/build")} /> -
- - - Page {currentPage} of {totalPages} - - -
+ {loadingMore && ( +
+ Loading more... +
+ )} )}