snort/src/Pages/ChatPage.tsx

82 lines
2.7 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-14 11:50:06 +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 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";
import { RawEvent } from "Nostr";
import { dmsInChat, isToSelf } from "Pages/MessagesPage";
import NoteToSelf from "Element/NoteToSelf";
2023-01-12 09:48:39 +00:00
type RouterParams = {
id: string
}
export default function ChatPage() {
const params = useParams<RouterParams>();
const publisher = useEventPublisher();
2023-01-15 22:59:05 +00:00
const id = bech32ToHex(params.id ?? "");
2023-01-18 23:49:37 +00:00
const pubKey = useSelector<any>(s => s.login.publicKey);
2023-01-17 11:30:42 +00:00
const dms = useSelector<any, RawEvent[]>(s => filterDms(s.login.dms));
2023-01-12 09:48:39 +00:00
const [content, setContent] = useState<string>();
2023-01-14 11:50:06 +00:00
const { ref, inView, entry } = useInView();
const dmListRef = useRef<HTMLDivElement>(null);
2023-01-12 09:48:39 +00:00
2023-01-17 11:30:42 +00:00
function filterDms(dms: RawEvent[]) {
2023-01-18 23:49:37 +00:00
return dmsInChat(id === pubKey ? dms.filter(d => isToSelf(d, pubKey)) : dms, id);
2023-01-12 09:48:39 +00:00
}
const sortedDms = useMemo<any[]>(() => {
return [...dms].sort((a, b) => a.created_at - b.created_at)
}, [dms]);
2023-01-14 11:50:06 +00:00
useEffect(() => {
if (inView && dmListRef.current) {
dmListRef.current.scroll(0, dmListRef.current.scrollHeight);
}
}, [inView, dmListRef, sortedDms]);
2023-01-12 09:48:39 +00:00
async function sendDm() {
2023-01-15 22:59:05 +00:00
if (content) {
let ev = await publisher.sendDm(content, id);
console.debug(ev);
publisher.broadcast(ev);
2023-01-17 10:53:49 +00:00
setContent("");
2023-01-15 22:59:05 +00:00
}
2023-01-14 11:50:06 +00:00
}
async function onEnter(e: KeyboardEvent) {
let isEnter = e.code === "Enter";
2023-01-15 22:59:05 +00:00
if (isEnter && !e.shiftKey) {
2023-01-14 11:50:06 +00:00
await sendDm();
}
2023-01-12 09:48:39 +00:00
}
return (
<>
{id === pubKey && (
<NoteToSelf className="f-grow mb-10" pubkey={id} />
) || (
<ProfileImage pubkey={id} className="f-grow mb10" />
)}
2023-01-14 11:50:06 +00:00
<div className="dm-list" ref={dmListRef}>
2023-01-12 09:48:39 +00:00
<div>
2023-01-14 11:50:06 +00:00
{sortedDms.map(a => <DM data={a} key={a.id} />)}
<div ref={ref} className="mb10"></div>
2023-01-12 09:48:39 +00:00
</div>
</div>
<div className="write-dm">
<div className="inner">
2023-01-14 11:50:06 +00:00
<textarea className="f-grow mr10" value={content} onChange={(e) => setContent(e.target.value)} onKeyDown={(e) => onEnter(e)}></textarea>
2023-01-25 18:08:53 +00:00
<button type="button" onClick={() => sendDm()}>Send</button>
2023-01-12 09:48:39 +00:00
</div>
</div>
</>
)
2023-01-25 18:08:53 +00:00
}