From cd258e56cd2b1d67f89fa741e064bfe1c6d90e6f Mon Sep 17 00:00:00 2001 From: Yingbai He Date: Fri, 15 Nov 2024 09:37:22 -0700 Subject: [PATCH] feat(TechDocs): add and similar to Catalog Tables Signed-off-by: Yingbai He --- .../Tables/CursorPaginatedDocsTable.tsx | 65 +++++++++++++++++ .../Tables/OffsetPaginatedDocsTable.tsx | 73 +++++++++++++++++++ 2 files changed, 138 insertions(+) create mode 100644 plugins/techdocs/src/home/components/Tables/CursorPaginatedDocsTable.tsx create mode 100644 plugins/techdocs/src/home/components/Tables/OffsetPaginatedDocsTable.tsx diff --git a/plugins/techdocs/src/home/components/Tables/CursorPaginatedDocsTable.tsx b/plugins/techdocs/src/home/components/Tables/CursorPaginatedDocsTable.tsx new file mode 100644 index 0000000000..e5ed54c624 --- /dev/null +++ b/plugins/techdocs/src/home/components/Tables/CursorPaginatedDocsTable.tsx @@ -0,0 +1,65 @@ +/* + * Copyright 2024 The Backstage Authors + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +import React from 'react'; + +import { Table, TableProps } from '@backstage/core-components'; +import { DocsTableRow } from './types'; + +type PaginatedDocsTableProps = { + prev?(): void; + next?(): void; +} & TableProps; + +/** + * @internal + */ + +export function CursorPaginatedDocsTable(props: PaginatedDocsTableProps) { + const { columns, data, next, prev, title, isLoading, options, ...restProps } = + props; + + return ( + { + if (page > 0) { + next?.(); + } else { + prev?.(); + } + }} + /* this will enable the prev button accordingly */ + page={prev ? 1 : 0} + /* this will enable the next button accordingly */ + totalCount={next ? Number.MAX_VALUE : Number.MAX_SAFE_INTEGER} + localization={{ pagination: { labelDisplayedRows: '' } }} + isLoading={isLoading} + {...restProps} + /> + ); +} diff --git a/plugins/techdocs/src/home/components/Tables/OffsetPaginatedDocsTable.tsx b/plugins/techdocs/src/home/components/Tables/OffsetPaginatedDocsTable.tsx new file mode 100644 index 0000000000..bee52eca85 --- /dev/null +++ b/plugins/techdocs/src/home/components/Tables/OffsetPaginatedDocsTable.tsx @@ -0,0 +1,73 @@ +/* + * Copyright 2024 The Backstage Authors + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +import React, { useEffect } from 'react'; + +import { Table, TableProps } from '@backstage/core-components'; +import { DocsTableRow } from './types'; +import { + EntityTextFilter, + useEntityList, +} from '@backstage/plugin-catalog-react'; + +/** + * @internal + */ +export function OffsetPaginatedDocsTable(props: TableProps) { + const { columns, data, isLoading, options } = props; + const { updateFilters, setLimit, setOffset, limit, totalItems, offset } = + useEntityList(); + const [page, setPage] = React.useState( + offset && limit ? Math.floor(offset / limit) : 0, + ); + + useEffect(() => { + if (totalItems && page * limit >= totalItems) { + setOffset!(Math.max(0, totalItems - limit)); + } else { + setOffset!(Math.max(0, page * limit)); + } + }, [setOffset, page, limit, totalItems]); + + return ( + + columns={columns} + data={data} + options={{ + paginationPosition: 'both', + pageSizeOptions: [5, 10, 20, 50, 100], + pageSize: limit, + emptyRowsWhenPaging: false, + ...options, + }} + onSearchChange={(searchText: string) => + updateFilters({ + text: searchText ? new EntityTextFilter(searchText) : undefined, + }) + } + page={page} + onPageChange={newPage => { + setPage(newPage); + }} + onRowsPerPageChange={pageSize => { + setLimit(pageSize); + }} + totalCount={totalItems} + localization={{ pagination: { labelDisplayedRows: '' } }} + isLoading={isLoading} + /> + ); +}