import { NDKEvent, NDKSubscription } from '@nostr-dev-kit/ndk'; import { useQuery } from '@tanstack/react-query'; import { useCallback, useEffect, useRef } from 'react'; import { useParams } from 'react-router-dom'; import { VList, VListHandle } from 'virtua'; import { ChatForm } from '@app/chats/components/chatForm'; import { ChatMessage } from '@app/chats/components/message'; import { useNDK } from '@libs/ndk/provider'; import { useStorage } from '@libs/storage/provider'; import { LoaderIcon } from '@shared/icons'; import { User } from '@shared/user'; import { useStronghold } from '@stores/stronghold'; import { useNostr } from '@utils/hooks/useNostr'; export function ChatScreen() { const listRef = useRef(null); const userPrivkey = useStronghold((state) => state.privkey); const { db } = useStorage(); const { ndk } = useNDK(); const { pubkey } = useParams(); const { fetchNIP04Messages } = useNostr(); const { status, data } = useQuery(['nip04-dm', pubkey], async () => { return await fetchNIP04Messages(pubkey); }); const renderItem = useCallback( (message: NDKEvent) => { return ( ); }, [data] ); useEffect(() => { if (data && data.length > 0) listRef.current?.scrollToIndex(data.length); }, [data]); useEffect(() => { const sub: NDKSubscription = ndk.subscribe( { kinds: [4], authors: [db.account.pubkey], '#p': [pubkey], since: Math.floor(Date.now() / 1000), }, { closeOnEose: false, } ); sub.addListener('event', (event) => { console.log(event); }); return () => { sub.stop(); }; }, [pubkey]); return (
{status === 'loading' ? (

Loading messages

) : data.length === 0 ? (

🙌

You two didn't talk yet, let's send first message

) : ( {data.map((message) => renderItem(message))} )}
); }