Skip to content

Commit 8c939c3

Browse files
committed
Merge branch 'validate-state' of lkiesow/opencast-admin-interface into r/18.x
Pull request #1418 Validate and reset corrupted table filter states
2 parents 37548cd + 9c6dac0 commit 8c939c3

3 files changed

Lines changed: 48 additions & 2 deletions

File tree

src/App.tsx

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,9 +16,13 @@ import About from "./components/About";
1616
import { useAppDispatch } from "./store";
1717
import { fetchOcVersion, fetchUserInfo } from "./slices/userInfoSlice";
1818
import { subscribeToAuthEvents } from "./utils/broadcastSync";
19+
import { useTableFilterStateValidation } from "./hooks/useTableFilterStateValidation";
1920

2021
function App() {
2122
const dispatch = useAppDispatch();
23+
24+
useTableFilterStateValidation();
25+
2226
useEffect(() => {
2327
// Load information about current user on mount
2428
dispatch(fetchUserInfo());
@@ -33,7 +37,7 @@ function App() {
3337
dispatch(fetchUserInfo());
3438
});
3539

36-
// eslint-disable-next-line react-hooks/exhaustive-deps
40+
// eslint-disable-next-line react-hooks/exhaustive-deps
3741
}, []);
3842

3943
return (
Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
import { useEffect } from "react";
2+
import { useAppDispatch, useAppSelector } from "../store";
3+
import { resetCorruptedState } from "../slices/tableFilterSlice";
4+
5+
/**
6+
* Custom hook to validate and fix corrupted table filter state in localStorage.
7+
* This hook should be used in components that rely on table filter state to ensure
8+
* the state is valid before using it.
9+
*/
10+
export const useTableFilterStateValidation = () => {
11+
const dispatch = useAppDispatch();
12+
const tableFilters = useAppSelector(state => state.tableFilters);
13+
14+
useEffect(() => {
15+
// Check for corrupted state and dispatch reset action if needed
16+
const hasCorruption =
17+
!Array.isArray(tableFilters.data) ||
18+
!Array.isArray(tableFilters.textFilter) ||
19+
!Array.isArray(tableFilters.stats);
20+
21+
if (hasCorruption) {
22+
console.warn("Detected corrupted table filter state, resetting to defaults");
23+
dispatch(resetCorruptedState());
24+
}
25+
}, [dispatch, tableFilters.data, tableFilters.textFilter, tableFilters.stats]);
26+
};

src/slices/tableFilterSlice.ts

Lines changed: 17 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -132,7 +132,7 @@ export const fetchStats = createAppAsyncThunk("tableFilters/fetchStats", async (
132132
}[],
133133
description: string,
134134
order: number,
135-
}
135+
}
136136
// fetch information about possible status an event can have
137137
const data = await axios.get<FetchStats>("/admin-ng/resources/STATS.json");
138138
const response = data.data;
@@ -380,6 +380,21 @@ const tableFilterSlice = createSlice({
380380
removeSecondFilter(state) {
381381
state.secondFilter = "";
382382
},
383+
resetCorruptedState(state) {
384+
// Reset corrupted localStorage state to initial values
385+
if (!Array.isArray(state.data)) {
386+
console.warn("Resetting corrupted tableFilters.data to empty array");
387+
state.data = [];
388+
}
389+
if (!Array.isArray(state.textFilter)) {
390+
console.warn("Resetting corrupted tableFilters.textFilter to empty array");
391+
state.textFilter = [];
392+
}
393+
if (!Array.isArray(state.stats)) {
394+
console.warn("Resetting corrupted tableFilters.stats to empty array");
395+
state.stats = [];
396+
}
397+
},
383398
},
384399
extraReducers: builder => {
385400
builder
@@ -427,6 +442,7 @@ export const {
427442
removeSelectedFilter,
428443
editSecondFilter,
429444
removeSecondFilter,
445+
resetCorruptedState,
430446
} = tableFilterSlice.actions;
431447

432448
// Export the slice reducer as the default export

0 commit comments

Comments
 (0)