2023-01-12 09:48:39 +00:00
|
|
|
import "./DM.css";
|
|
|
|
import { useEffect, useState } from "react";
|
2023-01-18 22:04:52 +00:00
|
|
|
import { useDispatch, useSelector } from "react-redux";
|
2023-02-08 21:10:26 +00:00
|
|
|
import { useIntl } from "react-intl";
|
2023-02-07 20:04:50 +00:00
|
|
|
import { useInView } from "react-intersection-observer";
|
2023-01-12 09:48:39 +00:00
|
|
|
|
2023-01-20 11:11:50 +00:00
|
|
|
import useEventPublisher from "Feed/EventPublisher";
|
|
|
|
import Event from "Nostr/Event";
|
|
|
|
import NoteTime from "Element/NoteTime";
|
|
|
|
import Text from "Element/Text";
|
|
|
|
import { setLastReadDm } from "Pages/MessagesPage";
|
|
|
|
import { RootState } from "State/Store";
|
|
|
|
import { HexKey, TaggedRawEvent } from "Nostr";
|
|
|
|
import { incDmInteraction } from "State/Login";
|
2023-02-07 19:47:57 +00:00
|
|
|
import { unwrap } from "Util";
|
2023-01-12 09:48:39 +00:00
|
|
|
|
2023-02-08 21:10:26 +00:00
|
|
|
import messages from "./messages";
|
|
|
|
|
2023-01-12 09:48:39 +00:00
|
|
|
export type DMProps = {
|
2023-02-07 20:04:50 +00:00
|
|
|
data: TaggedRawEvent;
|
|
|
|
};
|
2023-01-12 09:48:39 +00:00
|
|
|
|
|
|
|
export default function DM(props: DMProps) {
|
2023-02-07 20:04:50 +00:00
|
|
|
const dispatch = useDispatch();
|
2023-02-09 12:26:54 +00:00
|
|
|
const pubKey = useSelector<RootState, HexKey | undefined>(s => s.login.publicKey);
|
2023-02-07 20:04:50 +00:00
|
|
|
const publisher = useEventPublisher();
|
|
|
|
const [content, setContent] = useState("Loading...");
|
|
|
|
const [decrypted, setDecrypted] = useState(false);
|
|
|
|
const { ref, inView } = useInView();
|
2023-02-08 21:10:26 +00:00
|
|
|
const { formatMessage } = useIntl();
|
2023-02-07 20:04:50 +00:00
|
|
|
const isMe = props.data.pubkey === pubKey;
|
2023-02-09 12:26:54 +00:00
|
|
|
const otherPubkey = isMe ? pubKey : unwrap(props.data.tags.find(a => a[0] === "p")?.[1]);
|
2023-01-12 09:48:39 +00:00
|
|
|
|
2023-02-07 20:04:50 +00:00
|
|
|
async function decrypt() {
|
2023-02-07 19:47:57 +00:00
|
|
|
const e = new Event(props.data);
|
|
|
|
const decrypted = await publisher.decryptDm(e);
|
2023-02-07 20:04:50 +00:00
|
|
|
setContent(decrypted || "<ERROR>");
|
|
|
|
if (!isMe) {
|
|
|
|
setLastReadDm(e.PubKey);
|
|
|
|
dispatch(incDmInteraction());
|
2023-01-12 09:48:39 +00:00
|
|
|
}
|
2023-02-07 20:04:50 +00:00
|
|
|
}
|
2023-01-12 09:48:39 +00:00
|
|
|
|
2023-02-07 20:04:50 +00:00
|
|
|
useEffect(() => {
|
|
|
|
if (!decrypted && inView) {
|
|
|
|
setDecrypted(true);
|
|
|
|
decrypt().catch(console.error);
|
|
|
|
}
|
|
|
|
}, [inView, props.data]);
|
2023-01-12 09:48:39 +00:00
|
|
|
|
2023-02-07 20:04:50 +00:00
|
|
|
return (
|
|
|
|
<div className={`flex dm f-col${isMe ? " me" : ""}`} ref={ref}>
|
|
|
|
<div>
|
2023-02-09 12:26:54 +00:00
|
|
|
<NoteTime from={props.data.created_at * 1000} fallback={formatMessage(messages.JustNow)} />
|
2023-02-07 20:04:50 +00:00
|
|
|
</div>
|
|
|
|
<div className="w-max">
|
2023-02-09 12:26:54 +00:00
|
|
|
<Text content={content} tags={[]} users={new Map()} creator={otherPubkey} />
|
2023-02-07 20:04:50 +00:00
|
|
|
</div>
|
|
|
|
</div>
|
|
|
|
);
|
2023-01-14 14:37:31 +00:00
|
|
|
}
|