Skip to content

feat(frontend): Publish Agent Dialog Agent List Pagination #9833

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

Open
wants to merge 7 commits into
base: dev
Choose a base branch
from
Open
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
Original file line number Diff line number Diff line change
Expand Up @@ -389,9 +389,13 @@ async def get_my_agents(
user_id: typing.Annotated[
str, fastapi.Depends(autogpt_libs.auth.depends.get_user_id)
],
page: typing.Annotated[int, fastapi.Query(ge=1)] = 1,
page_size: typing.Annotated[int, fastapi.Query(ge=1)] = 20,
):
try:
agents = await backend.server.v2.store.db.get_my_agents(user_id)
agents = await backend.server.v2.store.db.get_my_agents(
user_id, page=page, page_size=page_size
)
return agents
except Exception:
logger.exception("Exception occurred whilst getting my agents")
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,9 @@ export const PublishAgentPopout: React.FC<PublishAgentPopoutProps> = ({
number | null
>(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 popupId = React.useId();
const router = useRouter();
Expand All @@ -81,20 +84,34 @@ export const PublishAgentPopout: React.FC<PublishAgentPopoutProps> = ({
setPublishData(submissionData);
}, [openPopout]); // eslint-disable-line react-hooks/exhaustive-deps

React.useEffect(() => {
if (open) {
setCurrentPage(1);
}
}, [open]);

React.useEffect(() => {
if (open) {
const loadMyAgents = async () => {
try {
const response = await api.getMyAgents();
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();
}
}, [open, api]);
}, [open, currentPage, api]);

const handleClose = () => {
setStep("select");
Expand Down Expand Up @@ -212,29 +229,61 @@ export const PublishAgentPopout: React.FC<PublishAgentPopoutProps> = ({
<div className="flex min-h-screen items-center justify-center">
<div className="mx-auto flex w-full max-w-[900px] flex-col rounded-3xl bg-white shadow-lg dark:bg-gray-800">
<div className="h-full overflow-y-auto">
<PublishAgentSelect
agents={
myAgents?.agents
.map((agent) => ({
name: agent.agent_name,
id: agent.agent_id,
version: agent.agent_version,
lastEdited: agent.last_edited,
imageSrc:
agent.agent_image || "https://picsum.photos/300/200",
}))
.sort(
(a, b) =>
new Date(b.lastEdited).getTime() -
new Date(a.lastEdited).getTime(),
) || []
}
onSelect={handleAgentSelect}
onCancel={handleClose}
onNext={handleNextFromSelect}
onClose={handleClose}
onOpenBuilder={() => router.push("/build")}
/>
{loading ? (
<div className="flex items-center justify-center p-8 text-gray-600">
Loading agents...
</div>
) : (
<>
<PublishAgentSelect
agents={
myAgents?.agents
.map((agent) => ({
name: agent.agent_name,
id: agent.agent_id,
version: agent.agent_version,
lastEdited: agent.last_edited,
imageSrc:
agent.agent_image ||
"https://picsum.photos/300/200",
}))
.sort(
(a, b) =>
new Date(b.lastEdited).getTime() -
new Date(a.lastEdited).getTime(),
) || []
}
onSelect={handleAgentSelect}
onCancel={handleClose}
onNext={handleNextFromSelect}
onClose={handleClose}
onOpenBuilder={() => router.push("/build")}
/>
<div className="flex items-center justify-between p-4">
<button
onClick={() =>
setCurrentPage((p) => Math.max(1, p - 1))
}
disabled={currentPage === 1}
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Swap this from page based to infinite scroll once the last page comes into view

className="text-sm font-medium text-blue-600 disabled:text-gray-400"
>
← Previous
</button>
<span className="text-sm text-gray-700">
Page {currentPage} of {totalPages}
</span>
<button
onClick={() =>
setCurrentPage((p) => Math.min(totalPages, p + 1))
}
disabled={currentPage === totalPages}
className="text-sm font-medium text-blue-600 disabled:text-gray-400"
>
Next →
</button>
</div>
</>
)}
</div>
</div>
</div>
Expand Down
Loading