diff --git a/src/api/TasksDoneTodayAPI/index.ts b/src/api/TasksDoneTodayAPI/index.ts index 594b4a0cd..d7d278104 100644 --- a/src/api/TasksDoneTodayAPI/index.ts +++ b/src/api/TasksDoneTodayAPI/index.ts @@ -33,3 +33,25 @@ export const deleteTaskDoneToday = async (id: string) => { export const deleteAllTasksDoneToday = async () => { await db.tasksDoneTodayCollection.clear(); }; + +export const checkAndCleanupDoneTodayCollection = async () => { + const tasks = await getAllTasksDoneToday(); + + if (tasks.length === 0) { + return; + } + + const firstTaskScheduledStart = new Date(tasks[0].scheduledStart); + const today = new Date(); + const isSameDay = (date1: Date, date2: Date): boolean => { + return ( + date1.getDate() === date2.getDate() && + date1.getMonth() === date2.getMonth() && + date1.getFullYear() === date2.getFullYear() + ); + }; + + if (!isSameDay(firstTaskScheduledStart, today)) { + await deleteAllTasksDoneToday(); + } +}; diff --git a/src/hooks/useApp.tsx b/src/hooks/useApp.tsx index 0a4f4ecda..42210b1a4 100644 --- a/src/hooks/useApp.tsx +++ b/src/hooks/useApp.tsx @@ -15,6 +15,7 @@ import { useSetRecoilState, useRecoilValue, useRecoilState } from "recoil"; import { scheduledHintCalls } from "@src/api/HintsAPI/ScheduledHintCall"; import { LocalStorageKeys } from "@src/constants/localStorageKeys"; import { checkAndCleanupTrash } from "@src/api/TrashAPI"; +import { checkAndCleanupDoneTodayCollection } from "@src/api/TasksDoneTodayAPI"; const langFromStorage = localStorage.getItem(LocalStorageKeys.LANGUAGE)?.slice(1, -1); const exceptionRoutes = ["/", "/invest", "/feedback", "/donate"]; @@ -147,6 +148,7 @@ function useApp() { useEffect(() => { checkAndCleanupTrash(); + checkAndCleanupDoneTodayCollection(); }, []); useEffect(() => {