2022-10-26 11:41:08 +02:00
|
|
|
import { randomUUID } from "crypto";
|
2022-09-26 23:11:43 +02:00
|
|
|
import { DateTime } from "luxon";
|
|
|
|
import { Message, MessageType } from "../types/message.js";
|
|
|
|
|
|
|
|
const maxMessageLength = 255;
|
|
|
|
|
|
|
|
class MessageManager {
|
|
|
|
private messages: Message[] = [];
|
|
|
|
|
2022-09-27 23:38:40 +02:00
|
|
|
public addMessage(type: MessageType, message: string, detail?: string, isImportant = false) {
|
2022-09-26 23:11:43 +02:00
|
|
|
this.messages.push({
|
2022-10-26 11:41:08 +02:00
|
|
|
id: randomUUID(),
|
2022-09-26 23:11:43 +02:00
|
|
|
message: message,
|
|
|
|
type: type,
|
|
|
|
time: DateTime.now(),
|
2022-09-27 23:38:40 +02:00
|
|
|
viewed: !isImportant,
|
2022-09-26 23:11:43 +02:00
|
|
|
detail: detail
|
|
|
|
});
|
|
|
|
if (this.messages.length > maxMessageLength) {
|
|
|
|
this.messages.shift();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
public error(message: string, detail?: string) {
|
2022-09-27 23:38:40 +02:00
|
|
|
this.addMessage(MessageType.ERROR, message, detail, true);
|
2022-09-26 23:11:43 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
public warn(message: string, detail?: string) {
|
|
|
|
this.addMessage(MessageType.WARN, message, detail);
|
|
|
|
}
|
|
|
|
|
|
|
|
public info(message: string, detail?: string) {
|
|
|
|
this.addMessage(MessageType.INFO, message, detail);
|
|
|
|
}
|
|
|
|
|
|
|
|
public success(message: string, detail?: string) {
|
|
|
|
this.addMessage(MessageType.SUCCESS, message, detail);
|
|
|
|
}
|
2022-10-13 17:11:55 +02:00
|
|
|
|
|
|
|
public get(){
|
|
|
|
return this.messages;
|
|
|
|
}
|
2022-10-26 14:09:10 +02:00
|
|
|
|
|
|
|
public getById(id: string){
|
|
|
|
return this.messages.find(value=>value.id == id);
|
|
|
|
}
|
|
|
|
|
|
|
|
public markReaded(id: string){
|
|
|
|
const index = this.messages.findIndex(value=>value.id == id);
|
|
|
|
if(index == -1){
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
this.messages[index].viewed = true;
|
|
|
|
return true;
|
|
|
|
}
|
2022-09-26 23:11:43 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
const messageManager = new MessageManager();
|
|
|
|
export default messageManager;
|