Files
CT/src/ViewModel/Global/notificationViewModel.ts
T

98 lines
2.7 KiB
TypeScript
Raw Normal View History

2026-04-01 19:00:16 +00:00
import * as React from "bloatless-react";
import ChatModel, { ChatMessage } from "../../Model/Chat/chatModel";
import ChatListViewModel from "../Chat/chatListViewModel";
2026-04-08 14:02:12 +00:00
import { ChatPageTypes } from "../Chat/chatViewModel";
2026-04-01 19:00:16 +00:00
export default class NotificationViewModel {
// data
seenMessageIds = new Set<string>();
messagesInMarquee: Notification[] = [];
marquee = new React.State<Notification | undefined>(undefined);
currentIndex = 0;
interval: number | undefined = undefined;
// main
showNotification = (message: ChatMessage): void => {
const notification = NotificationViewModel.createNotification(message);
if (this.seenMessageIds.has(message.id)) return;
2026-04-01 19:14:42 +00:00
const currentChat =
this.chatListViewModel.selectedChat.value.chatModel.info
.primaryChannel;
const currentPage =
this.chatListViewModel.selectedChat.value.selectedPage.value;
if (
notification.chat == currentChat &&
2026-04-08 14:02:12 +00:00
currentPage == ChatPageTypes.Messages
)
return;
2026-04-01 19:00:16 +00:00
this.messagesInMarquee.push(notification);
this.startLoop();
};
2026-04-01 19:14:42 +00:00
openNotification = () => {
const notification = this.marquee.value;
if (notification == undefined) return;
const chat = [
...this.chatListViewModel.chatViewModels.value.values(),
].find(
(chat) => chat.chatModel.info.primaryChannel == notification.chat,
);
chat.open();
2026-04-08 14:02:12 +00:00
chat.selectedPage.value = ChatPageTypes.Messages;
2026-04-01 19:14:42 +00:00
};
2026-04-01 19:00:16 +00:00
// loop
loop = () => {
if (this.messagesInMarquee.length == 0) {
this.marquee.value = undefined;
return this.stopLoop();
}
const notification = this.messagesInMarquee.shift();
this.seenMessageIds.delete(notification.messageId);
this.marquee.value = notification;
};
startLoop = () => {
if (this.interval != undefined) return;
this.loop();
this.interval = setInterval(() => {
this.loop();
}, 5000);
};
stopLoop = () => {
clearInterval(this.interval);
this.interval = undefined;
};
2026-04-01 19:14:42 +00:00
skipLoop = () => {
this.stopLoop();
this.startLoop();
};
2026-04-01 19:14:42 +00:00
2026-04-01 19:00:16 +00:00
// init
2026-04-13 10:58:21 +00:00
constructor(public readonly chatListViewModel: ChatListViewModel) {}
2026-04-01 19:00:16 +00:00
// util
static createNotification(message: ChatMessage): Notification {
return {
messageId: message.id,
chat: ChatModel.splitChannel(message.channel)[0],
sender: message.sender,
body: message.body,
};
}
}
export interface Notification {
messageId: string;
chat: string;
sender: string;
body: string;
}