import "./DM.css"; import { useEffect, useState } from "react"; import { useInView } from "react-intersection-observer"; import { FormattedMessage, useIntl } from "react-intl"; import { Chat, ChatMessage, ChatType, setLastReadIn } from "@/chat"; import NoteTime from "@/Components/Event/Note/NoteTime"; import messages from "@/Components/messages"; import Text from "@/Components/Text/Text"; import ProfileImage from "@/Components/User/ProfileImage"; import useEventPublisher from "@/Hooks/useEventPublisher"; import useLogin from "@/Hooks/useLogin"; export interface DMProps { chat: Chat; data: ChatMessage; } export default function DM(props: DMProps) { const { publicKey } = useLogin(s => ({ publicKey: s.publicKey })); const { publisher } = useEventPublisher(); const msg = props.data; const [content, setContent] = useState(); const { ref, inView } = useInView({ triggerOnce: true }); const { formatMessage } = useIntl(); const isMe = msg.from === publicKey; const otherPubkey = isMe ? publicKey : msg.from; async function decrypt() { if (publisher) { const decrypted = await msg.decrypt(publisher); setContent(decrypted || ""); if (!isMe) { setLastReadIn(msg.id); } } } function sender() { const isGroup = props.chat.type === ChatType.PrivateGroupChat || props.chat.type === ChatType.PublicGroupChat; if (isGroup && !isMe) { return ; } } useEffect(() => { if (inView) { if (msg.needsDecryption) { decrypt().catch(console.error); } else { setContent(msg.content); } } }, [inView]); return (
{sender()} {content ? ( ) : ( )}
); }