snort/src/pages/MessagesPage.tsx

90 lines
2.8 KiB
TypeScript
Raw Normal View History

2023-01-12 09:48:39 +00:00
import { useMemo } from "react";
import { useSelector } from "react-redux"
2023-01-17 10:47:00 +00:00
import { HexKey, RawEvent } from "../nostr";
2023-01-12 09:48:39 +00:00
import ProfileImage from "../element/ProfileImage";
import { hexToBech32 } from "../Util";
2023-01-17 10:47:00 +00:00
type DmChat = {
pubkey: HexKey,
unreadMessages: number,
newestMessage: number
}
2023-01-12 09:48:39 +00:00
export default function MessagesPage() {
2023-01-17 10:47:00 +00:00
const myPubKey = useSelector<any, string>(s => s.login.publicKey);
2023-01-12 09:48:39 +00:00
const dms = useSelector<any, RawEvent[]>(s => s.login.dms);
2023-01-17 10:47:00 +00:00
const chats = useMemo(() => {
return extractChats(dms, myPubKey);
2023-01-12 09:48:39 +00:00
}, [dms]);
2023-01-17 10:47:00 +00:00
function person(chat: DmChat) {
2023-01-12 09:48:39 +00:00
return (
2023-01-17 10:47:00 +00:00
<div className="flex mb10" key={chat.pubkey}>
<ProfileImage pubkey={chat.pubkey} className="f-grow" link={`/messages/${hexToBech32("npub", chat.pubkey)}`} />
2023-01-12 09:48:39 +00:00
<span className="pill">
2023-01-17 10:47:00 +00:00
{chat.unreadMessages}
2023-01-12 09:48:39 +00:00
</span>
</div>
)
}
return (
<>
<h3>Messages</h3>
2023-01-17 10:47:00 +00:00
{chats.sort((a, b) => b.newestMessage - a.newestMessage).map(person)}
2023-01-12 09:48:39 +00:00
</>
)
2023-01-17 10:47:00 +00:00
}
export function lastReadDm(pk: HexKey) {
let k = `dm:seen:${pk}`;
return parseInt(window.localStorage.getItem(k) ?? "0");
}
export function setLastReadDm(pk: HexKey) {
const now = Math.floor(new Date().getTime() / 1000);
let current = lastReadDm(pk);
if (current >= now) {
return;
}
let k = `dm:seen:${pk}`;
window.localStorage.setItem(k, now.toString());
}
2023-01-17 11:30:42 +00:00
export function isToSelf(e: RawEvent, pk: HexKey) {
return e.pubkey === pk && e.tags.some(a => a[0] === "p" && a[1] === pk);
}
export function dmsInChat(dms: RawEvent[], pk: HexKey) {
return dms.filter(a => a.pubkey === pk || a.tags.some(b => b[0] === "p" && b[1] === pk));
}
2023-01-17 10:47:00 +00:00
export function totalUnread(dms: RawEvent[], myPubKey: HexKey) {
return extractChats(dms, myPubKey).reduce((acc, v) => acc += v.unreadMessages, 0);
}
function unreadDms(dms: RawEvent[], myPubKey: HexKey, pk: HexKey) {
2023-01-17 11:30:42 +00:00
if (pk === myPubKey) return 0;
2023-01-17 10:47:00 +00:00
let lastRead = lastReadDm(pk);
2023-01-17 11:30:42 +00:00
return dmsInChat(dms, pk).filter(a => a.created_at >= lastRead && a.pubkey !== myPubKey).length;
2023-01-17 10:47:00 +00:00
}
function newestMessage(dms: RawEvent[], myPubKey: HexKey, pk: HexKey) {
2023-01-17 11:30:42 +00:00
return dmsInChat(dms, pk).reduce((acc, v) => acc = v.created_at > acc ? v.created_at : acc, 0);
2023-01-17 10:47:00 +00:00
}
export function extractChats(dms: RawEvent[], myPubKey: HexKey) {
const keys = dms.map(a => [a.pubkey, ...a.tags.filter(b => b[0] === "p").map(b => b[1])]).flat();
const filteredKeys = Array.from(new Set<string>(keys));
return filteredKeys.map(a => {
return {
pubkey: a,
unreadMessages: unreadDms(dms, myPubKey, a),
newestMessage: newestMessage(dms, myPubKey, a)
} as DmChat;
})
2023-01-12 09:48:39 +00:00
}