Skip to content

🤯 LobeHub - Super-Agents on Your Team. Add super-agents to the chats your team already uses.They take on work by themselves and get better every week.

1.7 KB
Raw Blame
'use client';

import { Pagination as Page, useResponsive } from '@lobehub/ui';
import { memo } from 'react';
import { useLocation } from 'react-router';

import { useWorkspaceAwareNavigate } from '@/features/Workspace/useWorkspaceAwareNavigate';
import { useQuery } from '@/hooks/useQuery';
import { SCROLL_PARENT_ID } from '@/routes/(main)/community/features/const';
import { type DiscoverTab } from '@/types/discover';

const SCROLL_CONTAINER_ID = 'lobe-mobile-scroll-container';

interface PaginationProps {
  currentPage: number;
  pageSize: number;
  /** The list's own path when it is not `/marketplace/<tab>` (e.g. Connectors lists MCP). */
  path?: string;
  tab?: DiscoverTab;
  total: number;
}

const Pagination = memo<PaginationProps>(({ path, tab, currentPage, total, pageSize }) => {
  const { page } = useQuery();
  const navigate = useWorkspaceAwareNavigate();
  const location = useLocation();
  const { mobile } = useResponsive();

  const handlePageChange = (newPage: number) => {
    const searchParams = new URLSearchParams(location.search);
    searchParams.set('page', String(newPage));
    navigate(`${path ?? `/marketplace/${tab}`}?${searchParams.toString()}`);

    const scrollContainerId = mobile ? SCROLL_CONTAINER_ID : SCROLL_PARENT_ID;
    const scrollableElement = document?.querySelector(`#${scrollContainerId}`);
    if (!scrollableElement) return;
    scrollableElement.scrollTo({ behavior: 'smooth', top: 0 });
  };

  return (
    <Page
      current={page ? Number(page) : currentPage}
      data-testid="pagination"
      pageSize={pageSize}
      showSizeChanger={false}
      total={total}
      style={{
        alignSelf: 'flex-end',
      }}
      onChange={handlePageChange}
    />
  );
});

export default Pagination;