From 162ab55445d438160b9ebfa59c391606e68b6c7b Mon Sep 17 00:00:00 2001 From: Kieran Date: Wed, 1 Feb 2023 20:34:23 +0000 Subject: [PATCH] cleanup old data at startup --- src/Pages/Layout.tsx | 23 +++++++++++++++++++---- 1 file changed, 19 insertions(+), 4 deletions(-) diff --git a/src/Pages/Layout.tsx b/src/Pages/Layout.tsx index 40a54302..a4bd1c24 100644 --- a/src/Pages/Layout.tsx +++ b/src/Pages/Layout.tsx @@ -18,6 +18,7 @@ import { SearchRelays } from 'Const'; import useEventPublisher from "Feed/EventPublisher"; import useModeration from "Hooks/useModeration"; import { IndexedUDB, useDb } from "State/Users/Db"; +import { db } from "Db"; export default function Layout() { @@ -81,10 +82,24 @@ export default function Layout() { useEffect(() => { // check DB support then init IndexedUDB.isAvailable() - .then(a => { - const db = a ? "indexdDb" : "redux"; - console.debug(`Using db: ${db}`); - dispatch(init(db)); + .then(async a => { + const dbType = a ? "indexdDb" : "redux"; + + // cleanup on load + if (dbType === "indexdDb") { + await db.feeds.clear(); + const now = Math.floor(new Date().getTime() / 1000); + + const cleanupEvents = await db.events + .where("created_at") + .above(now - (60 * 60)) + .primaryKeys(); + console.debug(`Cleanup ${cleanupEvents.length} events`); + await db.events.bulkDelete(cleanupEvents) + } + + console.debug(`Using db: ${dbType}`); + dispatch(init(dbType)); }) }, []);