snort/packages/app/src/Element/DM.tsx

60 lines
1.9 KiB
TypeScript
Raw Normal View History

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";
import { useInView } from "react-intersection-observer";
2023-03-28 14:34:01 +00:00
import { HexKey, TaggedRawEvent } from "@snort/nostr";
2023-01-12 09:48:39 +00:00
2023-01-20 11:11:50 +00:00
import useEventPublisher from "Feed/EventPublisher";
import NoteTime from "Element/NoteTime";
import Text from "Element/Text";
import { setLastReadDm } from "Pages/MessagesPage";
import { RootState } from "State/Store";
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 = {
data: TaggedRawEvent;
};
2023-01-12 09:48:39 +00:00
export default function DM(props: DMProps) {
const dispatch = useDispatch();
2023-02-09 12:26:54 +00:00
const pubKey = useSelector<RootState, HexKey | undefined>(s => s.login.publicKey);
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();
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
async function decrypt() {
2023-03-28 14:34:01 +00:00
const decrypted = await publisher.decryptDm(props.data);
setContent(decrypted || "<ERROR>");
if (!isMe) {
2023-03-28 14:34:01 +00:00
setLastReadDm(props.data.pubkey);
dispatch(incDmInteraction());
2023-01-12 09:48:39 +00:00
}
}
2023-01-12 09:48:39 +00:00
useEffect(() => {
if (!decrypted && inView) {
setDecrypted(true);
decrypt().catch(console.error);
}
}, [inView, props.data]);
2023-01-12 09:48:39 +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)} />
</div>
<div className="w-max">
2023-03-03 14:30:31 +00:00
<Text content={content} tags={[]} creator={otherPubkey} />
</div>
</div>
);
2023-01-14 14:37:31 +00:00
}