From e5fb4e4aae1e892c2475d98f801c210ceb48840e Mon Sep 17 00:00:00 2001 From: Vincenzo Scamporlino Date: Sun, 12 Nov 2023 23:26:59 +0100 Subject: [PATCH] catalog-react: expose pageInfo from context Signed-off-by: Vincenzo Scamporlino --- .../src/hooks/useEntityListProvider.tsx | 42 +++++++++---------- 1 file changed, 21 insertions(+), 21 deletions(-) diff --git a/plugins/catalog-react/src/hooks/useEntityListProvider.tsx b/plugins/catalog-react/src/hooks/useEntityListProvider.tsx index 2e187789fd..fb05cd2fe7 100644 --- a/plugins/catalog-react/src/hooks/useEntityListProvider.tsx +++ b/plugins/catalog-react/src/hooks/useEntityListProvider.tsx @@ -96,8 +96,6 @@ export type EntityListContextProps< | ((prevFilters: EntityFilters) => Partial), ) => void; - next?: () => void; - prev?: () => void; /** * Filter values from query parameters. */ @@ -105,6 +103,11 @@ export type EntityListContextProps< loading: boolean; error?: Error; + + pageInfo?: { + next?: () => void; + prev?: () => void; + }; }; /** @@ -152,8 +155,10 @@ export const EntityListProvider = ( }); return { - queryParameters: - parsed.filters ?? ({} as Record), + queryParameters: (parsed.filters ?? {}) as Record< + string, + string | string[] + >, cursor: typeof parsed.cursor === 'string' ? parsed.cursor : undefined, }; }, [location]); @@ -166,6 +171,7 @@ export const EntityListProvider = ( appliedFilters: {} as EntityFilters, entities: [], backendEntities: [], + pageInfo: props.enablePagination ? {} : undefined, }; }, ); @@ -311,23 +317,18 @@ export const EntityListProvider = ( [], ); - const next = useMemo(() => { - const newCursor = outputState.pageInfo?.nextCursor; - if (!newCursor || !props.enablePagination) { + const pageInfo = useMemo(() => { + if (!props.enablePagination) { return undefined; } - return () => setCursor(newCursor); - }, [outputState.pageInfo?.nextCursor, props.enablePagination]); - - const prev = useMemo(() => { - const newCursor = outputState.pageInfo?.prevCursor; - if (!newCursor || !props.enablePagination) { - return undefined; - } - - return () => setCursor(newCursor); - }, [outputState.pageInfo?.prevCursor, props.enablePagination]); + const prevCursor = outputState.pageInfo?.prevCursor; + const nextCursor = outputState.pageInfo?.nextCursor; + return { + prev: prevCursor ? () => setCursor(prevCursor) : undefined, + next: nextCursor ? () => setCursor(nextCursor) : undefined, + }; + }, [props.enablePagination, outputState.pageInfo]); const value = useMemo( () => ({ @@ -338,10 +339,9 @@ export const EntityListProvider = ( queryParameters, loading, error, - next, - prev, + pageInfo, }), - [outputState, updateFilters, queryParameters, loading, error, next, prev], + [outputState, updateFilters, queryParameters, loading, error, pageInfo], ); return (