From 970cb48e07d994b900e618e295fb1bd68d520511 Mon Sep 17 00:00:00 2001 From: Andreas Berger Date: Mon, 13 Jan 2025 09:16:43 +0100 Subject: [PATCH] Harmonize `CatalogTable` - Show pagination text for `OffsetPagination` - Use same `OffsetPaginatedCatalogTable` also as fallback if no pagination is set - Do not show paging if there is only one page Signed-off-by: Andreas Berger --- .changeset/funny-papayas-tell.md | 9 ++++ .../components/CatalogTable/CatalogTable.tsx | 47 +++++++------------ .../CursorPaginatedCatalogTable.tsx | 1 - .../OffsetPaginatedCatalogTable.tsx | 9 ++-- 4 files changed, 30 insertions(+), 36 deletions(-) create mode 100644 .changeset/funny-papayas-tell.md diff --git a/.changeset/funny-papayas-tell.md b/.changeset/funny-papayas-tell.md new file mode 100644 index 0000000000..d3c58e95c3 --- /dev/null +++ b/.changeset/funny-papayas-tell.md @@ -0,0 +1,9 @@ +--- +'@backstage/plugin-catalog': minor +--- + +Harmonize `CatalogTable` + +- Show pagination text for `OffsetPagination` +- Use same `OffsetPaginatedCatalogTable` also as fallback if no pagination is set +- Do not show paging if there is only one page diff --git a/plugins/catalog/src/components/CatalogTable/CatalogTable.tsx b/plugins/catalog/src/components/CatalogTable/CatalogTable.tsx index 9b3a950ffd..2076750da8 100644 --- a/plugins/catalog/src/components/CatalogTable/CatalogTable.tsx +++ b/plugins/catalog/src/components/CatalogTable/CatalogTable.tsx @@ -23,7 +23,6 @@ import { } from '@backstage/catalog-model'; import { CodeSnippet, - Table, TableColumn, TableProps, WarningPanel, @@ -47,7 +46,7 @@ import { OffsetPaginatedCatalogTable } from './OffsetPaginatedCatalogTable'; import { CursorPaginatedCatalogTable } from './CursorPaginatedCatalogTable'; import { defaultCatalogTableColumnsFunc } from './defaultCatalogTableColumnsFunc'; import { useTranslationRef } from '@backstage/core-plugin-api/alpha'; -import { catalogTranslationRef } from '../../alpha/translation'; +import { catalogTranslationRef } from '../../alpha'; import { FavoriteToggleIcon } from '@backstage/core-components'; /** @@ -194,7 +193,8 @@ export const CatalogTable = (props: CatalogTableProps) => { .join(' '); const actions = props.actions || defaultActions; - const options = { + const options: TableProps['options'] = { + paginationPosition: 'both', actionsColumnIndex: -1, loadingType: 'linear' as const, showEmptyDataSourceMessage: !loading, @@ -202,6 +202,12 @@ export const CatalogTable = (props: CatalogTableProps) => { ...tableOptions, }; + if (paginationMode !== 'cursor' && paginationMode !== 'offset') { + entities.sort(refCompare); + } + + const rows = entities.map(toEntityRow); + if (paginationMode === 'cursor') { return ( { actions={actions} subtitle={subtitle} options={options} - data={entities.map(toEntityRow)} + data={rows} next={pageInfo?.next} prev={pageInfo?.prev} /> ); - } else if (paginationMode === 'offset') { - return ( - - ); } - const rows = entities.sort(refCompare).map(toEntityRow); - const pageSize = 20; - const showPagination = rows.length > pageSize; - + // else use offset paging return ( - - isLoading={loading} + ); }; diff --git a/plugins/catalog/src/components/CatalogTable/CursorPaginatedCatalogTable.tsx b/plugins/catalog/src/components/CatalogTable/CursorPaginatedCatalogTable.tsx index e49ecf3cd9..1fcc2683be 100644 --- a/plugins/catalog/src/components/CatalogTable/CursorPaginatedCatalogTable.tsx +++ b/plugins/catalog/src/components/CatalogTable/CursorPaginatedCatalogTable.tsx @@ -35,7 +35,6 @@ export function CursorPaginatedCatalogTable(props: PaginatedCatalogTableProps) { columns={columns} data={data} options={{ - paginationPosition: 'both', ...options, // These settings are configured to force server side pagination pageSizeOptions: [], diff --git a/plugins/catalog/src/components/CatalogTable/OffsetPaginatedCatalogTable.tsx b/plugins/catalog/src/components/CatalogTable/OffsetPaginatedCatalogTable.tsx index 96c81088ca..7f599cad76 100644 --- a/plugins/catalog/src/components/CatalogTable/OffsetPaginatedCatalogTable.tsx +++ b/plugins/catalog/src/components/CatalogTable/OffsetPaginatedCatalogTable.tsx @@ -36,21 +36,23 @@ export function OffsetPaginatedCatalogTable( useEffect(() => { if (totalItems && page * limit >= totalItems) { - setOffset!(Math.max(0, totalItems - limit)); + setOffset?.(Math.max(0, totalItems - limit)); } else { - setOffset!(Math.max(0, page * limit)); + setOffset?.(Math.max(0, page * limit)); } }, [setOffset, page, limit, totalItems]); + const showPagination = (totalItems ?? data.length) > limit; + return ( );