import { 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 { Virtuoso } from 'react-virtuoso'; import { ChatMessageForm } from '@app/chats/components/messages/form'; import { ChatMessageItem } from '@app/chats/components/messages/item'; import { ChatSidebar } from '@app/chats/components/sidebar'; import { useNDK } from '@libs/ndk/provider'; import { useStorage } from '@libs/storage/provider'; import { LoaderIcon } from '@shared/icons'; import { useStronghold } from '@stores/stronghold'; import { useNostr } from '@utils/hooks/useNostr'; export function ChatScreen() { const virtuosoRef = 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 itemContent = useCallback( (index: string | number) => { const message = data[index]; if (!message) return; return ( ); }, [data] ); const computeItemKey = useCallback( (index: string | number) => { return data[index].id; }, [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

) : ( Empty, }} /> )}
); } const Empty = (

🙌

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

);