From a77572d076bafd9850624c542d0b3597b25deef3 Mon Sep 17 00:00:00 2001 From: Vincenzo Scamporlino Date: Thu, 16 Nov 2023 17:05:12 +0100 Subject: [PATCH] catalog: customize limit Signed-off-by: Vincenzo Scamporlino --- .changeset/silly-numbers-wash.md | 3 +-- plugins/catalog-react/api-report.md | 6 ++++- .../src/hooks/useEntityListProvider.test.tsx | 2 +- .../src/hooks/useEntityListProvider.tsx | 24 +++++++++++++------ plugins/catalog/config.d.ts | 14 ++++++----- .../CatalogPage/DefaultCatalogPage.tsx | 8 +++---- 6 files changed, 36 insertions(+), 21 deletions(-) diff --git a/.changeset/silly-numbers-wash.md b/.changeset/silly-numbers-wash.md index a4fd20ea8c..ba437006dd 100644 --- a/.changeset/silly-numbers-wash.md +++ b/.changeset/silly-numbers-wash.md @@ -10,6 +10,5 @@ To activate the pagination mode, simply update your configuration as follows: ```diff catalog: -+ experimental: -+ paginatedEntities: true ++ experimentalPagination: true ``` diff --git a/plugins/catalog-react/api-report.md b/plugins/catalog-react/api-report.md index bbe51070c8..9c1a6249ae 100644 --- a/plugins/catalog-react/api-report.md +++ b/plugins/catalog-react/api-report.md @@ -297,7 +297,11 @@ export const EntityListProvider: ( // @public (undocumented) export type EntityListProviderProps = PropsWithChildren<{ - enablePagination?: boolean; + enablePagination?: + | boolean + | { + limit?: number; + }; }>; // @public (undocumented) diff --git a/plugins/catalog-react/src/hooks/useEntityListProvider.test.tsx b/plugins/catalog-react/src/hooks/useEntityListProvider.test.tsx index c4a02bd122..5838c79cdd 100644 --- a/plugins/catalog-react/src/hooks/useEntityListProvider.test.tsx +++ b/plugins/catalog-react/src/hooks/useEntityListProvider.test.tsx @@ -275,7 +275,7 @@ describe('', () => { describe('', () => { const origReplaceState = window.history.replaceState; const enablePagination = true; - const limit = 2; + const limit = 20; const orderFields = [{ field: 'metadata.name', order: 'asc' }]; beforeEach(() => { diff --git a/plugins/catalog-react/src/hooks/useEntityListProvider.tsx b/plugins/catalog-react/src/hooks/useEntityListProvider.tsx index 63066e0592..9eaaab7da2 100644 --- a/plugins/catalog-react/src/hooks/useEntityListProvider.tsx +++ b/plugins/catalog-react/src/hooks/useEntityListProvider.tsx @@ -130,7 +130,7 @@ type OutputState = { * @public */ export type EntityListProviderProps = PropsWithChildren<{ - enablePagination?: boolean; + enablePagination?: boolean | { limit?: number }; }>; /** @@ -152,6 +152,17 @@ export const EntityListProvider = ( // update of the URL or two catalog sidebar links with different catalog filters. const location = useLocation(); + const enablePagination = + props.enablePagination === true || + typeof props.enablePagination === 'object'; + + const limit = + props.enablePagination && + typeof props.enablePagination === 'object' && + typeof props.enablePagination.limit === 'number' + ? props.enablePagination.limit + : 20; + const { queryParameters, cursor: initialCursor } = useMemo(() => { const parsed = qs.parse(location.search, { ignoreQueryPrefix: true, @@ -174,7 +185,7 @@ export const EntityListProvider = ( appliedFilters: {} as EntityFilters, entities: [], backendEntities: [], - pageInfo: props.enablePagination ? {} : undefined, + pageInfo: enablePagination ? {} : undefined, }; }, ); @@ -199,8 +210,7 @@ export const EntityListProvider = ( {} as Record, ); - if (props.enablePagination) { - const limit = 2; + if (enablePagination) { if (cursor) { if (cursor !== outputState.appliedCursor) { const entityFilter = reduceEntityFilters(compacted); @@ -290,7 +300,7 @@ export const EntityListProvider = ( requestedFilters, outputState, cursor, - props.enablePagination, + enablePagination, ], { loading: true }, ); @@ -321,7 +331,7 @@ export const EntityListProvider = ( ); const pageInfo = useMemo(() => { - if (!props.enablePagination) { + if (!enablePagination) { return undefined; } @@ -331,7 +341,7 @@ export const EntityListProvider = ( prev: prevCursor ? () => setCursor(prevCursor) : undefined, next: nextCursor ? () => setCursor(nextCursor) : undefined, }; - }, [props.enablePagination, outputState.pageInfo]); + }, [enablePagination, outputState.pageInfo]); const value = useMemo( () => ({ diff --git a/plugins/catalog/config.d.ts b/plugins/catalog/config.d.ts index a853d660c8..af749db905 100644 --- a/plugins/catalog/config.d.ts +++ b/plugins/catalog/config.d.ts @@ -15,11 +15,13 @@ */ export interface Config { catalog?: { - experimental?: { - /** - * @visibility frontend - */ - paginatedEntities?: boolean; - }; + /** + * @deepVisibility frontend + */ + experimentalPagination?: + | boolean + | { + limit?: number; + }; }; } diff --git a/plugins/catalog/src/components/CatalogPage/DefaultCatalogPage.tsx b/plugins/catalog/src/components/CatalogPage/DefaultCatalogPage.tsx index 4797871fbf..68632e5708 100644 --- a/plugins/catalog/src/components/CatalogPage/DefaultCatalogPage.tsx +++ b/plugins/catalog/src/components/CatalogPage/DefaultCatalogPage.tsx @@ -60,9 +60,9 @@ export function BaseCatalogPage(props: BaseCatalogPageProps) { const createComponentLink = useRouteRef(createComponentRouteRef); const { t } = useTranslationRef(catalogTranslationRef); - const enablePagination = useApi(configApiRef).getOptionalBoolean( - 'catalog.experimental.paginatedEntities', - ); + const experimentalPagination = useApi(configApiRef).getOptional( + 'catalog.experimentalPagination', + ) as boolean | { limit: number } | undefined; return ( @@ -74,7 +74,7 @@ export function BaseCatalogPage(props: BaseCatalogPageProps) { /> All your software catalog entities - + {filters} {content}