import { Message } from "@chatscope/chat-ui-kit-react";
import React, { useEffect } from "react";
import { useInView } from "react-intersection-observer";
import { MessageDisplay } from "./MessageDisplay";
import { Avatar, Box, ButtonBase, Typography } from "@mui/material";
import { formatTimestamp } from "../../utils/time";
import { getBaseApi } from "../../background";
import { getBaseApiReact } from "../../App";
import { generateHTML } from "@tiptap/react";
import Highlight from "@tiptap/extension-highlight";
import StarterKit from "@tiptap/starter-kit";
import Underline from "@tiptap/extension-underline";
import { executeEvent } from "../../utils/events";
import { WrapperUserAction } from "../WrapperUserAction";
import ReplyIcon from "@mui/icons-material/Reply";
import { Spacer } from "../../common/Spacer";
import { ReactionPicker } from "../ReactionPicker";
export const MessageItem = ({
message,
onSeen,
isLast,
isTemp,
myAddress,
onReply,
isShowingAsReply,
reply,
replyIndex,
scrollToItem,
handleReaction,
reactions,
isUpdating
}) => {
const { ref, inView } = useInView({
threshold: 0.7, // Fully visible
triggerOnce: true, // Only trigger once when it becomes visible
});
useEffect(() => {
if (inView && message.unread) {
onSeen(message.id);
}
}, [inView, message.id, message.unread, onSeen]);
return (
{isShowingAsReply ? (
) : (
{message?.senderName?.charAt(0)}
)}
{message?.senderName || message?.sender}
{!isShowingAsReply && (
{
onReply(message);
}}
>
)}
{!isShowingAsReply && handleReaction && (
{
if(reactions && reactions[val] && reactions[val]?.find((item)=> item?.sender === myAddress)){
handleReaction(val, message, false)
} else {
handleReaction(val, message, true)
}
}} />
)}
{reply && (
<>
{
scrollToItem(replyIndex)
}}
>
Replied to {reply?.senderName || reply?.senderAddress}
{reply?.messageText && (
)}
{reply?.decryptedData?.type === "notification" ? (
) : (
)}
>
)}
{message?.messageText && (
)}
{message?.decryptedData?.type === "notification" ? (
) : (
)}
{reactions && Object.keys(reactions).map((reaction)=> {
const numberOfReactions = reactions[reaction]?.length
// const myReaction = reactions
if(numberOfReactions === 0) return null
return (
{
if(reactions[reaction] && reactions[reaction]?.find((item)=> item?.sender === myAddress)){
handleReaction(reaction, message, false)
} else {
handleReaction(reaction, message, true)
}
}}>
{reaction}
)
})}
{isUpdating ? (
Updating...
) : isTemp ? (
Sending...
) : (
{formatTimestamp(message.timestamp)}
)}
{/*
*/}
{/* {!message.unread &&
Seen} */}
);
};
export const ReplyPreview = ({message})=> {
return (
Replied to {message?.senderName || message?.senderAddress}
{message?.messageText && (
)}
{message?.decryptedData?.type === "notification" ? (
) : (
)}
)
}