mirror of
https://github.com/portainer/portainer.git
synced 2025-07-23 15:29:42 +02:00
refactor(ui/datatables): migrate views to use datatable component [EE-4064] (#7609)
This commit is contained in:
parent
0f0513c684
commit
fe8e834dbf
90 changed files with 1714 additions and 2717 deletions
51
app/react/components/datatables/useGoToHighlightedRow.ts
Normal file
51
app/react/components/datatables/useGoToHighlightedRow.ts
Normal file
|
@ -0,0 +1,51 @@
|
|||
import _ from 'lodash';
|
||||
import { useRef, useLayoutEffect, useEffect } from 'react';
|
||||
|
||||
export function useGoToHighlightedRow<T extends { id: string }>(
|
||||
isServerSidePagination: boolean,
|
||||
pageSize: number,
|
||||
rows: Array<T>,
|
||||
goToPage: (page: number) => void,
|
||||
highlightedItemId?: string
|
||||
) {
|
||||
const handlePageChangeRef = useRef(goToPage);
|
||||
useLayoutEffect(() => {
|
||||
handlePageChangeRef.current = goToPage;
|
||||
});
|
||||
|
||||
const highlightedItemIdRef = useRef(highlightedItemId);
|
||||
|
||||
useEffect(() => {
|
||||
if (
|
||||
!isServerSidePagination &&
|
||||
highlightedItemId &&
|
||||
highlightedItemId !== highlightedItemIdRef.current
|
||||
) {
|
||||
const page = getRowPage(highlightedItemId, pageSize, rows);
|
||||
if (page) {
|
||||
handlePageChangeRef.current(page);
|
||||
}
|
||||
highlightedItemIdRef.current = highlightedItemId;
|
||||
}
|
||||
}, [highlightedItemId, isServerSidePagination, rows, pageSize]);
|
||||
}
|
||||
|
||||
function getRowPage<T extends { id: string }>(
|
||||
rowID: string,
|
||||
pageSize: number,
|
||||
rows: Array<T>
|
||||
) {
|
||||
const totalRows = rows.length;
|
||||
|
||||
if (!rowID || pageSize > totalRows) {
|
||||
return 0;
|
||||
}
|
||||
|
||||
const paginatedData = _.chunk(rows, pageSize);
|
||||
|
||||
const itemPage = paginatedData.findIndex((sub) =>
|
||||
sub.some((row) => row.id === rowID)
|
||||
);
|
||||
|
||||
return itemPage;
|
||||
}
|
Loading…
Add table
Add a link
Reference in a new issue