2024-07-20 04:17:10 +02:00
|
|
|
import * as React from "bloatless-react";
|
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";
|
|
|
|
|
import ConnectionViewModel from "../Global/connectionViewModel";
|
|
|
|
|
import { Colors } from "../../colors";
|
|
|
|
|
import { CommonKeys } from "../../View/keystrokes";
|
|
|
|
|
import { Entry } from "../../View/Components/option";
|
2024-07-25 19:32:46 +02:00
|
|
|
import StorageModel, {
|
2026-04-08 14:02:12 +00:00
|
|
|
StorageModelSubPaths,
|
2026-04-01 08:29:39 +00:00
|
|
|
filePaths,
|
2024-07-25 19:32:46 +02:00
|
|
|
} from "../../Model/Global/storageModel";
|
2026-04-18 11:55:01 +00:00
|
|
|
import ChatModel, {
|
|
|
|
|
ChatMessage,
|
|
|
|
|
ChatMessageReaction,
|
|
|
|
|
} from "../../Model/Chat/chatModel";
|
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>(
|
|
|
|
|
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 {
|
|
|
|
|
return this.chatListViewModel.selectedChat.value == this;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
get contextSelection(): ChatPageTypes {
|
|
|
|
|
return this.selectedPage.value;
|
|
|
|
|
}
|
|
|
|
|
|
2026-04-01 08:29:39 +00:00
|
|
|
// view
|
|
|
|
|
open = (): void => {
|
2026-04-12 19:20:37 +00:00
|
|
|
this.coreViewModel.context = this;
|
2026-04-01 08:29:39 +00:00
|
|
|
this.chatListViewModel.openChat(this);
|
2026-04-13 19:45:52 +00:00
|
|
|
//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-04-14 17:19:35 +00:00
|
|
|
this.closeCurrentContext();
|
2026-04-12 19:20:37 +00:00
|
|
|
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-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),
|
|
|
|
|
);
|
2026-04-09 16:48:22 +00:00
|
|
|
const lastUsedPage: string | null =
|
|
|
|
|
this.coreViewModel.storageModel.read(path);
|
2026-04-01 08:29:39 +00:00
|
|
|
if (lastUsedPage != null) {
|
2026-04-13 19:45:52 +00:00
|
|
|
this.openPage(lastUsedPage as ChatPageTypes);
|
2026-04-01 08:29:39 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
this.selectedPage.subscribeSilent((newPage) => {
|
2026-04-09 16:48:22 +00:00
|
|
|
this.coreViewModel.storageModel.write(path, newPage);
|
2026-04-01 08:29:39 +00:00
|
|
|
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,
|
2026-04-09 16:48:22 +00:00
|
|
|
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-04-02 19:22:10 +00:00
|
|
|
if (
|
|
|
|
|
this.chatListViewModel.selectedChat.value == this &&
|
2026-04-08 14:02:12 +00:00
|
|
|
this.selectedPage.value == ChatPageTypes.Messages
|
2026-04-02 19:22:10 +00:00
|
|
|
) {
|
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;
|
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-02 19:22:10 +00:00
|
|
|
|
2026-04-14 17:19:35 +00:00
|
|
|
// exit
|
|
|
|
|
close = (): void => {
|
|
|
|
|
this.coreViewModel.closeContext(this.contextId);
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
handleContextClose = (): void => {
|
|
|
|
|
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-09 00:04:27 +02:00
|
|
|
readonly coreViewModel: CoreViewModel,
|
2026-04-13 10:58:21 +00:00
|
|
|
public readonly chatModel: ChatModel,
|
|
|
|
|
public readonly settingsViewModel: SettingsViewModel,
|
|
|
|
|
public readonly notificationViewModel: NotificationViewModel,
|
|
|
|
|
public readonly connectionViewModel: ConnectionViewModel,
|
|
|
|
|
public readonly chatListViewModel: ChatListViewModel,
|
2026-04-01 08:29:39 +00:00
|
|
|
) {
|
2026-09-09 00:04:27 +02:00
|
|
|
super("chat", coreViewModel);
|
2026-04-12 19:20:37 +00:00
|
|
|
|
2026-04-01 08:29:39 +00:00
|
|
|
// 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,
|
|
|
|
|
);
|
|
|
|
|
this.settingsPageViewModel = new SettingsPageViewModel(
|
|
|
|
|
this.coreViewModel,
|
|
|
|
|
this,
|
|
|
|
|
);
|
|
|
|
|
|
|
|
|
|
// handlers
|
2026-04-14 09:20:44 +00:00
|
|
|
chatModel.chatMessageHandlerManager.setHandler(
|
|
|
|
|
this.chatModel.id,
|
2026-04-01 08:29:39 +00:00
|
|
|
(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-14 09:20:44 +00:00
|
|
|
chatModel.reactionHandlerManager.setHandler(
|
|
|
|
|
this.chatModel.id,
|
2026-04-06 20:47:49 +00:00
|
|
|
(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-12 19:20:37 +00:00
|
|
|
|
|
|
|
|
// keystrokes
|
|
|
|
|
this.registerKeyStroke(CommonKeys.Home, this.close);
|
2026-04-14 09:26:25 +00:00
|
|
|
this.registerKeyStroke("u", () =>
|
2026-04-12 19:20:37 +00:00
|
|
|
this.openPage(ChatPageTypes.Messages),
|
|
|
|
|
);
|
2026-04-14 09:26:25 +00:00
|
|
|
this.registerKeyStroke("i", () => this.openPage(ChatPageTypes.Tasks));
|
|
|
|
|
this.registerKeyStroke("o", () =>
|
2026-04-12 19:20:37 +00:00
|
|
|
this.openPage(ChatPageTypes.Calendar),
|
|
|
|
|
);
|
|
|
|
|
this.registerKeyStroke(CommonKeys.Settings, () =>
|
|
|
|
|
this.openPage(ChatPageTypes.Settings),
|
|
|
|
|
);
|
|
|
|
|
|
|
|
|
|
// context
|
2026-04-13 11:23:17 +00:00
|
|
|
this.chatListViewModel.selectedChat.subscribeSilent(
|
|
|
|
|
this.updateContexts,
|
|
|
|
|
);
|
2026-04-12 19:20:37 +00:00
|
|
|
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",
|
|
|
|
|
Messages = "messages",
|
|
|
|
|
Tasks = "tasks",
|
|
|
|
|
Calendar = "calendar",
|
2024-07-20 16:28:00 +02:00
|
|
|
}
|