snort/packages/app/src/Pages/ChatPage.tsx

88 lines
2.6 KiB
TypeScript
Raw Normal View History

2023-01-12 09:48:39 +00:00
import "./ChatPage.css";
2023-01-14 11:50:06 +00:00
import { KeyboardEvent, useEffect, useMemo, useRef, useState } from "react";
2023-01-12 09:48:39 +00:00
import { useSelector } from "react-redux";
import { useParams } from "react-router-dom";
2023-01-20 11:11:50 +00:00
import ProfileImage from "Element/ProfileImage";
import { bech32ToHex } from "Util";
import useEventPublisher from "Feed/EventPublisher";
2023-01-12 09:48:39 +00:00
2023-01-20 11:11:50 +00:00
import DM from "Element/DM";
2023-03-29 12:10:22 +00:00
import { RawEvent, TaggedRawEvent } from "@snort/nostr";
2023-01-20 11:11:50 +00:00
import { dmsInChat, isToSelf } from "Pages/MessagesPage";
import NoteToSelf from "Element/NoteToSelf";
2023-02-09 12:26:54 +00:00
import { RootState } from "State/Store";
2023-02-11 18:26:51 +00:00
import { FormattedMessage } from "react-intl";
2023-03-29 12:10:22 +00:00
import { useDmCache } from "Hooks/useDmsCache";
2023-01-12 09:48:39 +00:00
type RouterParams = {
id: string;
};
2023-01-12 09:48:39 +00:00
export default function ChatPage() {
const params = useParams<RouterParams>();
const publisher = useEventPublisher();
const id = bech32ToHex(params.id ?? "");
2023-02-09 12:26:54 +00:00
const pubKey = useSelector((s: RootState) => s.login.publicKey);
const [content, setContent] = useState<string>();
const dmListRef = useRef<HTMLDivElement>(null);
2023-03-29 12:10:22 +00:00
const dms = filterDms(useDmCache());
2023-01-12 09:48:39 +00:00
2023-03-29 12:10:22 +00:00
function filterDms(dms: readonly RawEvent[]) {
2023-02-09 12:26:54 +00:00
return dmsInChat(id === pubKey ? dms.filter(d => isToSelf(d, pubKey)) : dms, id);
}
2023-01-12 09:48:39 +00:00
2023-02-09 12:26:54 +00:00
const sortedDms = useMemo(() => {
return [...dms].sort((a, b) => a.created_at - b.created_at);
}, [dms]);
2023-01-12 09:48:39 +00:00
useEffect(() => {
2023-02-10 20:53:09 +00:00
if (dmListRef.current) {
dmListRef.current.scroll(0, dmListRef.current.scrollHeight);
}
2023-02-10 20:53:09 +00:00
}, [dmListRef.current?.scrollHeight]);
2023-01-14 11:50:06 +00:00
async function sendDm() {
if (content) {
2023-02-07 19:47:57 +00:00
const ev = await publisher.sendDm(content, id);
console.debug(ev);
publisher.broadcast(ev);
setContent("");
2023-01-14 11:50:06 +00:00
}
}
2023-01-14 11:50:06 +00:00
async function onEnter(e: KeyboardEvent) {
2023-02-07 19:47:57 +00:00
const isEnter = e.code === "Enter";
if (isEnter && !e.shiftKey) {
await sendDm();
2023-01-12 09:48:39 +00:00
}
}
2023-01-12 09:48:39 +00:00
return (
<>
2023-02-09 12:26:54 +00:00
{(id === pubKey && <NoteToSelf className="f-grow mb-10" pubkey={id} />) || (
<ProfileImage pubkey={id} className="f-grow mb10" />
)}
<div className="dm-list" ref={dmListRef}>
<div>
2023-02-09 12:26:54 +00:00
{sortedDms.map(a => (
2023-02-07 19:47:57 +00:00
<DM data={a as TaggedRawEvent} key={a.id} />
))}
</div>
</div>
<div className="write-dm">
<div className="inner">
<textarea
className="f-grow mr10"
value={content}
2023-02-09 12:26:54 +00:00
onChange={e => setContent(e.target.value)}
onKeyDown={e => onEnter(e)}></textarea>
<button type="button" onClick={() => sendDm()}>
2023-02-11 18:26:51 +00:00
<FormattedMessage defaultMessage="Send" description="Send DM button" />
</button>
</div>
</div>
</>
);
2023-01-25 18:08:53 +00:00
}