Files
CT/src/ViewModel/Chat/chatViewModel.ts
T

203 lines
6.4 KiB
TypeScript
Raw Normal View History

2024-07-20 04:17:10 +02:00
import * as React from "bloatless-react";
2024-07-28 17:15:43 +02:00
import CalendarPageViewModel, {
2026-04-01 08:29:39 +00:00
CALENDAR_EVENT_BOARD_ID,
2024-07-28 17:15:43 +02:00
} from "../Pages/calendarPageViewModel";
2026-04-06 20:47:49 +00:00
import ChatModel, {
ChatMessage,
ChatMessageReaction,
} from "../../Model/Chat/chatModel";
import StorageModel, {
2026-04-08 14:02:12 +00:00
StorageModelSubPaths,
2026-04-01 08:29:39 +00:00
filePaths,
} from "../../Model/Global/storageModel";
2024-07-20 16:34:16 +02:00
2024-07-20 14:16:26 +02:00
import ChatListViewModel from "./chatListViewModel";
2026-04-08 14:02:12 +00:00
import { Colors } from "../../colors";
2024-07-25 16:02:49 +02:00
import CoreViewModel from "../Global/coreViewModel";
import { Entry } from "../../View/Components/option";
2024-07-22 21:07:44 +02:00
import MessagePageViewModel from "../Pages/messagePageViewModel";
import SettingsPageViewModel from "../Pages/settingsPageViewModel";
2024-07-22 19:52:08 +02:00
import SettingsViewModel from "../Global/settingsViewModel";
2024-07-22 21:07:44 +02:00
import TaskPageViewModel from "../Pages/taskPageViewModel";
2024-07-28 17:15:43 +02:00
import { translations } from "../../View/translations";
import ConnectionViewModel from "../Global/connectionViewModel";
2026-04-01 19:00:16 +00:00
import NotificationViewModel from "../Global/notificationViewModel";
2024-07-20 04:17:10 +02:00
export default class ChatViewModel {
2026-04-01 08:29:39 +00:00
chatModel: ChatModel;
storageModel: StorageModel;
settingsViewModel: SettingsViewModel;
2026-04-01 19:00:16 +00:00
notificationViewModel: NotificationViewModel;
connectionViewModel: ConnectionViewModel;
2026-04-01 08:29:39 +00:00
chatListViewModel: ChatListViewModel;
calendarViewModel: CalendarPageViewModel;
taskPageViewModel: TaskPageViewModel;
messagePageViewModel: MessagePageViewModel;
settingsPageViewModel: SettingsPageViewModel;
// state
2026-04-08 14:02:12 +00:00
displayedColor: React.State<Colors> = new React.State<any>(Colors.Standard);
selectedPage: React.State<ChatPageTypes> = new React.State<any>(
ChatPageTypes.Messages,
2024-07-22 03:01:04 +02:00
);
2024-07-20 16:34:16 +02:00
2026-04-01 08:29:39 +00:00
index: React.State<number> = new React.State(0);
hasUnreadMessages: React.State<boolean> = new React.State(false);
taskBoardSuggestions: React.MapState<Entry> = new React.MapState();
2024-07-22 21:07:44 +02:00
2026-04-01 08:29:39 +00:00
// view
open = (): void => {
this.chatListViewModel.openChat(this);
};
close = (): void => {
this.chatListViewModel.closeChat();
};
2026-04-01 19:00:16 +00:00
closeSubPages = (): void => {};
2026-04-01 08:29:39 +00:00
2026-04-08 14:02:12 +00:00
setColor = (color: Colors): void => {
2026-04-01 08:29:39 +00:00
this.setDisplayedColor(color);
this.chatModel.setColor(color);
};
2026-04-08 14:02:12 +00:00
setDisplayedColor = (color: Colors): void => {
2026-04-01 08:29:39 +00:00
this.displayedColor.value = color;
};
resetColor = (): void => {
this.displayedColor.value = this.settingsPageViewModel.color.value;
};
updateIndex = (): void => {
const index: number =
this.chatListViewModel.chatIndexManager.getIndex(this);
this.index.value = index;
};
2024-07-21 21:41:19 +02:00
// load
2026-04-01 08:29:39 +00:00
loadPageSelection = (): void => {
const path: string[] = StorageModel.getPath(
2026-04-08 14:02:12 +00:00
StorageModelSubPaths.Chat,
2026-04-01 08:29:39 +00:00
filePaths.chat.lastUsedPage(this.chatModel.id),
);
const lastUsedPage: string | null = this.storageModel.read(path);
if (lastUsedPage != null) {
this.selectedPage.value = lastUsedPage as any;
}
this.selectedPage.subscribeSilent((newPage) => {
this.storageModel.write(path, newPage);
this.resetColor();
});
};
loadInfo = (): void => {
2026-04-01 14:16:38 +00:00
this.updateReadStatus();
2026-04-01 08:29:39 +00:00
this.taskBoardSuggestions.set(CALENDAR_EVENT_BOARD_ID, [
CALENDAR_EVENT_BOARD_ID,
translations.chatPage.calendar.eventsBoard,
]);
};
2026-04-01 14:16:38 +00:00
updateReadStatus = (): void => {
if (
this.chatListViewModel.selectedChat.value == this &&
2026-04-08 14:02:12 +00:00
this.selectedPage.value == ChatPageTypes.Messages
) {
2026-04-01 14:16:38 +00:00
this.chatModel.setReadStatus(false);
}
this.hasUnreadMessages.value = this.chatModel.info.hasUnreadMessages;
2026-04-01 19:00:16 +00:00
};
2026-04-01 14:16:38 +00:00
setReadStatus = (hasUnreadMessages: boolean): void => {
this.chatModel.setReadStatus(hasUnreadMessages);
this.hasUnreadMessages.value = hasUnreadMessages;
2026-04-01 19:00:16 +00:00
};
2026-04-01 14:16:38 +00:00
subscribeReadStatus = (): void => {
2026-04-06 15:50:13 +00:00
React.createProxyState(
[this.selectedPage, this.chatListViewModel.selectedChat],
() => {
if (this.chatListViewModel.selectedChat.value != this) return;
2026-04-08 14:02:12 +00:00
if (this.selectedPage.value != ChatPageTypes.Messages) return;
2026-04-06 15:50:13 +00:00
this.setReadStatus(false);
},
);
};
2026-04-01 08:29:39 +00:00
// init
constructor(
public coreViewModel: CoreViewModel,
storageModel: StorageModel,
chatModel: ChatModel,
settingsViewModel: SettingsViewModel,
2026-04-01 19:00:16 +00:00
notificationViewModel: NotificationViewModel,
connectionViewModel: ConnectionViewModel,
2026-04-01 08:29:39 +00:00
chatListViewModel: ChatListViewModel,
) {
// models
this.storageModel = storageModel;
this.chatModel = chatModel;
this.settingsViewModel = settingsViewModel;
this.connectionViewModel = connectionViewModel;
2026-04-01 19:00:16 +00:00
this.notificationViewModel = notificationViewModel;
2026-04-01 08:29:39 +00:00
this.chatListViewModel = chatListViewModel;
// page viewModels
this.calendarViewModel = new CalendarPageViewModel(
coreViewModel,
this,
this.storageModel,
this.chatModel.fileModel.boardsAndTasksModel.calendarModel,
this.chatModel.fileModel.boardsAndTasksModel,
);
this.taskPageViewModel = new TaskPageViewModel(
this.coreViewModel,
this,
this.storageModel,
this.chatModel.fileModel.boardsAndTasksModel,
);
this.messagePageViewModel = new MessagePageViewModel(
this.coreViewModel,
this,
);
this.settingsPageViewModel = new SettingsPageViewModel(
this.coreViewModel,
this,
);
// handlers
chatModel.chatMessageHandlerManager.addHandler(
(chatMessage: ChatMessage) => {
this.messagePageViewModel.showChatMessage(chatMessage);
2026-04-01 14:16:38 +00:00
this.updateReadStatus();
2026-04-01 19:00:16 +00:00
this.notificationViewModel.showNotification(chatMessage);
2026-04-01 08:29:39 +00:00
},
);
2026-04-06 20:47:49 +00:00
chatModel.reactionHandlerManager.addHandler(
(reaction: ChatMessageReaction) => {
2026-04-07 18:48:01 +00:00
this.messagePageViewModel.handleReaction(reaction);
2026-04-06 20:47:49 +00:00
},
);
2026-04-01 08:29:39 +00:00
// load
this.loadPageSelection();
this.resetColor();
this.loadInfo();
this.subscribeReadStatus();
2026-04-01 08:29:39 +00:00
}
2024-07-20 14:16:26 +02:00
}
2024-07-20 16:28:00 +02:00
// types
2026-04-08 14:02:12 +00:00
export enum ChatPageTypes {
2026-04-01 08:29:39 +00:00
Settings = "settings",
Messages = "messages",
Tasks = "tasks",
Calendar = "calendar",
2024-07-20 16:28:00 +02:00
}