Skip to content

Commit

Permalink
Harmonize CatalogTable
Browse files Browse the repository at this point in the history
- 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 <andreas@berger-ecommerce.com>
  • Loading branch information
Andy2003 committed Jan 13, 2025
1 parent 6c1cb93 commit d563f05
Show file tree
Hide file tree
Showing 4 changed files with 30 additions and 40 deletions.
9 changes: 9 additions & 0 deletions .changeset/funny-papayas-tell.md
Original file line number Diff line number Diff line change
@@ -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
51 changes: 16 additions & 35 deletions plugins/catalog/src/components/CatalogTable/CatalogTable.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,6 @@ import {
} from '@backstage/catalog-model';
import {
CodeSnippet,
Table,
TableColumn,
TableProps,
WarningPanel,
Expand All @@ -47,9 +46,8 @@ 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';
import { CatalogTableToolbar } from './CatalogTableToolbar';

/**
* Props for {@link CatalogTable}.
Expand Down Expand Up @@ -195,14 +193,21 @@ 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,
padding: 'dense' as const,
...tableOptions,
};

if (paginationMode !== 'cursor' && paginationMode !== 'offset') {
entities.sort(refCompare);
}

const rows = entities.map(toEntityRow);

if (paginationMode === 'cursor') {
return (
<CursorPaginatedCatalogTable
Expand All @@ -213,48 +218,24 @@ export const CatalogTable = (props: CatalogTableProps) => {
actions={actions}
subtitle={subtitle}
options={options}
data={entities.map(toEntityRow)}
data={rows}
next={pageInfo?.next}
prev={pageInfo?.prev}
/>
);
} else if (paginationMode === 'offset') {
return (
<OffsetPaginatedCatalogTable
columns={tableColumns}
emptyContent={emptyContent}
isLoading={loading}
title={title}
actions={actions}
subtitle={subtitle}
options={options}
data={entities.map(toEntityRow)}
/>
);
}

const rows = entities.sort(refCompare).map(toEntityRow);
const pageSize = 20;
const showPagination = rows.length > pageSize;

// else use offset paging
return (
<Table<CatalogTableRow>
isLoading={loading}
<OffsetPaginatedCatalogTable
columns={tableColumns}
options={{
paging: showPagination,
pageSize: pageSize,
pageSizeOptions: [20, 50, 100],
...options,
}}
components={{
Toolbar: CatalogTableToolbar,
}}
emptyContent={emptyContent}
isLoading={loading}
title={title}
data={rows}
actions={actions}
subtitle={subtitle}
emptyContent={emptyContent}
options={options}
data={rows}
/>
);
};
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,6 @@ export function CursorPaginatedCatalogTable(props: PaginatedCatalogTableProps) {
columns={columns}
data={data}
options={{
paginationPosition: 'both',
...options,
// These settings are configured to force server side pagination
pageSizeOptions: [],
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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 (
<Table
columns={columns}
data={data}
options={{
paginationPosition: 'both',
pageSizeOptions: [5, 10, 20, 50, 100],
pageSize: limit,
emptyRowsWhenPaging: false,
paging: showPagination,
...options,
}}
components={{
Expand All @@ -64,7 +66,6 @@ export function OffsetPaginatedCatalogTable(
setLimit(pageSize);
}}
totalCount={totalItems}
localization={{ pagination: { labelDisplayedRows: '' } }}
{...restProps}
/>
);
Expand Down

0 comments on commit d563f05

Please sign in to comment.