Skip to content

Commit 4e5584c

Browse files
author
Dylan Huang
committed
store page and page size state in globalstate and persist
1 parent 6b018d4 commit 4e5584c

2 files changed

Lines changed: 103 additions & 24 deletions

File tree

vite-app/src/GlobalState.tsx

Lines changed: 86 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,12 @@ const DEFAULT_PIVOT_CONFIG: PivotConfig = {
1212
filters: [],
1313
};
1414

15+
// Default pagination configuration
16+
const DEFAULT_PAGINATION_CONFIG = {
17+
currentPage: 1,
18+
pageSize: 25,
19+
};
20+
1521
export class GlobalState {
1622
isConnected: boolean = false;
1723
// rollout_id -> EvaluationRow
@@ -20,10 +26,17 @@ export class GlobalState {
2026
expandedRows: Record<string, boolean> = {};
2127
// Pivot configuration
2228
pivotConfig: PivotConfig;
29+
// Pagination configuration
30+
currentPage: number;
31+
pageSize: number;
2332

2433
constructor() {
2534
// Load pivot config from localStorage or use defaults
2635
this.pivotConfig = this.loadPivotConfig();
36+
// Load pagination config from localStorage or use defaults
37+
const paginationConfig = this.loadPaginationConfig();
38+
this.currentPage = paginationConfig.currentPage;
39+
this.pageSize = paginationConfig.pageSize;
2740
makeAutoObservable(this);
2841
}
2942

@@ -42,6 +55,21 @@ export class GlobalState {
4255
return { ...DEFAULT_PIVOT_CONFIG };
4356
}
4457

58+
// Load pagination configuration from localStorage
59+
private loadPaginationConfig() {
60+
try {
61+
const stored = localStorage.getItem("paginationConfig");
62+
if (stored) {
63+
const parsed = JSON.parse(stored);
64+
// Merge with defaults to handle any missing properties
65+
return { ...DEFAULT_PAGINATION_CONFIG, ...parsed };
66+
}
67+
} catch (error) {
68+
console.warn("Failed to load pagination config from localStorage:", error);
69+
}
70+
return { ...DEFAULT_PAGINATION_CONFIG };
71+
}
72+
4573
// Save pivot configuration to localStorage
4674
private savePivotConfig() {
4775
try {
@@ -51,12 +79,35 @@ export class GlobalState {
5179
}
5280
}
5381

82+
// Save pagination configuration to localStorage
83+
private savePaginationConfig() {
84+
try {
85+
localStorage.setItem("paginationConfig", JSON.stringify({
86+
currentPage: this.currentPage,
87+
pageSize: this.pageSize,
88+
}));
89+
} catch (error) {
90+
console.warn("Failed to save pagination config to localStorage:", error);
91+
}
92+
}
93+
5494
// Update pivot configuration and save to localStorage
5595
updatePivotConfig(updates: Partial<PivotConfig>) {
5696
Object.assign(this.pivotConfig, updates);
5797
this.savePivotConfig();
5898
}
5999

100+
// Update pagination configuration and save to localStorage
101+
updatePaginationConfig(updates: Partial<{ currentPage: number; pageSize: number }>) {
102+
if (updates.currentPage !== undefined) {
103+
this.currentPage = updates.currentPage;
104+
}
105+
if (updates.pageSize !== undefined) {
106+
this.pageSize = updates.pageSize;
107+
}
108+
this.savePaginationConfig();
109+
}
110+
60111
// Reset pivot configuration to defaults
61112
resetPivotConfig() {
62113
this.pivotConfig = {
@@ -66,13 +117,36 @@ export class GlobalState {
66117
this.savePivotConfig();
67118
}
68119

120+
// Reset pagination configuration to defaults
121+
resetPaginationConfig() {
122+
this.currentPage = DEFAULT_PAGINATION_CONFIG.currentPage;
123+
this.pageSize = DEFAULT_PAGINATION_CONFIG.pageSize;
124+
this.savePaginationConfig();
125+
}
126+
127+
// Set current page
128+
setCurrentPage(page: number) {
129+
this.currentPage = page;
130+
this.savePaginationConfig();
131+
}
132+
133+
// Set page size
134+
setPageSize(size: number) {
135+
this.pageSize = size;
136+
this.currentPage = 1; // Reset to first page when changing page size
137+
this.savePaginationConfig();
138+
}
139+
69140
upsertRows(dataset: EvaluationRow[]) {
70141
dataset.forEach((row) => {
71142
if (!row.execution_metadata?.rollout_id) {
72143
return;
73144
}
74145
this.dataset[row.execution_metadata.rollout_id] = row;
75146
});
147+
// Reset to first page when dataset changes
148+
this.currentPage = 1;
149+
this.savePaginationConfig();
76150
}
77151

78152
toggleRowExpansion(rolloutId?: string) {
@@ -122,4 +196,16 @@ export class GlobalState {
122196
get totalCount() {
123197
return Object.keys(this.dataset).length;
124198
}
199+
200+
get totalPages() {
201+
return Math.ceil(this.totalCount / this.pageSize);
202+
}
203+
204+
get startRow() {
205+
return (this.currentPage - 1) * this.pageSize + 1;
206+
}
207+
208+
get endRow() {
209+
return Math.min(this.currentPage * this.pageSize, this.totalCount);
210+
}
125211
}

vite-app/src/components/EvaluationTable.tsx

Lines changed: 17 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,4 @@
11
import { observer } from "mobx-react";
2-
import { useState, useEffect } from "react";
32
import { state } from "../App";
43
import { EvaluationRow } from "./EvaluationRow";
54
import Button from "./Button";
@@ -32,28 +31,19 @@ const TableBody = observer(
3231

3332
// Dedicated component for rendering the list - following MobX best practices
3433
export const EvaluationTable = observer(() => {
35-
const [currentPage, setCurrentPage] = useState(1);
36-
const [pageSize, setPageSize] = useState(25);
37-
3834
const totalRows = state.sortedDataset.length;
39-
const totalPages = Math.ceil(totalRows / pageSize);
40-
const startRow = (currentPage - 1) * pageSize + 1;
41-
const endRow = Math.min(currentPage * pageSize, totalRows);
35+
const totalPages = state.totalPages;
36+
const startRow = state.startRow;
37+
const endRow = state.endRow;
4238

4339
const handlePageChange = (page: number) => {
44-
setCurrentPage(Math.max(1, Math.min(page, totalPages)));
40+
state.setCurrentPage(Math.max(1, Math.min(page, totalPages)));
4541
};
4642

4743
const handlePageSizeChange = (newPageSize: number) => {
48-
setPageSize(newPageSize);
49-
setCurrentPage(1); // Reset to first page when changing page size
44+
state.setPageSize(newPageSize);
5045
};
5146

52-
// Reset to first page when dataset changes
53-
useEffect(() => {
54-
setCurrentPage(1);
55-
}, [totalRows]);
56-
5747
return (
5848
<div className="bg-white border border-gray-200">
5949
{/* Pagination Controls - Fixed outside scrollable area */}
@@ -65,7 +55,7 @@ export const EvaluationTable = observer(() => {
6555
<div className="flex items-center gap-2">
6656
<label className="text-xs text-gray-600">Page size:</label>
6757
<Select
68-
value={pageSize}
58+
value={state.pageSize}
6959
onChange={(e) => handlePageSizeChange(Number(e.target.value))}
7060
size="sm"
7161
>
@@ -79,34 +69,34 @@ export const EvaluationTable = observer(() => {
7969
<div className="flex items-center gap-2">
8070
<Button
8171
onClick={() => handlePageChange(1)}
82-
disabled={currentPage === 1}
72+
disabled={state.currentPage === 1}
8373
size="sm"
8474
variant="secondary"
8575
>
8676
First
8777
</Button>
8878
<Button
89-
onClick={() => handlePageChange(currentPage - 1)}
90-
disabled={currentPage === 1}
79+
onClick={() => handlePageChange(state.currentPage - 1)}
80+
disabled={state.currentPage === 1}
9181
size="sm"
9282
variant="secondary"
9383
>
9484
Previous
9585
</Button>
9686
<span className="text-xs text-gray-600 px-2">
97-
Page {currentPage} of {totalPages}
87+
Page {state.currentPage} of {totalPages}
9888
</span>
9989
<Button
100-
onClick={() => handlePageChange(currentPage + 1)}
101-
disabled={currentPage === totalPages}
90+
onClick={() => handlePageChange(state.currentPage + 1)}
91+
disabled={state.currentPage === totalPages}
10292
size="sm"
10393
variant="secondary"
10494
>
10595
Next
10696
</Button>
10797
<Button
10898
onClick={() => handlePageChange(totalPages)}
109-
disabled={currentPage === totalPages}
99+
disabled={state.currentPage === totalPages}
110100
size="sm"
111101
variant="secondary"
112102
>
@@ -132,7 +122,10 @@ export const EvaluationTable = observer(() => {
132122
</TableHead>
133123

134124
{/* Table Body */}
135-
<TableBody currentPage={currentPage} pageSize={pageSize} />
125+
<TableBody
126+
currentPage={state.currentPage}
127+
pageSize={state.pageSize}
128+
/>
136129
</table>
137130
</div>
138131
</div>

0 commit comments

Comments
 (0)