Skip to content

Commit 6a8e73e

Browse files
author
Dylan Huang
committed
Refactor GlobalState to consolidate configuration management under GlobalConfig
- Replaced individual configuration properties (pivot, filter, pagination, sort) with a unified GlobalConfig object. - Updated methods to load and save configurations from/to localStorage as a single entity. - Adjusted computed properties to access individual configurations from GlobalConfig. - Enhanced state management for pagination and sorting updates to reflect changes in the new structure.
1 parent 95c63e8 commit 6a8e73e

1 file changed

Lines changed: 72 additions & 159 deletions

File tree

vite-app/src/GlobalState.tsx

Lines changed: 72 additions & 159 deletions
Original file line numberDiff line numberDiff line change
@@ -49,14 +49,8 @@ export class GlobalState {
4949
dataset: Record<string, EvaluationRow> = {};
5050
// rollout_id -> expanded
5151
expandedRows: Record<string, boolean> = {};
52-
// Pivot configuration
53-
pivotConfig: PivotConfig;
54-
// Unified filter configuration for both pivot and table views
55-
filterConfig: FilterGroup[];
56-
// Pagination configuration
57-
paginationConfig: PaginationConfig;
58-
// Sort configuration
59-
sortConfig: SortConfig;
52+
// Unified global configuration
53+
globalConfig: GlobalConfig;
6054
// Debounced, actually applied filter configuration (for performance while typing)
6155
appliedFilterConfig: FilterGroup[];
6256
// Loading state
@@ -71,27 +65,35 @@ export class GlobalState {
7165
private createdAtMsById: Record<string, number> = {};
7266

7367
// Debounce timers for localStorage saves and filter application
74-
private savePivotConfigTimer: ReturnType<typeof setTimeout> | null = null;
75-
private saveFilterConfigTimer: ReturnType<typeof setTimeout> | null = null;
76-
private savePaginationConfigTimer: ReturnType<typeof setTimeout> | null =
77-
null;
68+
private saveGlobalConfigTimer: ReturnType<typeof setTimeout> | null = null;
7869
private applyFilterTimer: ReturnType<typeof setTimeout> | null = null;
7970

8071
constructor() {
81-
// Load pivot config from localStorage or use defaults
82-
this.pivotConfig = this.loadPivotConfig();
83-
// Load filter config from localStorage or use defaults
84-
this.filterConfig = this.loadFilterConfig();
72+
// Load global config from localStorage or use defaults
73+
this.globalConfig = this.loadGlobalConfig();
8574
// Initialize applied filter config with current value
8675
this.appliedFilterConfig = this.filterConfig.slice();
87-
// Load pagination config from localStorage or use defaults
88-
this.paginationConfig = this.loadPaginationConfig();
89-
// Load sort config from localStorage or use defaults
90-
this.sortConfig = this.loadSortConfig();
9176
makeAutoObservable(this);
9277
this.queryParamsWatcher = new QueryParamsWatcher(this);
9378
}
9479

80+
// Computed getters for individual configs
81+
get pivotConfig(): PivotConfig {
82+
return this.globalConfig.pivotConfig;
83+
}
84+
85+
get filterConfig(): FilterGroup[] {
86+
return this.globalConfig.filterConfig;
87+
}
88+
89+
get paginationConfig(): PaginationConfig {
90+
return this.globalConfig.paginationConfig;
91+
}
92+
93+
get sortConfig(): SortConfig {
94+
return this.globalConfig.sortConfig;
95+
}
96+
9597
// Computed getters for individual pagination properties
9698
get currentPage(): number {
9799
return this.paginationConfig.currentPage;
@@ -110,133 +112,53 @@ export class GlobalState {
110112
return this.sortConfig.sortDirection;
111113
}
112114

113-
// Load pivot configuration from localStorage
114-
private loadPivotConfig(): PivotConfig {
115-
try {
116-
const stored = localStorage.getItem("pivotConfig");
117-
if (stored) {
118-
const parsed = JSON.parse(stored);
119-
// Merge with defaults to handle any missing properties
120-
return { ...DEFAULT_PIVOT_CONFIG, ...parsed };
121-
}
122-
} catch (error) {
123-
console.warn("Failed to load pivot config from localStorage:", error);
124-
}
125-
return { ...DEFAULT_PIVOT_CONFIG };
126-
}
127-
128-
// Load filter configuration from localStorage
129-
private loadFilterConfig(): FilterGroup[] {
130-
try {
131-
const stored = localStorage.getItem("filterConfig");
132-
if (stored) {
133-
const parsed = JSON.parse(stored);
134-
return Array.isArray(parsed) ? parsed : DEFAULT_FILTER_CONFIG;
135-
}
136-
} catch (error) {
137-
console.warn("Failed to load filter config from localStorage:", error);
138-
}
139-
return DEFAULT_FILTER_CONFIG;
140-
}
141-
142-
// Load pagination configuration from localStorage
143-
private loadPaginationConfig(): PaginationConfig {
144-
try {
145-
const stored = localStorage.getItem("paginationConfig");
146-
if (stored) {
147-
const parsed = JSON.parse(stored);
148-
// Merge with defaults to handle any missing properties
149-
return { ...DEFAULT_PAGINATION_CONFIG, ...parsed };
150-
}
151-
} catch (error) {
152-
console.warn(
153-
"Failed to load pagination config from localStorage:",
154-
error
155-
);
156-
}
157-
return DEFAULT_PAGINATION_CONFIG;
158-
}
159-
160-
// Load sort configuration from localStorage
161-
private loadSortConfig(): SortConfig {
115+
// Load global configuration from localStorage
116+
private loadGlobalConfig(): GlobalConfig {
162117
try {
163-
const stored = localStorage.getItem("sortConfig");
118+
const stored = localStorage.getItem("globalConfig");
164119
if (stored) {
165120
const parsed = JSON.parse(stored);
166121
// Merge with defaults to handle any missing properties
167-
return { ...DEFAULT_SORT_CONFIG, ...parsed };
122+
return {
123+
pivotConfig: { ...DEFAULT_PIVOT_CONFIG, ...parsed.pivotConfig },
124+
filterConfig: Array.isArray(parsed.filterConfig)
125+
? parsed.filterConfig
126+
: DEFAULT_FILTER_CONFIG,
127+
paginationConfig: {
128+
...DEFAULT_PAGINATION_CONFIG,
129+
...parsed.paginationConfig,
130+
},
131+
sortConfig: { ...DEFAULT_SORT_CONFIG, ...parsed.sortConfig },
132+
};
168133
}
169134
} catch (error) {
170-
console.warn("Failed to load sort config from localStorage:", error);
135+
console.warn("Failed to load global config from localStorage:", error);
171136
}
172-
return DEFAULT_SORT_CONFIG;
137+
return { ...DEFAULT_GLOBAL_CONFIG };
173138
}
174139

175-
// Save pivot configuration to localStorage
176-
private savePivotConfig() {
177-
if (this.savePivotConfigTimer) clearTimeout(this.savePivotConfigTimer);
178-
this.savePivotConfigTimer = setTimeout(() => {
140+
// Save global configuration to localStorage
141+
private saveGlobalConfig() {
142+
if (this.saveGlobalConfigTimer) clearTimeout(this.saveGlobalConfigTimer);
143+
this.saveGlobalConfigTimer = setTimeout(() => {
179144
try {
180-
localStorage.setItem("pivotConfig", JSON.stringify(this.pivotConfig));
145+
localStorage.setItem("globalConfig", JSON.stringify(this.globalConfig));
181146
} catch (error) {
182-
console.warn("Failed to save pivot config to localStorage:", error);
183-
}
184-
}, 200);
185-
}
186-
187-
// Save filter configuration to localStorage
188-
private saveFilterConfig() {
189-
if (this.saveFilterConfigTimer) clearTimeout(this.saveFilterConfigTimer);
190-
this.saveFilterConfigTimer = setTimeout(() => {
191-
try {
192-
localStorage.setItem("filterConfig", JSON.stringify(this.filterConfig));
193-
} catch (error) {
194-
console.warn("Failed to save filter config to localStorage:", error);
195-
}
196-
}, 200);
197-
}
198-
199-
// Save pagination configuration to localStorage
200-
private savePaginationConfig() {
201-
if (this.savePaginationConfigTimer)
202-
clearTimeout(this.savePaginationConfigTimer);
203-
this.savePaginationConfigTimer = setTimeout(() => {
204-
try {
205-
localStorage.setItem(
206-
"paginationConfig",
207-
JSON.stringify(this.paginationConfig)
208-
);
209-
} catch (error) {
210-
console.warn(
211-
"Failed to save pagination config to localStorage:",
212-
error
213-
);
214-
}
215-
}, 200);
216-
}
217-
218-
// Save sort configuration to localStorage
219-
private saveSortConfig() {
220-
if (this.saveFilterConfigTimer) clearTimeout(this.saveFilterConfigTimer);
221-
this.saveFilterConfigTimer = setTimeout(() => {
222-
try {
223-
localStorage.setItem("sortConfig", JSON.stringify(this.sortConfig));
224-
} catch (error) {
225-
console.warn("Failed to save sort config to localStorage:", error);
147+
console.warn("Failed to save global config to localStorage:", error);
226148
}
227149
}, 200);
228150
}
229151

230152
// Update pivot configuration and save to localStorage
231153
updatePivotConfig(updates: Partial<PivotConfig>) {
232-
Object.assign(this.pivotConfig, updates);
233-
this.savePivotConfig();
154+
Object.assign(this.globalConfig.pivotConfig, updates);
155+
this.saveGlobalConfig();
234156
}
235157

236158
// Update filter configuration and save to localStorage
237159
updateFilterConfig(filters: FilterGroup[]) {
238-
this.filterConfig = filters;
239-
this.saveFilterConfig();
160+
this.globalConfig.filterConfig = filters;
161+
this.saveGlobalConfig();
240162

241163
// Debounce application of filters to avoid re-filtering on every keystroke
242164
if (this.applyFilterTimer) clearTimeout(this.applyFilterTimer);
@@ -247,68 +169,68 @@ export class GlobalState {
247169

248170
// Update pagination configuration and save to localStorage
249171
updatePaginationConfig(updates: Partial<PaginationConfig>) {
250-
Object.assign(this.paginationConfig, updates);
251-
this.savePaginationConfig();
172+
Object.assign(this.globalConfig.paginationConfig, updates);
173+
this.saveGlobalConfig();
252174
}
253175

254176
// Update sort configuration and save to localStorage
255177
updateSortConfig(updates: Partial<SortConfig>) {
256-
Object.assign(this.sortConfig, updates);
178+
Object.assign(this.globalConfig.sortConfig, updates);
257179
// Reset to first page when sorting changes
258-
this.paginationConfig.currentPage = 1;
259-
this.saveSortConfig();
180+
this.globalConfig.paginationConfig.currentPage = 1;
181+
this.saveGlobalConfig();
260182
}
261183

262184
// Handle sort field click - toggle direction if same field, set to asc if new field
263185
handleSortFieldClick(field: string) {
264186
if (this.sortConfig.sortField === field) {
265187
// Toggle direction for same field
266-
this.sortConfig.sortDirection =
188+
this.globalConfig.sortConfig.sortDirection =
267189
this.sortConfig.sortDirection === "asc" ? "desc" : "asc";
268190
} else {
269191
// New field, set to ascending
270-
this.sortConfig.sortField = field;
271-
this.sortConfig.sortDirection = "asc";
192+
this.globalConfig.sortConfig.sortField = field;
193+
this.globalConfig.sortConfig.sortDirection = "asc";
272194
}
273-
this.saveSortConfig();
195+
this.saveGlobalConfig();
274196
}
275197

276198
// Reset pivot configuration to defaults
277199
resetPivotConfig() {
278-
this.pivotConfig = { ...DEFAULT_PIVOT_CONFIG };
279-
this.savePivotConfig();
200+
this.globalConfig.pivotConfig = { ...DEFAULT_PIVOT_CONFIG };
201+
this.saveGlobalConfig();
280202
}
281203

282204
// Reset filter configuration to defaults
283205
resetFilterConfig() {
284-
this.filterConfig = [...DEFAULT_FILTER_CONFIG];
206+
this.globalConfig.filterConfig = [...DEFAULT_FILTER_CONFIG];
285207
this.appliedFilterConfig = [...DEFAULT_FILTER_CONFIG];
286-
this.saveFilterConfig();
208+
this.saveGlobalConfig();
287209
}
288210

289211
// Reset pagination configuration to defaults
290212
resetPaginationConfig() {
291-
this.paginationConfig = { ...DEFAULT_PAGINATION_CONFIG };
292-
this.savePaginationConfig();
213+
this.globalConfig.paginationConfig = { ...DEFAULT_PAGINATION_CONFIG };
214+
this.saveGlobalConfig();
293215
}
294216

295217
// Reset sort configuration to defaults
296218
resetSortConfig() {
297-
this.sortConfig = { ...DEFAULT_SORT_CONFIG };
298-
this.saveSortConfig();
219+
this.globalConfig.sortConfig = { ...DEFAULT_SORT_CONFIG };
220+
this.saveGlobalConfig();
299221
}
300222

301223
// Set current page
302224
setCurrentPage(page: number) {
303-
this.paginationConfig.currentPage = page;
304-
this.savePaginationConfig();
225+
this.globalConfig.paginationConfig.currentPage = page;
226+
this.saveGlobalConfig();
305227
}
306228

307229
// Set page size
308230
setPageSize(size: number) {
309-
this.paginationConfig.pageSize = size;
310-
this.paginationConfig.currentPage = 1; // Reset to first page when changing page size
311-
this.savePaginationConfig();
231+
this.globalConfig.paginationConfig.pageSize = size;
232+
this.globalConfig.paginationConfig.currentPage = 1; // Reset to first page when changing page size
233+
this.saveGlobalConfig();
312234
}
313235

314236
// Set loading state
@@ -341,10 +263,10 @@ export class GlobalState {
341263

342264
runInAction(() => {
343265
// Reset to first page when dataset changes
344-
this.paginationConfig.currentPage = 1;
266+
this.globalConfig.paginationConfig.currentPage = 1;
345267
this.isLoading = false;
346268
});
347-
this.savePaginationConfig();
269+
this.saveGlobalConfig();
348270
}
349271

350272
toggleRowExpansion(rolloutId?: string) {
@@ -371,15 +293,6 @@ export class GlobalState {
371293
});
372294
}
373295

374-
get globalConfig(): GlobalConfig {
375-
return {
376-
pivotConfig: this.pivotConfig,
377-
filterConfig: this.filterConfig,
378-
paginationConfig: this.paginationConfig,
379-
sortConfig: this.sortConfig,
380-
};
381-
}
382-
383296
// Computed values following MobX best practices
384297
get sortedIds() {
385298
const ids = Object.keys(this.dataset);

0 commit comments

Comments
 (0)