From 4302cdf0ea2842b4339c330f61d99322baaf3392 Mon Sep 17 00:00:00 2001 From: Abdullah Atta Date: Thu, 9 Nov 2023 13:05:02 +0500 Subject: [PATCH] web: use new virtualized grouping for trash --- .../stores/{trash-store.js => trash-store.ts} | 55 +++++++------------ 1 file changed, 21 insertions(+), 34 deletions(-) rename apps/web/src/stores/{trash-store.js => trash-store.ts} (51%) diff --git a/apps/web/src/stores/trash-store.js b/apps/web/src/stores/trash-store.ts similarity index 51% rename from apps/web/src/stores/trash-store.js rename to apps/web/src/stores/trash-store.ts index 92cb3b2f9..608a315de 100644 --- a/apps/web/src/stores/trash-store.js +++ b/apps/web/src/stores/trash-store.ts @@ -21,49 +21,36 @@ import { db } from "../common/db"; import createStore from "../common/store"; import BaseStore from "./index"; import { store as appStore } from "./app-store"; -import { store as notestore } from "./note-store"; -import { groupArray } from "@notesnook/core/dist/utils/grouping"; +import { store as noteStore } from "./note-store"; +import { store as notebookStore } from "./notebook-store"; +import { TrashItem, VirtualizedGrouping } from "@notesnook/core"; -/** - * @extends {BaseStore} - */ -class TrashStore extends BaseStore { - trash = []; +class TrashStore extends BaseStore { + trash: VirtualizedGrouping | undefined = undefined; - refresh = () => { - this.set( - (state) => - (state.trash = groupArray( - db.trash.all, - db.settings.getGroupOptions("trash") - )) + refresh = async () => { + const grouping = await db.trash.grouped( + db.settings.getGroupOptions("trash") ); + this.set({ trash: grouping }); }; - delete = (ids, commit = false) => { - if (!commit) { - return this.set((state) => { - for (let id of ids) { - const index = state.trash.findIndex((item) => item.id === id); - if (index > -1) state.trash.splice(index, 1); - } - }); - } - return db.trash.delete(...ids); + delete = async (...ids: string[]) => { + await db.trash.delete(...ids); + await this.get().refresh(); }; - restore = (ids) => { - return db.trash.restore(...ids).then(() => { - this.refresh(); - appStore.refreshNavItems(); - notestore.refresh(); - }); + restore = async (...ids: string[]) => { + await db.trash.restore(...ids); + await this.get().refresh(); + await appStore.refreshNavItems(); + await noteStore.refresh(); + await notebookStore.refresh(); }; - clear = () => { - return db.trash.clear().then(() => { - this.set((state) => (state.trash = [])); - }); + clear = async () => { + await db.trash.clear(); + await this.get().refresh(); }; }