From d996fb031e6f0fc53737e62e1841617ffcbf6566 Mon Sep 17 00:00:00 2001 From: marlon-erler Date: Sat, 20 Jul 2024 04:36:15 +0200 Subject: [PATCH] add removeRecursive method --- src/Model/storageModel.ts | 28 ++++++++++++++++++++++++++-- 1 file changed, 26 insertions(+), 2 deletions(-) diff --git a/src/Model/storageModel.ts b/src/Model/storageModel.ts index fdf1d5c5..d194acb2 100644 --- a/src/Model/storageModel.ts +++ b/src/Model/storageModel.ts @@ -21,10 +21,33 @@ export default class StorageModel { return localStorage.getItem(key); }; - remove = (pathComponents: string[]): void => { + remove = ( + pathComponents: string[], + shouldInitialize: boolean = true + ): void => { const key: string = StorageModel.pathComponentsToKey(...pathComponents); localStorage.removeItem(key); - this.initializeTree(); + + if (shouldInitialize) { + this.initializeTree(); + } + }; + + removeRecursive = (pathComponentsOfEntityToDelete: string[]): void => { + a: for (const key of Object.keys(localStorage)) { + const pathComponentsOfCurrentEntity: string[] = + StorageModel.keyToPathComponents(key); + + for (let i = 0; i < pathComponentsOfEntityToDelete.length; i++) { + if (!pathComponentsOfCurrentEntity[i]) continue a; + if ( + pathComponentsOfCurrentEntity[i] != pathComponentsOfEntityToDelete[i] + ) + continue a; + } + + this.remove(pathComponentsOfCurrentEntity); + } }; list = (pathComponents: string[]): string[] => { @@ -113,6 +136,7 @@ export const storageKeys = { previousObjectFilters: [DATA_VERSION, "history", "object-filters"], // chat etc + chats: [DATA_VERSION, "chat"], chatInfo: (id: string) => [DATA_VERSION, "chat", id, "info"], chatMessages: (id: string) => [DATA_VERSION, "chat", id, "messages"], chatObjects: (id: string) => [DATA_VERSION, "chat", id, "objects"],