Files
CT/src/ViewModel/chatViewModel.tsx
T

128 lines
3.6 KiB
TypeScript
Raw Normal View History

2024-07-20 04:17:10 +02:00
import * as React from "bloatless-react";
2024-07-20 16:34:16 +02:00
import StorageModel, { storageKeys } from "../Model/storageModel";
2024-07-20 14:16:26 +02:00
import ChatListViewModel from "./chatListViewModel";
2024-07-20 04:17:10 +02:00
import { ChatModel } from "../Model/chatModel";
2024-07-20 18:10:42 +02:00
import { Color } from "./colors";
2024-07-20 18:58:25 +02:00
import { localeCompare } from "../Model/Utility/utility";
2024-07-20 04:17:10 +02:00
export default class ChatViewModel {
2024-07-20 14:16:26 +02:00
chatModel: ChatModel;
chatListViewModel: ChatListViewModel;
2024-07-20 16:34:16 +02:00
storageModel: StorageModel;
2024-07-20 04:17:10 +02:00
2024-07-20 14:16:26 +02:00
// state
2024-07-20 16:49:01 +02:00
index: React.State<number> = new React.State(0);
2024-07-20 14:16:26 +02:00
primaryChannel: React.State<string> = new React.State("");
primaryChannelInput: React.State<string> = new React.State("");
2024-07-20 18:49:05 +02:00
secondaryChannels: React.ListState<string> = new React.ListState();
newSecondaryChannelInput: React.State<string> = new React.State("");
2024-07-20 18:10:42 +02:00
color: React.State<Color> = new React.State<Color>(Color.Standard);
2024-07-20 16:28:00 +02:00
selectedPage: React.State<ChatPageType> = new React.State<ChatPageType>(
ChatPageType.Messages
);
2024-07-20 14:16:26 +02:00
// guards
cannotSetPrimaryChannel: React.State<boolean> = React.createProxyState(
[this.primaryChannel, this.primaryChannelInput],
() =>
this.primaryChannelInput.value == "" ||
this.primaryChannelInput.value == this.primaryChannel.value
);
2024-07-20 16:49:01 +02:00
// sorting
updateIndex = (): void => {
const index = this.chatListViewModel.getIndexOfChat(this);
this.index.value = index;
};
2024-07-20 14:16:26 +02:00
// methods
2024-07-20 16:34:16 +02:00
open = (): void => {
2024-07-20 14:16:26 +02:00
this.chatListViewModel.openChat(this);
2024-07-20 16:28:00 +02:00
};
2024-07-20 14:16:26 +02:00
2024-07-20 16:34:16 +02:00
close = (): void => {
this.chatListViewModel.closeChat();
2024-07-20 16:28:00 +02:00
};
2024-07-20 17:30:36 +02:00
setPrimaryChannel = (): void => {
this.chatModel.setPrimaryChannel(this.primaryChannelInput.value);
this.primaryChannel.value = this.chatModel.info.primaryChannel;
this.chatListViewModel.updateIndices();
2024-07-20 18:49:05 +02:00
};
addSecondaryChannel = (): void => {
this.secondaryChannels.add(this.newSecondaryChannelInput.value);
this.newSecondaryChannelInput.value = "";
this.storeSecondaryChannels();
2024-07-20 18:58:25 +02:00
this.restoreSecondaryChannels();
2024-07-20 18:49:05 +02:00
};
removeSecondaryChannel = (secondaryChannel: string): void => {
this.secondaryChannels.remove(secondaryChannel);
this.storeSecondaryChannels();
};
storeSecondaryChannels = (): void => {
this.chatModel.setSecondaryChannels([
...this.secondaryChannels.value.values(),
]);
2024-07-20 18:58:25 +02:00
};
2024-07-20 17:30:36 +02:00
2024-07-20 18:10:42 +02:00
setColor = (newColor: Color): void => {
this.color.value = newColor;
this.chatModel.setColor(newColor);
2024-07-20 18:49:05 +02:00
};
2024-07-20 18:10:42 +02:00
2024-07-20 16:34:16 +02:00
// restore
restorePageSelection = (): void => {
const path: string[] = storageKeys.chatLastUsedPage(this.chatModel.id);
const lastUsedPage: string | null = this.storageModel.restore(path);
if (lastUsedPage != null) {
this.selectedPage.value = lastUsedPage as any;
}
this.selectedPage.subscribeSilent((newPage) => {
this.storageModel.store(path, newPage);
});
};
2024-07-20 18:58:25 +02:00
restoreSecondaryChannels = (): void => {
this.secondaryChannels.clear();
for (const secondaryChannel of this.chatModel.info.secondaryChannels.sort(localeCompare)) {
this.secondaryChannels.add(secondaryChannel);
}
};
2024-07-20 14:16:26 +02:00
// init
constructor(chatListViewModel: ChatListViewModel, chatModel: ChatModel) {
this.chatModel = chatModel;
2024-07-20 16:34:16 +02:00
this.storageModel = chatModel.storageModel;
2024-07-20 14:16:26 +02:00
this.chatListViewModel = chatListViewModel;
this.primaryChannel.value = chatModel.info.primaryChannel;
this.primaryChannelInput.value = chatModel.info.primaryChannel;
2024-07-20 18:49:05 +02:00
2024-07-20 18:10:42 +02:00
this.color.value = chatModel.color;
2024-07-20 16:34:16 +02:00
2024-07-20 18:58:25 +02:00
this.restoreSecondaryChannels();
2024-07-20 16:34:16 +02:00
this.restorePageSelection();
2024-07-20 16:49:01 +02:00
this.updateIndex();
2024-07-20 14:16:26 +02:00
}
}
2024-07-20 16:28:00 +02:00
// types
export enum ChatPageType {
2024-07-20 16:34:16 +02:00
Settings = "settings",
Messages = "messages",
AllObjects = "all",
Kanban = "kanban",
Calendar = "calendar",
Progress = "progress",
2024-07-20 16:28:00 +02:00
}