import NoteMetadata from '@components/note/metadata'; import { ImagePreview } from '@components/note/preview/image'; import { VideoPreview } from '@components/note/preview/video'; import { NoteRepost } from '@components/note/repost'; import { RelayContext } from '@components/relaysProvider'; import { UserExtend } from '@components/user/extend'; import { UserMention } from '@components/user/mention'; import { getParentID } from '@utils/transform'; import destr from 'destr'; import { memo, useCallback, useContext, useEffect, useMemo, useRef, useState } from 'react'; import reactStringReplace from 'react-string-replace'; export const NoteParent = memo(function NoteParent({ id }: { id: string }) { const [pool, relays]: any = useContext(RelayContext); const [event, setEvent] = useState(null); const unsubscribe = useRef(null); const fetchEvent = useCallback(async () => { const { createNote } = await import('@utils/bindings'); const activeAccount = JSON.parse(localStorage.getItem('activeAccount')); 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, } ); }, [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 () => { unsubscribe.current; }; }, [checkNoteExist]); const content = useMemo(() => { let parsedContent = event ? event.content : null; if (parsedContent !== null) { // get data tags const tags = destr(event.tags); // handle urls parsedContent = reactStringReplace(parsedContent, /(https?:\/\/\S+)/g, (match, i) => { if (match.match(/\.(jpg|jpeg|gif|png|webp)$/i)) { // image url return ; } else if (match.match(/(www\.)?(youtube\.com\/watch\?v=|youtu\.be\/)([a-zA-Z0-9_-]{11})/i)) { // youtube return ; } else if (match.match(/\.(mp4|webm)$/i)) { // video return ; } else { return ( {match} ); } }); // handle #-hashtags parsedContent = reactStringReplace(parsedContent, /#(\w+)/g, (match, i) => ( #{match} )); // handle mentions if (tags.length > 0) { parsedContent = reactStringReplace(parsedContent, /\#\[(\d+)\]/gm, (match, i) => { if (tags[match][0] === 'p') { // @-mentions return ; } else if (tags[match][0] === 'e') { // note-mentions return ; } else { return; } }); } } return parsedContent; }, [event]); if (event) { return (
{content}
e.stopPropagation()} className="mt-5 pl-[52px]">
); } else { return (
ยท
); } });