import { NoteMetadata } from '@components/note/metadata'; import { RelayContext } from '@components/relaysProvider'; import { UserExtend } from '@components/user/extend'; import { contentParser } from '@utils/parser'; import { getParentID } from '@utils/transform'; import useLocalStorage from '@rehooks/local-storage'; import { memo, useCallback, useContext, useEffect, useRef, useState } from 'react'; export const NoteParent = memo(function NoteParent({ id }: { id: string }) { const [pool, relays]: any = useContext(RelayContext); const [activeAccount]: any = useLocalStorage('activeAccount', {}); const [event, setEvent] = useState(null); const unsubscribe = useRef(null); const content = event ? contentParser(event.content, event.tags) : ''; const fetchEvent = useCallback(async () => { const { createNote } = await import('@utils/bindings'); unsubscribe.current = pool.subscribe( [ { ids: [id], kinds: [1], }, ], relays, (event: any) => { // update state setEvent(event); // insert to database const parentID = getParentID(event.tags, event.id); // insert event to local database createNote({ event_id: event.id, pubkey: event.pubkey, kind: event.kind, tags: JSON.stringify(event.tags), content: event.content, parent_id: parentID, parent_comment_id: '', created_at: event.created_at, account_id: activeAccount.id, }).catch(console.error); }, undefined, undefined, { unsubscribeOnEose: true, } ); }, [activeAccount.id, id, pool, relays]); const checkNoteExist = useCallback(async () => { const { getNoteById } = await import('@utils/bindings'); getNoteById({ event_id: id }) .then((res) => { if (res) { setEvent(res); } else { fetchEvent(); } }) .catch(console.error); }, [fetchEvent, id]); useEffect(() => { checkNoteExist(); return () => { if (unsubscribe.current) { unsubscribe.current(); } }; }, [checkNoteExist]); if (event) { return (
{content}
e.stopPropagation()} className="mt-5 pl-[52px]">
); } else { return (
ยท
); } });