import { NDKSubscription } from '@nostr-dev-kit/ndk'; import { useMutation, useQuery, useQueryClient } 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/chat/components/messages/form'; import { ChatMessageItem } from '@app/chat/components/messages/item'; import { ChatSidebar } from '@app/chat/components/sidebar'; import { useNDK } from '@libs/ndk/provider'; import { createChat, getChatMessages } from '@libs/storage'; import { useAccount } from '@utils/hooks/useAccount'; export function ChatScreen() { const queryClient = useQueryClient(); const virtuosoRef = useRef(null); const { ndk } = useNDK(); const { pubkey } = useParams(); const { account } = useAccount(); const { status, data } = useQuery( ['chat', pubkey], async () => { return await getChatMessages(account.pubkey, pubkey); }, { enabled: account ? true : false, } ); const itemContent: any = useCallback( (index: string | number) => { return ( ); }, [data] ); const computeItemKey = useCallback( (index: string | number) => { return data[index].id; }, [data] ); const chat = useMutation({ mutationFn: (data: any) => { return createChat( data.id, data.receiver_pubkey, data.sender_pubkey, data.content, data.tags, data.created_at ); }, onSuccess: () => { queryClient.invalidateQueries({ queryKey: ['chat', pubkey] }); }, }); useEffect(() => { const sub: NDKSubscription = ndk.subscribe( { kinds: [4], authors: [account.pubkey], '#p': [pubkey], since: Math.floor(Date.now() / 1000), }, { closeOnEose: false, } ); sub.addListener('event', (event) => { chat.mutate({ id: event.id, receiver_pubkey: pubkey, sender_pubkey: event.pubkey, content: event.content, tags: event.tags, created_at: event.created_at, }); }); return () => { sub.stop(); }; }, [pubkey]); return (

Encrypted Chat

{status === 'loading' ? (

Loading...

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

🙌

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

);