Skip to content

Commit

Permalink
Set Form Drafts to expire after 24 hours (#7125)
Browse files Browse the repository at this point in the history
  • Loading branch information
Ashesh3 authored Feb 14, 2024
1 parent ba0b192 commit bba3b5c
Showing 1 changed file with 17 additions and 0 deletions.
17 changes: 17 additions & 0 deletions src/Utils/AutoSave.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -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 && (
Expand Down

0 comments on commit bba3b5c

Please sign in to comment.