From 4671813725b898625ff04cd712058f81717e8f2d Mon Sep 17 00:00:00 2001 From: Ashesh3 <3626859+Ashesh3@users.noreply.github.com> Date: Wed, 31 Jan 2024 13:36:44 +0530 Subject: [PATCH] Set Form Drafts to expire after 24 hours --- src/Utils/AutoSave.tsx | 17 +++++++++++++++++ 1 file changed, 17 insertions(+) diff --git a/src/Utils/AutoSave.tsx b/src/Utils/AutoSave.tsx index 5cdf118650f..0c929f371e5 100644 --- a/src/Utils/AutoSave.tsx +++ b/src/Utils/AutoSave.tsx @@ -104,6 +104,23 @@ export function DraftSection(props: { }; }, []); + // Remove drafts older than 24 hours + useEffect(() => { + const keys = Object.keys(localStorage); + const now = Date.now(); + keys.forEach((key) => { + if (key.startsWith("form_draft_")) { + const savedDrafts = localStorage.getItem(key); + const drafts = savedDrafts ? JSON.parse(savedDrafts) : []; + const newDrafts = drafts.filter( + (draft: Draft) => now - draft.timestamp < 24 * 60 * 60 * 1000 + ); + localStorage.setItem(key, JSON.stringify(newDrafts)); + if (newDrafts.length === 0) localStorage.removeItem(key); + } + }); + }, []); + return ( <> {drafts && (