support creating new chats, list chats

This commit is contained in:
marlon-erler
2024-07-20 05:10:50 +02:00
parent 92f0d3c662
commit d9f0f400f8
8 changed files with 520 additions and 168 deletions
+48
View File
@@ -0,0 +1,48 @@
import * as React from "bloatless-react";
import ChatListModel from "../Model/chatListModel";
import { ChatModel } from "../Model/chatModel";
import StorageModel from "../Model/storageModel";
export default class ChatListViewModel {
chatListModel: ChatListModel;
// state
newChatPrimaryChannel: React.State<string> = new React.State("");
chatModels: React.ListState<ChatModel> = new React.ListState();
// guards
cannotCreateChat = React.createProxyState(
[this.newChatPrimaryChannel],
() => this.newChatPrimaryChannel.value == ""
);
// methods
createChat = (): void => {
const chatModel: ChatModel = this.chatListModel.createChat(
this.newChatPrimaryChannel.value
);
this.newChatPrimaryChannel.value = "";
this.chatModels.add(chatModel);
};
deleteChat = (chat: ChatModel): void => {
this.chatListModel.deleteChat(chat);
this.chatModels.remove(chat);
};
// restore
restoreChats = (): void => {
for (const chat of this.chatListModel.chatModels.values()) {
this.chatModels.add(chat);
}
};
// init
constructor(storageModel: StorageModel) {
const chatListModel = new ChatListModel(storageModel);
this.chatListModel = chatListModel;
this.restoreChats();
}
}