@@ -122,22 +122,24 @@ const initialColumnVisibility = {
122122 maxRss : false ,
123123}
124124
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+ }
141143} ;
142144
143145// Reducer function
@@ -174,10 +176,10 @@ function reducer(state: State, action: Action): State {
174176 case "RESET_COLUMN_VISIBILITY" :
175177 return {
176178 ...state ,
177- columnVisibility : initialState . columnVisibility ,
179+ columnVisibility : initialColumnVisibility ,
178180 } ;
179181 case "RESET_STATE" :
180- return initialState ;
182+ return getInitialState ( ) ;
181183 default :
182184 return state ;
183185 }
@@ -215,7 +217,11 @@ export function DataTable({ columns, username }: DataTableProps) {
215217 const { theme, setTheme } = useTheme ( ) ;
216218
217219 // 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+ } , [ ] ) ;
219225
220226 useEffect ( ( ) => {
221227 addUsersJobs ( ) ;
@@ -380,17 +386,16 @@ export function DataTable({ columns, username }: DataTableProps) {
380386 // Filter out any of the old data in the data table which no longer exists (has been finished for over 48 hours)
381387 updatedTableDataUnfiltered = updatedTableDataUnfiltered . filter ( ( oldJob : Job ) => newData . some ( ( newJob : Job ) => oldJob . id === newJob . id ) ) ;
382388 updatedTableData = updatedTableData . filter ( ( oldJob : Job ) => newData . some ( ( newJob : Job ) => oldJob . id === newJob . id ) ) ;
383-
384389 // Update table data as both a variable and in local storage
385390 dispatch ( { type : "SET_TABLE_DATA_UNFILTERED" , payload : updatedTableDataUnfiltered } ) ;
386391 dispatch ( { type : "SET_TABLE_DATA" , payload : updatedTableData } ) ;
387392 }
388393 } ;
389394
390395 // Trigger table updates every 5000ms
391- interval = setInterval ( ( ) => {
396+ interval = setInterval ( async ( ) => {
392397 updateData ( ) ;
393- addUsersJobs ( ) ;
398+ await addUsersJobs ( ) ;
394399 } , 5000 ) ;
395400 } catch ( error ) {
396401 handleError ( error , "Error updating table" ) ;
@@ -633,9 +638,14 @@ export function DataTable({ columns, username }: DataTableProps) {
633638 const jobsToAdd = userJobs . filter ( userJob => {
634639 return ! state . tableData . some ( existingJob => existingJob . name === userJob . name )
635640 } ) ;
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+ }
639649 } ;
640650
641651 const handleStateFiltering = ( stateFilter : string ) => {
0 commit comments