fix the rest of warnings
This commit is contained in:
@ -164,4 +164,4 @@ export const MaxAboutLength = 1000;
|
||||
/*
|
||||
* Snort backend publishes rates
|
||||
*/
|
||||
export const SnortPubkey = "npub1sn0rtcjcf543gj4wsg7fa59s700d5ztys5ctj0g69g2x6802npjqhjjtws";
|
||||
export const SnortPubkey = "npub1sn0rtcjcf543gj4wsg7fa59s700d5ztys5ctj0g69g2x6802npjqhjjtws";
|
||||
|
18
packages/app/src/Utils/Thread/ChainKey.tsx
Normal file
18
packages/app/src/Utils/Thread/ChainKey.tsx
Normal file
@ -0,0 +1,18 @@
|
||||
import { unwrap } from "@snort/shared";
|
||||
import { EventExt, NostrLink, TaggedNostrEvent } from "@snort/system";
|
||||
|
||||
/**
|
||||
* Get the chain key as a reply event
|
||||
*/
|
||||
export function replyChainKey(ev: TaggedNostrEvent) {
|
||||
const t = EventExt.extractThread(ev);
|
||||
return t?.replyTo?.value ?? t?.root?.value;
|
||||
}
|
||||
|
||||
/**
|
||||
* Get the chain key of this event
|
||||
*/
|
||||
export function chainKey(ev: TaggedNostrEvent) {
|
||||
const link = NostrLink.fromEvent(ev);
|
||||
return unwrap(link.toEventTag())[1];
|
||||
}
|
14
packages/app/src/Utils/Thread/ThreadContext.tsx
Normal file
14
packages/app/src/Utils/Thread/ThreadContext.tsx
Normal file
@ -0,0 +1,14 @@
|
||||
/* eslint-disable no-debugger */
|
||||
import { TaggedNostrEvent } from "@snort/system";
|
||||
import { createContext } from "react";
|
||||
|
||||
interface ThreadContext {
|
||||
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);
|
63
packages/app/src/Utils/Thread/ThreadContextWrapper.tsx
Normal file
63
packages/app/src/Utils/Thread/ThreadContextWrapper.tsx
Normal file
@ -0,0 +1,63 @@
|
||||
import { unwrap } from "@snort/shared";
|
||||
import { NostrLink, TaggedNostrEvent, u256 } from "@snort/system";
|
||||
import { ReactNode, useMemo, useState } from "react";
|
||||
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";
|
||||
|
||||
export function ThreadContextWrapper({ link, children }: { link: NostrLink; children?: ReactNode }) {
|
||||
const location = useLocation();
|
||||
const [currentId, setCurrentId] = useState(unwrap(link.toEventTag())[1]);
|
||||
const feed = useThreadFeed(link);
|
||||
const { isBlocked } = useModeration();
|
||||
|
||||
const chains = useMemo(() => {
|
||||
const chains = new Map<u256, Array<TaggedNostrEvent>>();
|
||||
if (feed.thread) {
|
||||
feed.thread
|
||||
?.filter(a => !isBlocked(a.pubkey))
|
||||
.forEach(v => {
|
||||
const replyTo = replyChainKey(v);
|
||||
if (replyTo) {
|
||||
if (!chains.has(replyTo)) {
|
||||
chains.set(replyTo, [v]);
|
||||
} else {
|
||||
unwrap(chains.get(replyTo)).push(v);
|
||||
}
|
||||
}
|
||||
});
|
||||
}
|
||||
return chains;
|
||||
}, [feed.thread]);
|
||||
|
||||
// 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) ??
|
||||
(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);
|
||||
} else {
|
||||
return currentNote;
|
||||
}
|
||||
}
|
||||
}, [feed.thread.length, currentId, location]);
|
||||
|
||||
const ctxValue = useMemo<ThreadContext>(() => {
|
||||
return {
|
||||
current: currentId,
|
||||
root,
|
||||
chains,
|
||||
reactions: feed.reactions,
|
||||
data: feed.thread,
|
||||
setCurrent: v => setCurrentId(v),
|
||||
};
|
||||
}, [root, chains]);
|
||||
|
||||
return <ThreadContext.Provider value={ctxValue}>{children}</ThreadContext.Provider>;
|
||||
}
|
Reference in New Issue
Block a user