From bba3b5c7d2172ff01cc481d8f75d18df6403f12d Mon Sep 17 00:00:00 2001 From: Ashesh <3626859+Ashesh3@users.noreply.github.com> Date: Wed, 14 Feb 2024 10:18:25 +0530 Subject: [PATCH] Set Form Drafts to expire after 24 hours (#7125) --- 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 && (