import { useInfiniteQuery } from "@tanstack/react-query"; import { Loader } from "@mantine/core"; import { useTranslation } from "react-i18next"; import { getSidebarPages } from "@/features/page/services/page-service"; import { IPage } from "@/features/page/types/page.types"; import { IPagination } from "@/lib/types"; import { PageRow } from "./page-row"; import classes from "./destination-picker.module.css"; type PageChildrenProps = { spaceId: string; pageId?: string; depth: number; limit: number; selectedId: string | null; excludePageId?: string; onSelectPage: (page: Partial) => void; }; export function PageChildren({ spaceId, pageId, depth, limit, selectedId, excludePageId, onSelectPage, }: PageChildrenProps) { const { t } = useTranslation(); const { data, isLoading, hasNextPage, fetchNextPage } = useInfiniteQuery({ queryKey: ["destination-pages", spaceId, pageId ?? "root"], queryFn: ({ pageParam }) => getSidebarPages({ spaceId, pageId, limit, cursor: pageParam, }), initialPageParam: undefined as string | undefined, getNextPageParam: (lastPage: IPagination) => lastPage.meta?.nextCursor ?? undefined, }); const pages = data?.pages.flatMap((page) => page.items) ?? []; if (isLoading) { return (
); } if (pages.length === 0) { return (
{pageId ? t("No pages inside") : t("No pages in this space")}
); } return ( <> {pages.map((page) => ( ))} {hasNextPage && (
fetchNextPage()}> {t("Load more")}
)} ); }