@@ -122,22 +122,24 @@ const initialColumnVisibility = {
122
122
maxRss : false ,
123
123
}
124
124
125
- // Initial state
126
- const initialState : State = {
127
- autoloadMine : getItemFromLocalStorage ( "autoloadMine" , "true" ) ,
128
- tableData : getItemFromLocalStorage ( "tableData" , "[]" ) ,
129
- tableDataUnfiltered : getItemFromLocalStorage ( "tableDataUnfiltered" , "[]" ) ,
130
- sorting : getItemFromLocalStorage ( "sorting" , "[]" ) ,
131
- columnFilters : getItemFromLocalStorage ( "columnFilters" , "[]" ) ,
132
- stateSelectValue : getItemFromLocalStorage ( "stateSelectValue" , JSON . stringify ( "All States" ) ) ,
133
- jobSearchResults : [ ] ,
134
- filteredJobSearchResults : [ ] ,
135
- searchQuery : "" ,
136
- apiQuery : "" ,
137
- rowSelection : { } ,
138
- columnVisibility : getItemFromLocalStorage ( "columnVisibility" , JSON . stringify ( initialColumnVisibility ) ) ,
139
- error : null ,
140
- username : UNKNOWN_USER ,
125
+ // The initial state of the data table on remount using local storage to preserve states
126
+ function getInitialState ( ) : State {
127
+ return {
128
+ autoloadMine : getItemFromLocalStorage ( "autoloadMine" , "true" ) ,
129
+ tableData : getItemFromLocalStorage ( "tableData" , "[]" ) ,
130
+ tableDataUnfiltered : getItemFromLocalStorage ( "tableDataUnfiltered" , "[]" ) ,
131
+ sorting : getItemFromLocalStorage ( "sorting" , "[]" ) ,
132
+ columnFilters : getItemFromLocalStorage ( "columnFilters" , "[]" ) ,
133
+ stateSelectValue : getItemFromLocalStorage ( "stateSelectValue" , JSON . stringify ( "All States" ) ) ,
134
+ jobSearchResults : [ ] ,
135
+ filteredJobSearchResults : [ ] ,
136
+ searchQuery : "" ,
137
+ apiQuery : "" ,
138
+ rowSelection : { } ,
139
+ columnVisibility : getItemFromLocalStorage ( "columnVisibility" , JSON . stringify ( initialColumnVisibility ) ) ,
140
+ error : null ,
141
+ username : UNKNOWN_USER ,
142
+ }
141
143
} ;
142
144
143
145
// Reducer function
@@ -174,10 +176,10 @@ function reducer(state: State, action: Action): State {
174
176
case "RESET_COLUMN_VISIBILITY" :
175
177
return {
176
178
...state ,
177
- columnVisibility : initialState . columnVisibility ,
179
+ columnVisibility : initialColumnVisibility ,
178
180
} ;
179
181
case "RESET_STATE" :
180
- return initialState ;
182
+ return getInitialState ( ) ;
181
183
default :
182
184
return state ;
183
185
}
@@ -215,7 +217,11 @@ export function DataTable({ columns, username }: DataTableProps) {
215
217
const { theme, setTheme } = useTheme ( ) ;
216
218
217
219
// useReducer hook to manage state
218
- const [ state , dispatch ] = useReducer ( reducer , initialState ) ;
220
+ const [ state , dispatch ] = useReducer ( reducer , getInitialState ( ) ) ;
221
+
222
+ useEffect ( ( ) => {
223
+ dispatch ( { type : "RESET_STATE" } ) ;
224
+ } , [ ] ) ;
219
225
220
226
useEffect ( ( ) => {
221
227
addUsersJobs ( ) ;
@@ -380,17 +386,16 @@ export function DataTable({ columns, username }: DataTableProps) {
380
386
// Filter out any of the old data in the data table which no longer exists (has been finished for over 48 hours)
381
387
updatedTableDataUnfiltered = updatedTableDataUnfiltered . filter ( ( oldJob : Job ) => newData . some ( ( newJob : Job ) => oldJob . id === newJob . id ) ) ;
382
388
updatedTableData = updatedTableData . filter ( ( oldJob : Job ) => newData . some ( ( newJob : Job ) => oldJob . id === newJob . id ) ) ;
383
-
384
389
// Update table data as both a variable and in local storage
385
390
dispatch ( { type : "SET_TABLE_DATA_UNFILTERED" , payload : updatedTableDataUnfiltered } ) ;
386
391
dispatch ( { type : "SET_TABLE_DATA" , payload : updatedTableData } ) ;
387
392
}
388
393
} ;
389
394
390
395
// Trigger table updates every 5000ms
391
- interval = setInterval ( ( ) => {
396
+ interval = setInterval ( async ( ) => {
392
397
updateData ( ) ;
393
- addUsersJobs ( ) ;
398
+ await addUsersJobs ( ) ;
394
399
} , 5000 ) ;
395
400
} catch ( error ) {
396
401
handleError ( error , "Error updating table" ) ;
@@ -633,9 +638,14 @@ export function DataTable({ columns, username }: DataTableProps) {
633
638
const jobsToAdd = userJobs . filter ( userJob => {
634
639
return ! state . tableData . some ( existingJob => existingJob . name === userJob . name )
635
640
} ) ;
636
-
637
- dispatch ( { type : "SET_TABLE_DATA" , payload : [ ...state . tableDataUnfiltered , ...jobsToAddUnfiltered ] } ) ;
638
- dispatch ( { type : "SET_TABLE_DATA_UNFILTERED" , payload : [ ...state . tableData , ...jobsToAdd ] } ) ;
641
+
642
+ if ( jobsToAddUnfiltered . length > 0 ) {
643
+ dispatch ( { type : "SET_TABLE_DATA" , payload : [ ...state . tableDataUnfiltered , ...jobsToAddUnfiltered ] } ) ;
644
+ }
645
+
646
+ if ( jobsToAdd . length > 0 ) {
647
+ dispatch ( { type : "SET_TABLE_DATA_UNFILTERED" , payload : [ ...state . tableData , ...jobsToAdd ] } ) ;
648
+ }
639
649
} ;
640
650
641
651
const handleStateFiltering = ( stateFilter : string ) => {
0 commit comments