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

240 lines
7.8 KiB
TypeScript
Raw Normal View History

2026-04-18 11:55:01 +00:00
import ChatListViewModel from "./chatListViewModel";
import TaskPageViewModel from "../Pages/taskPageViewModel";
import SettingsPageViewModel from "../Pages/settingsPageViewModel";
import MessagePageViewModel from "../Pages/messagePageViewModel";
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-18 11:55:01 +00:00
import SettingsViewModel from "../Global/settingsViewModel";
import NotificationViewModel from "../Global/notificationViewModel";
import CoreViewModel, { Context, ContextHost } from "../Global/coreViewModel";
2026-09-28 15:21:50 +02:00
import ContactListViewModel from "../Global/contactViewModel";
2026-04-18 11:55:01 +00:00
import ConnectionViewModel from "../Global/connectionViewModel";
2026-09-23 00:09:09 +02:00
import * as React from "../../react";
2026-04-18 11:55:01 +00:00
import { Colors } from "../../colors";
import { CommonKeys } from "../../View/keystrokes";
import { Entry } from "../../View/Components/option";
import StorageModel, {
2026-04-08 14:02:12 +00:00
StorageModelSubPaths,
2026-04-01 08:29:39 +00:00
filePaths,
} from "../../Model/Global/storageModel";
2026-04-18 11:55:01 +00:00
import ChatModel, {
ChatMessage,
ChatMessageReaction,
} from "../../Model/Chat/chatModel";
import FileTransferViewModel from "../Global/fileTransferViewModel";
2026-04-12 19:20:37 +00:00
export default class ChatViewModel extends ContextHost<ChatPageTypes> {
2026-04-01 08:29:39 +00:00
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>(
2026-09-26 23:32:38 +02:00
ChatPageTypes.Messages,
2024-07-22 03:01:04 +02:00
);
2026-04-12 19:20:37 +00:00
pageContexts = new React.MapState<Context>();
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-12 19:20:37 +00:00
// context
get isOpen(): boolean {
2026-09-26 23:32:38 +02:00
return this.chatListViewModel.selectedChat.value == this;
2026-04-12 19:20:37 +00:00
}
get contextSelection(): ChatPageTypes {
2026-09-26 23:32:38 +02:00
return this.selectedPage.value;
2026-04-12 19:20:37 +00:00
}
2026-04-01 08:29:39 +00:00
// view
open = (): void => {
2026-09-26 23:32:38 +02:00
this.coreViewModel.context = this;
this.chatListViewModel.openChat(this);
//this.selectedPage.callSubscriptions();
2026-04-01 08:29:39 +00:00
};
2026-04-15 12:47:10 +00:00
2026-04-12 19:20:37 +00:00
openPage = (page: ChatPageTypes): void => {
2026-09-26 23:32:38 +02:00
this.closeCurrentContext();
this.selectedPage.value = page;
2026-04-01 08:29:39 +00:00
};
2026-04-08 14:02:12 +00:00
setColor = (color: Colors): void => {
2026-09-26 23:32:38 +02:00
this.setDisplayedColor(color);
this.chatModel.setColor(color);
2026-04-01 08:29:39 +00:00
};
2026-04-08 14:02:12 +00:00
setDisplayedColor = (color: Colors): void => {
2026-09-26 23:32:38 +02:00
this.displayedColor.value = color;
2026-04-01 08:29:39 +00:00
};
resetColor = (): void => {
2026-09-26 23:32:38 +02:00
this.displayedColor.value = this.settingsPageViewModel.color.value;
2026-04-01 08:29:39 +00:00
};
updateIndex = (): void => {
2026-09-26 23:32:38 +02:00
const index: number =
this.chatListViewModel.chatIndexManager.getIndex(this);
this.index.value = index;
2026-04-01 08:29:39 +00:00
};
share = (): void => {
this.fileTransferViewModel.showDirectionSelectionModal();
}
2024-07-21 21:41:19 +02:00
// load
2026-04-01 08:29:39 +00:00
loadPageSelection = (): void => {
2026-09-26 23:32:38 +02:00
const path: string[] = StorageModel.getPath(
StorageModelSubPaths.Chat,
filePaths.chat.lastUsedPage(this.chatModel.id),
);
const lastUsedPage: string | null =
this.coreViewModel.storageModel.read(path);
if (lastUsedPage != null) {
this.openPage(lastUsedPage as ChatPageTypes);
}
this.selectedPage.subscribeSilent((newPage) => {
this.coreViewModel.storageModel.write(path, newPage);
this.resetColor();
});
2026-04-01 08:29:39 +00:00
};
loadInfo = (): void => {
2026-09-26 23:32:38 +02:00
this.updateReadStatus();
this.taskBoardSuggestions.set(CALENDAR_EVENT_BOARD_ID, [
CALENDAR_EVENT_BOARD_ID,
this.coreViewModel.translations.chatPage.calendar.eventsBoard,
]);
2026-04-01 08:29:39 +00:00
};
2026-04-01 14:16:38 +00:00
updateReadStatus = (): void => {
2026-09-26 23:32:38 +02:00
if (
this.chatListViewModel.selectedChat.value == this &&
this.selectedPage.value == ChatPageTypes.Messages
) {
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 => {
2026-09-26 23:32:38 +02:00
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-09-26 23:32:38 +02:00
React.createProxyState(
[this.selectedPage, this.chatListViewModel.selectedChat],
() => {
if (this.chatListViewModel.selectedChat.value != this) return;
if (this.selectedPage.value != ChatPageTypes.Messages) return;
this.setReadStatus(false);
},
);
2026-04-06 15:50:13 +00:00
};
2026-04-14 17:19:35 +00:00
// exit
close = (): void => {
2026-09-26 23:32:38 +02:00
this.coreViewModel.closeContext(this.contextId);
2026-04-14 17:19:35 +00:00
};
handleContextClose = (): void => {
2026-09-26 23:32:38 +02:00
this.chatListViewModel.closeChat();
2026-04-15 12:47:10 +00:00
};
2026-04-14 17:19:35 +00:00
2026-04-01 08:29:39 +00:00
// init
constructor(
2026-09-26 23:32:38 +02:00
readonly coreViewModel: CoreViewModel,
public readonly chatModel: ChatModel,
public readonly settingsViewModel: SettingsViewModel,
public readonly notificationViewModel: NotificationViewModel,
public readonly connectionViewModel: ConnectionViewModel,
public readonly chatListViewModel: ChatListViewModel,
2026-09-28 15:21:50 +02:00
public readonly contactListViewModel: ContactListViewModel,
public readonly fileTransferViewModel: FileTransferViewModel,
2026-04-01 08:29:39 +00:00
) {
2026-09-26 23:32:38 +02:00
super("chat", coreViewModel);
// page viewModels
this.calendarViewModel = new CalendarPageViewModel(
coreViewModel,
this,
this.chatModel.fileModel.boardsAndTasksModel.calendarModel,
this.chatModel.fileModel.boardsAndTasksModel,
);
this.taskPageViewModel = new TaskPageViewModel(
this.coreViewModel,
this,
this.chatModel.fileModel.boardsAndTasksModel,
);
this.messagePageViewModel = new MessagePageViewModel(
this.coreViewModel,
this,
2026-09-28 15:21:50 +02:00
this.contactListViewModel,
2026-09-26 23:32:38 +02:00
);
this.settingsPageViewModel = new SettingsPageViewModel(
this.coreViewModel,
this,
);
// handlers
chatModel.chatMessageHandlerManager.setHandler(
this.chatModel.id,
(chatMessage: ChatMessage) => {
this.messagePageViewModel.showChatMessage(chatMessage);
this.updateReadStatus();
this.notificationViewModel.showNotification(chatMessage);
},
);
chatModel.reactionHandlerManager.setHandler(
this.chatModel.id,
(reaction: ChatMessageReaction) => {
this.messagePageViewModel.handleReaction(reaction);
},
);
chatModel.changeHandlerManager.setHandler(
this.chatModel.id,
this.settingsPageViewModel.updateData,
);
// load
this.loadPageSelection();
this.resetColor();
this.loadInfo();
this.subscribeReadStatus();
// keystrokes
this.registerKeyStroke(CommonKeys.Home, this.close);
this.registerKeyStroke("u", () =>
this.openPage(ChatPageTypes.Messages),
);
this.registerKeyStroke("i", () => this.openPage(ChatPageTypes.Tasks));
this.registerKeyStroke("o", () =>
this.openPage(ChatPageTypes.Calendar),
);
this.registerKeyStroke(CommonKeys.Settings, () =>
this.openPage(ChatPageTypes.Settings),
);
// context
this.chatListViewModel.selectedChat.subscribeSilent(
this.updateContexts,
);
this.selectedPage.subscribeSilent(this.updateContexts);
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",
2026-09-26 23:32:38 +02:00
Messages = "messages",
Tasks = "tasks",
Calendar = "calendar",
2024-07-20 16:28:00 +02:00
}