add chatListModel

This commit is contained in:
marlon-erler
2024-07-20 04:43:16 +02:00
parent a634aa7768
commit 92f0d3c662
3 changed files with 89 additions and 15 deletions
+17 -1
View File
@@ -333,9 +333,23 @@
const key = _StorageModel.pathComponentsToKey(...pathComponents); const key = _StorageModel.pathComponentsToKey(...pathComponents);
return localStorage.getItem(key); return localStorage.getItem(key);
}; };
remove = (pathComponents) => { remove = (pathComponents, shouldInitialize = true) => {
const key = _StorageModel.pathComponentsToKey(...pathComponents); const key = _StorageModel.pathComponentsToKey(...pathComponents);
localStorage.removeItem(key); localStorage.removeItem(key);
if (shouldInitialize == true) {
this.initializeTree();
}
};
removeRecursive = (pathComponentsOfEntityToDelete) => {
a: for (const key of Object.keys(localStorage)) {
const pathComponentsOfCurrentEntity = _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, false);
}
this.initializeTree(); this.initializeTree();
}; };
list = (pathComponents) => { list = (pathComponents) => {
@@ -363,6 +377,7 @@
} }
// utility // utility
initializeTree = () => { initializeTree = () => {
console.log("initializing tree");
this.storageEntryTree = {}; this.storageEntryTree = {};
for (const key of Object.keys(localStorage)) { for (const key of Object.keys(localStorage)) {
const components = _StorageModel.keyToPathComponents(key); const components = _StorageModel.keyToPathComponents(key);
@@ -405,6 +420,7 @@
previousObjectStatuses: [DATA_VERSION, "history", "object-statuses"], previousObjectStatuses: [DATA_VERSION, "history", "object-statuses"],
previousObjectFilters: [DATA_VERSION, "history", "object-filters"], previousObjectFilters: [DATA_VERSION, "history", "object-filters"],
// chat etc // chat etc
chats: [DATA_VERSION, "chat"],
chatInfo: (id) => [DATA_VERSION, "chat", id, "info"], chatInfo: (id) => [DATA_VERSION, "chat", id, "info"],
chatMessages: (id) => [DATA_VERSION, "chat", id, "messages"], chatMessages: (id) => [DATA_VERSION, "chat", id, "messages"],
chatObjects: (id) => [DATA_VERSION, "chat", id, "objects"], chatObjects: (id) => [DATA_VERSION, "chat", id, "objects"],
+54
View File
@@ -0,0 +1,54 @@
import StorageModel, { storageKeys } from "./storageModel";
import { ChatModel } from "./chatModel";
import { v4 } from "uuid";
export default class ChatListModel {
storageModel: StorageModel;
// data
chatModels = new Map<string, ChatModel>();
// store & add
addChatModel(chatModel: ChatModel) {
this.chatModels.set(chatModel.id, chatModel);
}
createChat = (primaryChannel: string): void => {
const id: string = v4();
const chatModel = new ChatModel(this.storageModel, id);
chatModel.setPrimaryChannel(primaryChannel);
this.addChatModel(chatModel);
};
deleteChat = (chatId: string): void => {
const chat = this.chatModels.get(chatId);
if (!chat) return;
chat.remove();
this.untrackChat(chatId);
};
untrackChat = (chatId: string): void => {
const chat = this.chatModels.get(chatId);
if (!chat) return;
this.chatModels.delete(chatId);
};
// restore
loadChats = (): void => {
const chatDir = storageKeys.chats;
for (const chatId of chatDir) {
const chatModel = new ChatModel(this.storageModel, chatId);
this.addChatModel(chatModel);
}
};
// init
constructor(storageModel: StorageModel) {
this.storageModel = storageModel;
}
}
+18 -14
View File
@@ -71,6 +71,13 @@ export class ChatModel {
this.storageModel.storeStringifiable(objectPath, object); this.storageModel.storeStringifiable(objectPath, object);
} }
// remove
remove = () => {
this.unloadData();
const dirPath = [...storageKeys.chats, this.id];
this.storageModel.removeRecursive(dirPath);
}
// restore // restore
restoreInfo(): void { restoreInfo(): void {
const info: any = this.storageModel.restoreStringifiable(this.infoPath); const info: any = this.storageModel.restoreStringifiable(this.infoPath);
@@ -104,30 +111,27 @@ export class ChatModel {
} }
} }
// memory
loadData(): void {
this.restoreMessages();
this.restoreObjects();
}
unloadData(): void {
this.messages.clear();
this.objects.clear();
}
// init // init
constructor(storageModel: StorageModel, chatId: string) { constructor(storageModel: StorageModel, chatId: string) {
this.id = chatId; this.id = chatId;
this.storageModel = storageModel; this.storageModel = storageModel;
this.restoreInfo(); this.restoreInfo();
this.restoreMessages();
this.restoreObjects();
} }
} }
// creation methods // creation methods
export function createChat(
storageModel: StorageModel,
primaryChannel: string
): ChatModel {
const id: string = v4();
const chatModel = new ChatModel(storageModel, id);
chatModel.setPrimaryChannel(primaryChannel);
return chatModel;
}
export function generateChatInfo(primaryChannel: string): ChatInfo { export function generateChatInfo(primaryChannel: string): ChatInfo {
return { return {
primaryChannel, primaryChannel,