Files
CT/src/ViewModel/chatListViewModel.tsx
T

77 lines
2.1 KiB
TypeScript
Raw Normal View History

2024-07-20 05:10:50 +02:00
import * as React from "bloatless-react";
import ChatListModel from "../Model/chatListModel";
import { ChatModel } from "../Model/chatModel";
2024-07-20 14:16:26 +02:00
import ChatViewModel from "./chatViewModel";
2024-07-20 05:10:50 +02:00
import StorageModel from "../Model/storageModel";
export default class ChatListViewModel {
2024-07-20 14:16:26 +02:00
storageModel: StorageModel;
2024-07-20 05:10:50 +02:00
chatListModel: ChatListModel;
// state
newChatPrimaryChannel: React.State<string> = new React.State("");
2024-07-20 14:16:26 +02:00
chatViewModels: React.ListState<ChatViewModel> = new React.ListState();
selectedChat: React.State<ChatViewModel | undefined> = new React.State<
2024-07-20 16:49:01 +02:00
ChatViewModel | undefined
2024-07-20 14:16:26 +02:00
>(undefined);
2024-07-20 05:10:50 +02:00
// guards
cannotCreateChat = React.createProxyState(
[this.newChatPrimaryChannel],
() => this.newChatPrimaryChannel.value == ""
);
2024-07-20 16:49:01 +02:00
// sorting
get sortedPrimaryChannels(): string[] {
return this.chatListModel.sortedPrimaryChannels;
}
getIndexOfChat(chat: ChatViewModel): number {
return this.sortedPrimaryChannels.indexOf(chat.primaryChannel.value);
}
2024-07-20 05:10:50 +02:00
// methods
createChat = (): void => {
const chatModel: ChatModel = this.chatListModel.createChat(
this.newChatPrimaryChannel.value
);
this.newChatPrimaryChannel.value = "";
2024-07-20 14:16:26 +02:00
const chatViewModel: ChatViewModel = new ChatViewModel(this, chatModel);
this.chatViewModels.add(chatViewModel);
};
untrackChat = (chatViewModel: ChatViewModel): void => {
this.chatListModel.untrackChat(chatViewModel.chatModel);
this.chatViewModels.remove(chatViewModel);
2024-07-20 05:10:50 +02:00
};
2024-07-20 14:16:26 +02:00
openChat = (chatViewModel: ChatViewModel): void => {
this.selectedChat.value = chatViewModel;
};
closeChat = (): void => {
this.selectedChat.value = undefined;
2024-07-20 05:10:50 +02:00
};
// restore
restoreChats = (): void => {
2024-07-20 14:16:26 +02:00
for (const chatModel of this.chatListModel.chatModels.values()) {
const chatViewModel = new ChatViewModel(this, chatModel);
this.chatViewModels.add(chatViewModel);
2024-07-20 05:10:50 +02:00
}
};
// init
constructor(storageModel: StorageModel) {
2024-07-20 14:16:26 +02:00
this.storageModel = storageModel;
2024-07-20 05:10:50 +02:00
const chatListModel = new ChatListModel(storageModel);
this.chatListModel = chatListModel;
this.restoreChats();
}
}