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";
|
2024-07-25 19:32:46 +02:00
|
|
|
import StorageModel, {
|
2026-04-01 08:29:39 +00:00
|
|
|
StorageModelSubPath,
|
|
|
|
|
filePaths,
|
2024-07-25 19:32:46 +02:00
|
|
|
} from "../../Model/Global/storageModel";
|
2024-07-20 16:34:16 +02:00
|
|
|
|
2024-07-20 14:16:26 +02:00
|
|
|
import ChatListViewModel from "./chatListViewModel";
|
2024-07-23 02:50:22 +02:00
|
|
|
import { Color } from "../../colors";
|
2024-07-25 16:02:49 +02:00
|
|
|
import CoreViewModel from "../Global/coreViewModel";
|
2024-07-28 17:09:27 +02:00
|
|
|
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";
|
2026-04-01 08:38:40 +00:00
|
|
|
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;
|
2026-04-01 08:38:40 +00:00
|
|
|
connectionViewModel: ConnectionViewModel;
|
2026-04-01 08:29:39 +00:00
|
|
|
chatListViewModel: ChatListViewModel;
|
|
|
|
|
|
|
|
|
|
calendarViewModel: CalendarPageViewModel;
|
|
|
|
|
taskPageViewModel: TaskPageViewModel;
|
|
|
|
|
messagePageViewModel: MessagePageViewModel;
|
|
|
|
|
settingsPageViewModel: SettingsPageViewModel;
|
|
|
|
|
|
|
|
|
|
// state
|
|
|
|
|
displayedColor: React.State<Color> = new React.State<any>(Color.Standard);
|
|
|
|
|
selectedPage: React.State<ChatPageType> = new React.State<any>(
|
|
|
|
|
ChatPageType.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
|
|
|
|
|
|
|
|
setColor = (color: Color): void => {
|
|
|
|
|
this.setDisplayedColor(color);
|
|
|
|
|
this.chatModel.setColor(color);
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
setDisplayedColor = (color: Color): void => {
|
|
|
|
|
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(
|
|
|
|
|
StorageModelSubPath.Chat,
|
|
|
|
|
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 => {
|
2026-04-02 19:22:10 +00:00
|
|
|
if (
|
|
|
|
|
this.chatListViewModel.selectedChat.value == this &&
|
|
|
|
|
this.selectedPage.value == ChatPageType.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
|
|
|
|
2026-04-02 19:22:10 +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;
|
|
|
|
|
if (this.selectedPage.value != ChatPageType.Messages) return;
|
|
|
|
|
this.setReadStatus(false);
|
|
|
|
|
},
|
|
|
|
|
);
|
|
|
|
|
};
|
2026-04-02 19:22:10 +00:00
|
|
|
|
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,
|
2026-04-01 08:38:40 +00:00
|
|
|
connectionViewModel: ConnectionViewModel,
|
2026-04-01 08:29:39 +00:00
|
|
|
chatListViewModel: ChatListViewModel,
|
|
|
|
|
) {
|
|
|
|
|
// models
|
|
|
|
|
this.storageModel = storageModel;
|
|
|
|
|
this.chatModel = chatModel;
|
|
|
|
|
this.settingsViewModel = settingsViewModel;
|
2026-04-01 08:38:40 +00:00
|
|
|
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();
|
2026-04-02 19:22:10 +00:00
|
|
|
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
|
|
|
|
|
export enum ChatPageType {
|
2026-04-01 08:29:39 +00:00
|
|
|
Settings = "settings",
|
|
|
|
|
Messages = "messages",
|
|
|
|
|
Tasks = "tasks",
|
|
|
|
|
Calendar = "calendar",
|
2024-07-20 16:28:00 +02:00
|
|
|
}
|