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