Skip to content

Commit bde7b07

Browse files
authored
[cueweb] Fix autoload and caching (#1623)
- Fixed autoload user jobs by not including finished user jobs - Added a reinitialization of data table state values on re-mount using local storage values
1 parent a0b9313 commit bde7b07

File tree

2 files changed

+36
-26
lines changed

2 files changed

+36
-26
lines changed

cueweb/app/jobs/data-table.tsx

+35-25
Original file line numberDiff line numberDiff line change
@@ -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) => {

cueweb/app/utils/get_utils.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -45,7 +45,7 @@ export async function getLayers(body: string): Promise<Layer[]> {
4545

4646
// Fetch jobs for a specific user, including finished jobs
4747
export async function getJobsForUser(user: string): Promise<Job[]> {
48-
const body = { r: { include_finished: true, users: [`${user}`] } };
48+
const body = { r: { include_finished: false, users: [`${user}`] } };
4949
return await getJobs(JSON.stringify(body));
5050
}
5151

0 commit comments

Comments
 (0)