fix: DM's not decrypting

fixes #629
This commit is contained in:
Kieran 2023-09-12 15:54:49 +01:00
parent e77b9928d0
commit 3ffcd1b2c5
Signed by: Kieran
GPG Key ID: DE71CEB3925BE941

View File

@ -1,6 +1,6 @@
import "./DM.css";
import { useEffect, useState } from "react";
import { useIntl } from "react-intl";
import { FormattedMessage, useIntl } from "react-intl";
import { useInView } from "react-intersection-observer";
import useEventPublisher from "Feed/EventPublisher";
@ -8,9 +8,9 @@ import NoteTime from "Element/NoteTime";
import Text from "Element/Text";
import useLogin from "Hooks/useLogin";
import { Chat, ChatMessage, ChatType, setLastReadIn } from "chat";
import ProfileImage from "./ProfileImage";
import messages from "./messages";
import ProfileImage from "./ProfileImage";
export interface DMProps {
chat: Chat;
@ -21,9 +21,8 @@ export default function DM(props: DMProps) {
const pubKey = useLogin().publicKey;
const publisher = useEventPublisher();
const msg = props.data;
const [content, setContent] = useState(msg.needsDecryption ? "Loading..." : msg.content);
const [decrypted, setDecrypted] = useState(false);
const { ref, inView } = useInView();
const [content, setContent] = useState<string>();
const { ref, inView } = useInView({ triggerOnce: true });
const { formatMessage } = useIntl();
const isMe = msg.from === pubKey;
const otherPubkey = isMe ? pubKey : msg.from;
@ -46,17 +45,20 @@ export default function DM(props: DMProps) {
}
useEffect(() => {
if (!decrypted && inView && msg.needsDecryption) {
setDecrypted(true);
if (inView) {
if (msg.needsDecryption) {
decrypt().catch(console.error);
} else {
setContent(msg.content);
}
}, [inView, msg]);
}
}, [inView]);
return (
<div className={isMe ? "dm me" : "dm other"} ref={ref}>
<div>
{sender()}
<Text id={msg.id} content={content} tags={[]} creator={otherPubkey} />
{content ? <Text id={msg.id} content={content} tags={[]} creator={otherPubkey} /> : <FormattedMessage defaultMessage="Loading..." />}
</div>
<div>
<NoteTime from={msg.created_at * 1000} fallback={formatMessage(messages.JustNow)} />