forked from Kieran/snort
bug: ordering chats
This commit is contained in:
parent
564d705f08
commit
1372c266c6
@ -10,6 +10,7 @@ import useEventPublisher from "../feed/EventPublisher";
|
|||||||
|
|
||||||
import DM from "../element/DM";
|
import DM from "../element/DM";
|
||||||
import { RawEvent } from "../nostr";
|
import { RawEvent } from "../nostr";
|
||||||
|
import { dmsInChat } from "./MessagesPage";
|
||||||
|
|
||||||
type RouterParams = {
|
type RouterParams = {
|
||||||
id: string
|
id: string
|
||||||
@ -19,20 +20,13 @@ export default function ChatPage() {
|
|||||||
const params = useParams<RouterParams>();
|
const params = useParams<RouterParams>();
|
||||||
const publisher = useEventPublisher();
|
const publisher = useEventPublisher();
|
||||||
const id = bech32ToHex(params.id ?? "");
|
const id = bech32ToHex(params.id ?? "");
|
||||||
const dms = useSelector<any, RawEvent[]>(s => filterDms(s.login.dms, s.login.publicKey));
|
const dms = useSelector<any, RawEvent[]>(s => filterDms(s.login.dms));
|
||||||
const [content, setContent] = useState<string>();
|
const [content, setContent] = useState<string>();
|
||||||
const { ref, inView, entry } = useInView();
|
const { ref, inView, entry } = useInView();
|
||||||
const dmListRef = useRef<HTMLDivElement>(null);
|
const dmListRef = useRef<HTMLDivElement>(null);
|
||||||
|
|
||||||
function filterDms(dms: RawEvent[], myPubkey: string) {
|
function filterDms(dms: RawEvent[]) {
|
||||||
return dms.filter(a => {
|
return dmsInChat(dms, id);
|
||||||
if (a.pubkey === myPubkey && a.tags.some(b => b[0] === "p" && b[1] === id)) {
|
|
||||||
return true;
|
|
||||||
} else if (a.pubkey === id && a.tags.some(b => b[0] === "p" && b[1] === myPubkey)) {
|
|
||||||
return true;
|
|
||||||
}
|
|
||||||
return false;
|
|
||||||
});
|
|
||||||
}
|
}
|
||||||
|
|
||||||
const sortedDms = useMemo<any[]>(() => {
|
const sortedDms = useMemo<any[]>(() => {
|
||||||
|
@ -7,7 +7,6 @@ import { hexToBech32 } from "../Util";
|
|||||||
|
|
||||||
type DmChat = {
|
type DmChat = {
|
||||||
pubkey: HexKey,
|
pubkey: HexKey,
|
||||||
lastRead: number,
|
|
||||||
unreadMessages: number,
|
unreadMessages: number,
|
||||||
newestMessage: number
|
newestMessage: number
|
||||||
}
|
}
|
||||||
@ -55,17 +54,26 @@ export function setLastReadDm(pk: HexKey) {
|
|||||||
window.localStorage.setItem(k, now.toString());
|
window.localStorage.setItem(k, now.toString());
|
||||||
}
|
}
|
||||||
|
|
||||||
|
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));
|
||||||
|
}
|
||||||
|
|
||||||
export function totalUnread(dms: RawEvent[], myPubKey: HexKey) {
|
export function totalUnread(dms: RawEvent[], myPubKey: HexKey) {
|
||||||
return extractChats(dms, myPubKey).reduce((acc, v) => acc += v.unreadMessages, 0);
|
return extractChats(dms, myPubKey).reduce((acc, v) => acc += v.unreadMessages, 0);
|
||||||
}
|
}
|
||||||
|
|
||||||
function unreadDms(dms: RawEvent[], myPubKey: HexKey, pk: HexKey) {
|
function unreadDms(dms: RawEvent[], myPubKey: HexKey, pk: HexKey) {
|
||||||
|
if (pk === myPubKey) return 0;
|
||||||
let lastRead = lastReadDm(pk);
|
let lastRead = lastReadDm(pk);
|
||||||
return dms?.filter(a => a.pubkey === pk && a.pubkey !== myPubKey && a.created_at >= lastRead).length;
|
return dmsInChat(dms, pk).filter(a => a.created_at >= lastRead && a.pubkey !== myPubKey).length;
|
||||||
}
|
}
|
||||||
|
|
||||||
function newestMessage(dms: RawEvent[], myPubKey: HexKey, pk: HexKey) {
|
function newestMessage(dms: RawEvent[], myPubKey: HexKey, pk: HexKey) {
|
||||||
return dms.filter(a => a.pubkey === pk && a.pubkey !== myPubKey).reduce((acc, v) => acc = v.created_at > acc ? v.created_at : acc, 0);
|
return dmsInChat(dms, pk).reduce((acc, v) => acc = v.created_at > acc ? v.created_at : acc, 0);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
@ -75,7 +83,6 @@ export function extractChats(dms: RawEvent[], myPubKey: HexKey) {
|
|||||||
return filteredKeys.map(a => {
|
return filteredKeys.map(a => {
|
||||||
return {
|
return {
|
||||||
pubkey: a,
|
pubkey: a,
|
||||||
lastRead: lastReadDm(a),
|
|
||||||
unreadMessages: unreadDms(dms, myPubKey, a),
|
unreadMessages: unreadDms(dms, myPubKey, a),
|
||||||
newestMessage: newestMessage(dms, myPubKey, a)
|
newestMessage: newestMessage(dms, myPubKey, a)
|
||||||
} as DmChat;
|
} as DmChat;
|
||||||
|
Loading…
Reference in New Issue
Block a user