aboutsummaryrefslogtreecommitdiffstats
path: root/src/utils/hooks/use-posts-list/use-posts-list.ts
diff options
context:
space:
mode:
Diffstat (limited to 'src/utils/hooks/use-posts-list/use-posts-list.ts')
-rw-r--r--src/utils/hooks/use-posts-list/use-posts-list.ts83
1 files changed, 0 insertions, 83 deletions
diff --git a/src/utils/hooks/use-posts-list/use-posts-list.ts b/src/utils/hooks/use-posts-list/use-posts-list.ts
deleted file mode 100644
index bb77f31..0000000
--- a/src/utils/hooks/use-posts-list/use-posts-list.ts
+++ /dev/null
@@ -1,83 +0,0 @@
-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<WPPostPreview>,
- 'data'
-> & {
- /**
- * The articles list.
- */
- articles: Maybe<GraphQLConnection<ArticlePreview>[]>;
- /**
- * The index of the first new result when loading more posts.
- */
- firstNewResultIndex: Maybe<number>;
-};
-
-export const usePostsList = (
- config: UsePaginationConfig<WPPostPreview>
-): usePostsListReturn => {
- const {
- data,
- error,
- hasNextPage,
- isEmpty,
- isError,
- isLoading,
- isLoadingMore,
- isRefreshing,
- isValidating,
- loadMore,
- size,
- } = usePagination(config);
- const [firstNewResultIndex, setFirstNewResultIndex] =
- useState<Maybe<number>>(undefined);
-
- const handleLoadMore = useCallback(async () => {
- setFirstNewResultIndex(size * config.perPage + 1);
-
- await loadMore();
- }, [config.perPage, loadMore, size]);
-
- const articles: Maybe<GraphQLConnection<ArticlePreview>[]> = data?.map(
- (page): GraphQLConnection<ArticlePreview> => {
- return {
- edges: page.edges.map((edge): GraphQLEdge<ArticlePreview> => {
- 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,
- };
-};