@aws-northstar/ui
Version:
NorthStar Design System v2
54 lines (50 loc) • 3.99 kB
JavaScript
import { jsx as _jsx } from "react/jsx-runtime";
/** *******************************************************************************************************************
Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved.
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 TextFilter from '@cloudscape-design/components/text-filter';
import Pagination from '@cloudscape-design/components/pagination';
import Table from '@cloudscape-design/components/table';
import orderBy from 'lodash/orderBy';
import get from 'lodash/get';
import chunk from 'lodash/chunk';
import { useMemo, useState } from 'react';
/**
* Extends the Cloudscape Table component with pagination options for @tanstack/react-query infinite query hooks.
* Compatible with generated hooks for paginated operations from <a href='https://aws.github.io/aws-pdk/developer_guides/type-safe-api/index.html' target='_blank' rel='noreferrer noopener'>AWS PDK Type Safe API</a>.
*/
const InfiniteQueryTable = ({ query, itemsKey, pageSize, clientSideSort, clientSideTextFilter, extendData, ...tableProps }) => {
const [currentPage, setCurrentPage] = useState(1);
const [filterText, setFilterText] = useState('');
const [sortingColumn, setSortingColumn] = useState(clientSideSort?.defaultSortingColumn);
const [sortingDescending, setSortingDescending] = useState(clientSideSort?.defaultSortingDescending);
const allLocalData = useMemo(() => {
const flattenedData = (query?.data?.pages ?? []).flatMap((p) => p[itemsKey]);
return extendData ? extendData(flattenedData) : flattenedData;
}, [query.data?.pages, extendData, itemsKey]);
const filteredData = useMemo(() => allLocalData.filter((item) => clientSideTextFilter ? clientSideTextFilter.filterFunction(filterText, item) : true), [filterText, clientSideTextFilter, allLocalData]);
const sortedData = useMemo(() => sortingColumn?.sortingField
? orderBy(filteredData, (s) => get(s, sortingColumn.sortingField), sortingDescending ? 'desc' : 'asc')
: filteredData, [filteredData, sortingColumn, sortingDescending]);
const pages = useMemo(() => chunk(sortedData, pageSize ?? 100), [sortedData, pageSize]);
return (_jsx(Table, { pagination: _jsx(Pagination, { currentPageIndex: currentPage, pagesCount: pages.length, openEnd: query.hasNextPage, onChange: (e) => setCurrentPage(e.detail.currentPageIndex), onNextPageClick: (e) => {
if (!e.detail.requestedPageAvailable) {
void query.fetchNextPage();
}
} }), items: query.isError ? [] : pages[currentPage - 1], loading: query.isLoading || query.isFetchingNextPage || query.isRefetching, sortingColumn: sortingColumn, sortingDescending: sortingDescending, sortingDisabled: !clientSideSort, filter: clientSideTextFilter ? (_jsx(TextFilter, { filteringText: filterText, filteringPlaceholder: clientSideTextFilter.placeholder, onChange: (e) => setFilterText(e.detail.filteringText) })) : undefined, ...tableProps, onSortingChange: clientSideSort
? (e) => {
setSortingColumn(e.detail.sortingColumn);
setSortingDescending(e.detail.isDescending);
}
: tableProps?.onSortingChange }));
};
export default InfiniteQueryTable;