snort/src/element/DM.tsx

49 lines
1.5 KiB
TypeScript
Raw Normal View History

2023-01-12 09:48:39 +00:00
import "./DM.css";
import { useEffect, useState } from "react";
import { useSelector } from "react-redux";
2023-01-14 11:50:06 +00:00
import { useInView } from 'react-intersection-observer';
2023-01-12 09:48:39 +00:00
import useEventPublisher from "../feed/EventPublisher";
import Event from "../nostr/Event";
import NoteTime from "./NoteTime";
2023-01-14 14:37:31 +00:00
import Text from "./Text";
2023-01-17 10:47:00 +00:00
import { lastReadDm, setLastReadDm } from "../pages/MessagesPage";
2023-01-12 09:48:39 +00:00
export type DMProps = {
data: any
}
export default function DM(props: DMProps) {
const pubKey = useSelector<any>(s => s.login.publicKey);
const publisher = useEventPublisher();
const [content, setContent] = useState("Loading...");
2023-01-14 11:50:06 +00:00
const [decrypted, setDecrypted] = useState(false);
const { ref, inView, entry } = useInView();
2023-01-17 10:47:00 +00:00
const isMe = props.data.pubkey === pubKey;
2023-01-12 09:48:39 +00:00
async function decrypt() {
2023-01-15 19:40:47 +00:00
let e = new Event(props.data);
2023-01-17 10:47:00 +00:00
if (!isMe) {
setLastReadDm(e.PubKey);
}
2023-01-12 09:48:39 +00:00
let decrypted = await publisher.decryptDm(e);
2023-01-15 19:40:47 +00:00
setContent(decrypted || "<ERROR>");
2023-01-12 09:48:39 +00:00
}
useEffect(() => {
2023-01-14 11:50:06 +00:00
if (!decrypted && inView) {
setDecrypted(true);
decrypt().catch(console.error);
}
}, [inView, props.data]);
2023-01-12 09:48:39 +00:00
return (
2023-01-17 10:47:00 +00:00
<div className={`flex dm f-col${isMe ? " me" : ""}`} ref={ref}>
2023-01-16 10:38:30 +00:00
<div><NoteTime from={props.data.created_at * 1000} fallback={'Just now'} /></div>
2023-01-12 09:48:39 +00:00
<div className="w-max">
2023-01-16 17:48:25 +00:00
<Text content={content} tags={[]} users={new Map()} />
2023-01-12 09:48:39 +00:00
</div>
</div>
)
2023-01-14 14:37:31 +00:00
}