Skip to content

Commit e4adf83

Browse files
committed
Fix resetting page index to 0 when reloading, add page index bounds guard
1 parent f401ef1 commit e4adf83

File tree

6 files changed

+48
-6
lines changed

6 files changed

+48
-6
lines changed

.changeset/tall-cows-fry.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
'@orchestrator-ui/orchestrator-ui-components': patch
3+
---
4+
5+
Fix resetting page index to 0 when reloading, add page index bounds guard

package-lock.json

Lines changed: 4 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

packages/orchestrator-ui-components/src/components/WfoTable/WfoTable/WfoTable.tsx

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,11 @@ import { getPageCount } from '../utils/tableUtils';
1818
import { WfoTableDataRows } from './WfoTableDataRows';
1919
import { WfoTableHeaderRow } from './WfoTableHeaderRow';
2020
import { getWfoTableStyles } from './styles';
21-
import { getColumnWidthsFromConfig, getSortedVisibleColumns } from './utils';
21+
import {
22+
getColumnWidthsFromConfig,
23+
getSortedVisibleColumns,
24+
usePageIndexBoundsGuard,
25+
} from './utils';
2226

2327
export type Pagination = {
2428
pageSize: number;
@@ -133,6 +137,8 @@ export const WfoTable = <T extends object>({
133137
}: WfoTableProps<T>) => {
134138
const [localColumnWidths, setLocalColumnWidths] =
135139
useState<LocalColumnWidths>(getColumnWidthsFromConfig(columnConfig));
140+
const dataLength = data.length;
141+
usePageIndexBoundsGuard({ dataLength, isLoading, pagination });
136142

137143
const parentRef = useRef<HTMLDivElement>(null);
138144

@@ -191,7 +197,7 @@ export const WfoTable = <T extends object>({
191197
}, {} as WfoTableColumnConfig<T>);
192198

193199
const rowVirtualizer = useVirtualizer({
194-
count: data.length,
200+
count: dataLength,
195201
getScrollElement: () => parentRef.current,
196202
estimateSize: () => ROW_HEIGHT,
197203
overscan: 10,
@@ -231,7 +237,7 @@ export const WfoTable = <T extends object>({
231237
/>
232238
</thead>
233239
)}
234-
{data.length === 0 ? (
240+
{dataLength === 0 ? (
235241
<tbody css={isLoading && bodyLoadingStyle}>
236242
<tr css={rowStyle}>
237243
<td

packages/orchestrator-ui-components/src/components/WfoTable/WfoTable/utils.ts

Lines changed: 29 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,6 @@
1-
import { LocalColumnWidths, TableColumnKeys } from '@/components';
1+
import { useEffect } from 'react';
2+
3+
import { LocalColumnWidths, Pagination, TableColumnKeys } from '@/components';
24
import { SortOrder } from '@/types';
35
import { toObjectWithSortedKeys } from '@/utils';
46

@@ -93,3 +95,29 @@ export const getColumnWidthsFromConfig = <T extends object>(
9395
return columnWidths;
9496
}, {} as LocalColumnWidths);
9597
};
98+
99+
export const usePageIndexBoundsGuard = ({
100+
dataLength,
101+
isLoading,
102+
pagination,
103+
}: {
104+
dataLength: number;
105+
isLoading: boolean;
106+
pagination?: Pagination;
107+
}) => {
108+
useEffect(() => {
109+
if (!pagination) return;
110+
111+
const { pageIndex, pageSize, totalItemCount, onChangePage } =
112+
pagination;
113+
114+
const maxPage = Math.max(
115+
0,
116+
Math.floor(((totalItemCount ?? 1) - 1) / pageSize),
117+
);
118+
119+
if (!isLoading && dataLength === 0 && pageIndex > maxPage) {
120+
onChangePage?.(maxPage);
121+
}
122+
}, [dataLength, isLoading, pagination]);
123+
};

packages/orchestrator-ui-components/src/components/WfoTable/utils/tableUtils.ts

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -64,7 +64,6 @@ export const getQueryStringHandler =
6464
? `${queryString}*`
6565
: queryString;
6666
setDataDisplayParam('queryString', query);
67-
setDataDisplayParam('pageIndex', 0);
6867
};
6968

7069
export const getPageCount = (pagination: Pagination) =>

0 commit comments

Comments
 (0)