import { useCallback, useState } from 'react'; import type { ArticlePreview, GraphQLConnection, GraphQLEdge, Maybe, WPPostPreview, } from '../../../types'; import { type UsePaginationConfig, usePagination, type UsePaginationReturn, } from '../use-pagination'; import { convertPostPreviewToArticlePreview } from 'src/services/graphql'; export type usePostsListReturn = Omit< UsePaginationReturn, 'data' > & { /** * The articles list. */ articles: Maybe[]>; /** * The index of the first new result when loading more posts. */ firstNewResultIndex: Maybe; }; export const usePostsList = ( config: UsePaginationConfig ): usePostsListReturn => { const { data, error, hasNextPage, isEmpty, isError, isLoading, isLoadingMore, isRefreshing, isValidating, loadMore, size, } = usePagination(config); const [firstNewResultIndex, setFirstNewResultIndex] = useState>(undefined); const handleLoadMore = useCallback(async () => { setFirstNewResultIndex(size * config.perPage + 1); await loadMore(); }, [config.perPage, loadMore, size]); const articles: Maybe[]> = data?.map( (page): GraphQLConnection => { return { edges: page.edges.map((edge): GraphQLEdge => { return { cursor: edge.cursor, node: convertPostPreviewToArticlePreview(edge.node), }; }), pageInfo: page.pageInfo, }; } ); return { articles, error, firstNewResultIndex, hasNextPage, isEmpty, isError, isLoading, isLoadingMore, isRefreshing, isValidating, loadMore: handleLoadMore, size, }; };