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

136 lines
4.3 KiB
TypeScript
Raw Normal View History

2024-07-21 01:12:35 +02:00
import * as React from "bloatless-react";
2026-04-06 20:47:49 +00:00
import {
ChatMessage,
ChatMessageReaction,
ChatMessageReactionReference,
2026-04-08 14:02:12 +00:00
ChatMessageStatuses,
2026-04-07 18:39:55 +00:00
ReactionSymbols,
2026-04-06 20:47:49 +00:00
} from "../../Model/Chat/chatModel";
2024-07-21 01:12:35 +02:00
2024-07-25 16:02:49 +02:00
import CoreViewModel from "../Global/coreViewModel";
2024-07-22 21:07:44 +02:00
import MessagePageViewModel from "../Pages/messagePageViewModel";
2024-07-21 13:13:41 +02:00
2024-07-21 01:12:35 +02:00
export default class ChatMessageViewModel {
2026-04-01 08:29:39 +00:00
// data
chatMessage: ChatMessage;
channel: string;
sender: string;
dateSent: string;
body: React.State<string> = new React.State("");
2026-04-08 14:02:12 +00:00
status: React.State<ChatMessageStatuses | any> = new React.State<any>(
2026-04-01 08:29:39 +00:00
undefined,
);
sentByUser: boolean;
2024-07-26 03:14:55 +02:00
2026-04-06 20:47:49 +00:00
reactionsThumbsUp = new React.MapState<ChatMessageReaction>();
reactionsCheck = new React.MapState<ChatMessageReaction>();
reactionsStop = new React.MapState<ChatMessageReaction>();
2026-04-06 20:47:49 +00:00
reactionsAttention = new React.MapState<ChatMessageReaction>();
reactionsDoubleAttention = new React.MapState<ChatMessageReaction>();
reactionsQuestion = new React.MapState<ChatMessageReaction>();
2026-04-07 18:39:55 +00:00
reactionsThumbsUpCount = React.createProxyState(
[this.reactionsThumbsUp],
() => this.reactionsThumbsUp.value.size,
);
reactionsCheckCount = React.createProxyState(
[this.reactionsCheck],
() => this.reactionsCheck.value.size,
);
reactionsStopCount = React.createProxyState(
[this.reactionsStop],
() => this.reactionsStop.value.size,
);
2026-04-07 18:39:55 +00:00
reactionsAttentionCount = React.createProxyState(
[this.reactionsAttention],
() => this.reactionsAttention.value.size,
);
reactionsDoubleAttentionCount = React.createProxyState(
[this.reactionsDoubleAttention],
() => this.reactionsDoubleAttention.value.size,
);
reactionsQuestionCount = React.createProxyState(
[this.reactionsQuestion],
() => this.reactionsQuestion.value.size,
);
2026-04-01 08:29:39 +00:00
// state
isPresentingInfoModal: React.State<boolean> = new React.State(false);
2024-07-21 01:12:35 +02:00
2026-04-01 08:29:39 +00:00
// methods
copyMessage = (): void => {
navigator.clipboard.writeText(this.body.value);
};
resendMessage = (): void => {
this.messagePageViewModel.sendMessageFromBody(this.body.value);
};
decryptMessage = (): void => {
this.messagePageViewModel.decryptMessage(this);
};
2024-07-21 13:13:41 +02:00
2026-04-01 08:29:39 +00:00
// view
showInfoModal = (): void => {
this.isPresentingInfoModal.value = true;
};
2024-07-23 13:09:46 +02:00
2026-04-01 08:29:39 +00:00
hideInfoModal = (): void => {
this.isPresentingInfoModal.value = false;
};
2024-07-23 13:09:46 +02:00
2026-04-06 20:47:49 +00:00
// reactions
2026-04-07 18:48:01 +00:00
handleReaction = (reaction: ChatMessageReaction): void => {
function setReaction(mapState: React.MapState<any>) {
if (reaction.isDeleting) {
mapState.remove(reaction.sender);
} else {
mapState.set(reaction.sender, reaction);
}
}
2026-04-06 20:47:49 +00:00
switch (reaction.content) {
2026-04-07 18:39:55 +00:00
case ReactionSymbols.ThumbsUp:
2026-04-07 18:48:01 +00:00
return setReaction(this.reactionsThumbsUp);
2026-04-07 18:39:55 +00:00
case ReactionSymbols.Check:
2026-04-07 18:48:01 +00:00
return setReaction(this.reactionsCheck);
case ReactionSymbols.Stop:
return setReaction(this.reactionsStop);
2026-04-07 18:39:55 +00:00
case ReactionSymbols.Attention:
2026-04-07 18:48:01 +00:00
return setReaction(this.reactionsAttention);
2026-04-07 18:39:55 +00:00
case ReactionSymbols.DoubleAttention:
2026-04-07 18:48:01 +00:00
return setReaction(this.reactionsDoubleAttention);
2026-04-07 18:39:55 +00:00
case ReactionSymbols.Question:
2026-04-07 18:48:01 +00:00
return setReaction(this.reactionsQuestion);
2026-04-06 20:47:49 +00:00
}
};
2026-04-07 18:48:01 +00:00
sendReaction = (content: ReactionSymbols, isDeleting: boolean): void => {
this.messagePageViewModel.sendReaction(
this.chatMessage.id,
content,
isDeleting,
);
2026-04-07 18:39:55 +00:00
};
2026-04-01 08:29:39 +00:00
// load
loadData = (): void => {
this.channel = this.chatMessage.channel;
this.sender = this.chatMessage.sender;
this.dateSent = new Date(this.chatMessage.dateSent).toLocaleString();
this.body.value = this.chatMessage.body;
this.status.value = this.chatMessage.status;
};
2024-07-21 13:13:41 +02:00
2026-04-01 08:29:39 +00:00
// init
constructor(
public coreViewModel: CoreViewModel,
2026-04-09 16:54:56 +00:00
public messagePageViewModel: MessagePageViewModel,
2026-04-01 08:29:39 +00:00
chatMessage: ChatMessage,
sentByUser: boolean,
) {
this.chatMessage = chatMessage;
this.sentByUser = sentByUser;
this.loadData();
}
2024-07-21 01:12:35 +02:00
}