refactor: thread loading improvements

This commit is contained in:
2024-04-04 16:30:30 +01:00
parent ad2b6dbcf7
commit ee31726961
29 changed files with 272 additions and 972 deletions

View File

@ -2,13 +2,12 @@
import { TaggedNostrEvent } from "@snort/system";
import { createContext } from "react";
interface ThreadContext {
export interface ThreadContextState {
current: string;
root?: TaggedNostrEvent;
chains: Map<string, Array<TaggedNostrEvent>>;
data: Array<TaggedNostrEvent>;
reactions: Array<TaggedNostrEvent>;
setCurrent: (i: string) => void;
}
export const ThreadContext = createContext({} as ThreadContext);
export const ThreadContext = createContext({} as ThreadContextState);

View File

@ -6,7 +6,7 @@ import { useLocation } from "react-router-dom";
import useThreadFeed from "@/Feed/ThreadFeed";
import useModeration from "@/Hooks/useModeration";
import { chainKey, replyChainKey } from "@/Utils/Thread/ChainKey";
import { ThreadContext } from "@/Utils/Thread/ThreadContext";
import { ThreadContext, ThreadContextState } from "@/Utils/Thread/ThreadContext";
export function ThreadContextWrapper({ link, children }: { link: NostrLink; children?: ReactNode }) {
const location = useLocation();
@ -16,8 +16,8 @@ export function ThreadContextWrapper({ link, children }: { link: NostrLink; chil
const chains = useMemo(() => {
const chains = new Map<u256, Array<TaggedNostrEvent>>();
if (feed.thread) {
feed.thread
if (feed) {
feed
?.filter(a => !isBlocked(a.pubkey))
.forEach(v => {
const replyTo = replyChainKey(v);
@ -31,30 +31,29 @@ export function ThreadContextWrapper({ link, children }: { link: NostrLink; chil
});
}
return chains;
}, [feed.thread]);
}, [feed]);
// Root is the parent of the current note or the current note if its a root note or the root of the thread
const root = useMemo(() => {
const currentNote =
feed.thread?.find(a => chainKey(a) === currentId) ??
feed?.find(a => chainKey(a) === currentId) ??
(location.state && "sig" in location.state ? (location.state as TaggedNostrEvent) : undefined);
if (currentNote) {
const key = replyChainKey(currentNote);
if (key) {
return feed.thread?.find(a => chainKey(a) === key);
return feed?.find(a => chainKey(a) === key);
} else {
return currentNote;
}
}
}, [feed.thread.length, currentId, location]);
}, [feed.length, currentId, location]);
const ctxValue = useMemo<ThreadContext>(() => {
const ctxValue = useMemo<ThreadContextState>(() => {
return {
current: currentId,
root,
chains,
reactions: feed.reactions,
data: feed.thread,
data: feed,
setCurrent: v => setCurrentId(v),
};
}, [root, chains]);