Press n or j to go to the next uncovered block, b, p or k for the previous block.
| 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 | 370x 2477x 370x 370x 270x 100x 42x 42x 394x 366x 366x 2471x 361x 262x 132x 23182x 23182x 23182x 40122x 23182x 394x 254x 254x 5412x 254x 254x 232x 254x 140x 140x 3147x 140x 140x 138x | import { useSelector } from "react-redux";
import { api, PaginatedTaskListRead, TaskRead } from "@/api/apiStore.gen.ts";
import { useProjectId } from "@/hooks/project.ts";
import { useAppDispatch } from "@/store/hooks.ts";
import { ApiQueries, RootState } from "@/store/store";
function updateTaskInList(
task: TaskRead,
paginatedTasks: PaginatedTaskListRead,
updateOnly: boolean,
) {
const tasks = paginatedTasks?.results || ([] as TaskRead[]);
const taskIndex = tasks.findIndex((t) => t.id === task.id);
// If results is null, initialize it with the current tasks
Iif (paginatedTasks.results == null) paginatedTasks.results = tasks;
// Update the task in the list if it exists
if (taskIndex !== -1) {
tasks[taskIndex] = task;
} else if (!updateOnly) {
// If the task does not exist, add it to the list
tasks.unshift(task);
paginatedTasks.count += 1;
}
}
function findTaskPageInList(
taskId: number,
tasks: [string, ApiQueries[string]][],
): number {
for (const [cacheKey, value] of tasks) {
const paginatedTasks = value?.data as PaginatedTaskListRead | undefined;
if (paginatedTasks) {
const foundTask = paginatedTasks.results?.find((t) => t.id === taskId);
if (foundTask) {
return Number(value?.originalArgs?.page || 1);
}
}
}
return 1; // If not on any page, add to page 1
}
export function useUpdateTaskCache() {
const dispatch = useAppDispatch();
const flowsheetId = useProjectId();
const queries: ApiQueries = useSelector(
(state: RootState) => state.api.queries,
);
return (updatedTask: TaskRead) => {
// Update the standard task list cache if this task is not a child task
if (updatedTask.parent === null) {
const endpointName = "coreTasksList";
const allResults = Object.entries(queries).filter(([key]) =>
key.startsWith(endpointName),
);
const pageNumber = findTaskPageInList(updatedTask.id, allResults);
dispatch(
api.util.updateQueryData(
"coreTasksList",
{ page: pageNumber, flowsheet: flowsheetId },
(paginatedTasks) => {
updateTaskInList(updatedTask, paginatedTasks, false);
},
),
);
return;
}
const endpointName = "coreTasksChildrenList";
const allResults = Object.entries(queries).filter(([key]) =>
key.startsWith(endpointName),
);
const pageNumber = findTaskPageInList(updatedTask.id, allResults);
// Update the child task list cache
dispatch(
api.util.updateQueryData(
"coreTasksChildrenList",
{
id: updatedTask.parent as number,
page: pageNumber,
},
(paginatedTasks) => {
updateTaskInList(updatedTask, paginatedTasks, true);
},
),
);
};
}
|