diff --git a/JoyboyCommunity/src/hooks/useNostr.tsx b/JoyboyCommunity/src/hooks/useNostr.tsx index b3b1fa8b..9dc2382f 100644 --- a/JoyboyCommunity/src/hooks/useNostr.tsx +++ b/JoyboyCommunity/src/hooks/useNostr.tsx @@ -15,7 +15,7 @@ import {JOYBOY_RELAYS, RELAYS_PROD} from '../utils/relay'; export const useNostr = () => { const pool = new SimplePool(); - const relays = RELAYS_PROD; + const relays = JOYBOY_RELAYS; const nip07signer = new NDKNip07Signer(); const ndk = new NDK({signer: nip07signer}); @@ -114,15 +114,41 @@ export const useNostr = () => { } }; + // const getEventsByQuery = async ( + // ids: string[] = ['1', '3'], + // filter?: Filter, + // relaysProps?: string[], + // ) => { + // try { + // const events = await pool.querySync(relaysProps ?? relays, { + // ids, + // ...filter, + // }); + // return events; + // } catch (e) { + // console.log('error getEventsByQuery', e); + // } + // }; + + const getEventsFilter = async (filter?: Filter, isSetEvents?: boolean) => { + const eventsNotes = await pool.querySync(relays, {kinds: [1], ...filter}); + if (isSetEvents) { + setEventsData(eventsNotes); + } + return eventsNotes; + }; + const getEventsByQuery = async ( ids: string[] = ['1', '3'], + kinds: number[] = [1, 3], filter?: Filter, relaysProps?: string[], ) => { try { const events = await pool.querySync(relaysProps ?? relays, { - ids, ...filter, + kinds, + // ids, }); return events; } catch (e) { @@ -187,6 +213,7 @@ export const useNostr = () => { sk: Uint8Array, content: string, tags?: string[][], + kind: number = 1, ): Promise<{ event?: VerifiedEvent; isValid?: boolean; @@ -195,7 +222,7 @@ export const useNostr = () => { try { const event = finalizeEvent( { - kind: 1, + kind: kind ?? 1, created_at: Math.floor(Date.now() / 1000), tags: tags ?? [], content, @@ -215,12 +242,19 @@ export const useNostr = () => { console.log('isGood', isGood); let eventPublish = await pool.publish(JOYBOY_RELAYS, event); + + let test = await pool.publish(JOYBOY_RELAYS, event); + console.log('eventPublish', eventPublish); + let relay = await Relay.connect(JOYBOY_RELAYS[0]); + let publish = await relay.publish(event); + console.log('publish', publish); + return { event, isValid: true, - published:eventPublish + published: eventPublish, }; } catch (e) { console.log('issue sendNote', e); diff --git a/JoyboyCommunity/src/shared/components/Comments.tsx b/JoyboyCommunity/src/shared/components/Comments.tsx index 2e4fdda1..7563175c 100644 --- a/JoyboyCommunity/src/shared/components/Comments.tsx +++ b/JoyboyCommunity/src/shared/components/Comments.tsx @@ -1,8 +1,13 @@ -import React from 'react'; +import React, {useCallback, useEffect, useState} from 'react'; import {FlatList, View} from 'react-native'; - import PostComment from './PostComment'; - +import {Input} from '../../components'; +import {SendComment, ViewSendComment} from './styled'; +import {MaterialCommunityIcons} from '@expo/vector-icons'; +import {useNostr} from '../../hooks/useNostr'; +import {useAuth} from '../../store/auth'; +import {Event as EventNostr} from 'nostr-tools'; +import {JOYBOY_RELAYS} from '../../utils/relay'; const mockEvents = [ { post: { @@ -74,18 +79,96 @@ const mockEvents = [ sourceUser: null, }, ]; -function Comments() { + +interface IComments { + event?: EventNostr; +} +function Comments({event}: IComments) { + const [text, setText] = useState(); + const {privateKey, publicKey} = useAuth(); + + const [comments, setComments] = useState([]); + + const {sendNote, getEventsByQuery} = useNostr(); + + useEffect(() => { + const fetchReplies = async () => { + if (event?.id) { + let tags = [['e', event?.id]]; + let eventsFind = await getEventsByQuery(['1'], [1], { + '#e': tags[0], + }); + console.log('events comments', eventsFind); + setComments(eventsFind); + } + }; + fetchReplies(); + }, [event]); + /** @TODO handle send comment */ + + const handleSendComment = useCallback(async () => { + try { + console.log('handle send note'); + // do something on post + if (!text || text?.length == 0) { + alert('Write your note'); + return; + } + alert('Note sending, please wait.'); + + if (!privateKey) { + alert('Please login before send a note'); + return; + } + + /** @TODO handle tags NIP-10 */ + + /** tags */ + // let tags = [['e', event?.id, JOYBOY_RELAYS[0], 'root', publicKey]]; + let tags = [['e', event?.id, JOYBOY_RELAYS[0], 'root', publicKey]]; + + const noteEvent = await sendNote(privateKey, text, tags); + console.log('noteEvent', noteEvent); + if (noteEvent?.isValid) { + alert('Note send'); + } + } catch (e) { + console.log('Error send note', e); + } + }, [text]); + const isCreateDisabled = text && text?.length > 0 ? false : true; + + /**@TODO fetch data */ + useEffect(() => {}, []); return ( - } - renderItem={({item}) => { - return ; - }} - /> + + + + + + + + + } + renderItem={({item}) => { + return ; + // return ; + }} + /> + ); } diff --git a/JoyboyCommunity/src/shared/components/PostDetails.tsx b/JoyboyCommunity/src/shared/components/PostDetails.tsx index e6debff2..a00f05ba 100644 --- a/JoyboyCommunity/src/shared/components/PostDetails.tsx +++ b/JoyboyCommunity/src/shared/components/PostDetails.tsx @@ -128,7 +128,7 @@ function PostDetails({route}) { - + ); diff --git a/JoyboyCommunity/src/shared/components/styled.ts b/JoyboyCommunity/src/shared/components/styled.ts new file mode 100644 index 00000000..8594a361 --- /dev/null +++ b/JoyboyCommunity/src/shared/components/styled.ts @@ -0,0 +1,22 @@ +import {Pressable, View} from 'react-native'; +import styled from 'styled-components'; + +export const ViewSendComment = styled(View)` + flex-direction: row; + dispay:flex; + justify-content: space-between; + align-items: center; + border-radius: 8px; + padding: 8px 24px; + border-color: black; + color: white; +`; + + +export const SendComment = styled(Pressable)` + border-radius: 8px; + padding: 8px 24px; + border-color: black; + color: white; +`; + diff --git a/JoyboyCommunity/src/utils/relay.ts b/JoyboyCommunity/src/utils/relay.ts index 7159834c..ddda25bb 100644 --- a/JoyboyCommunity/src/utils/relay.ts +++ b/JoyboyCommunity/src/utils/relay.ts @@ -1,6 +1,7 @@ export const JOYBOY_RELAYS = [ - 'wss://nostr.joyboy.community', - 'wss://localhost:3000', + // 'http://localhost:3000', + 'ws://localhost:3000', + // 'wss://nostr.joyboy.community', ]; diff --git a/onchain/Scarb.lock b/onchain/Scarb.lock index a13987f3..a7e5641f 100644 --- a/onchain/Scarb.lock +++ b/onchain/Scarb.lock @@ -63,5 +63,5 @@ dependencies = [ [[package]] name = "snforge_std" -version = "0.21.0" -source = "git+https://github.com/foundry-rs/starknet-foundry?tag=v0.21.0#2996b8c1dd66b2715fc67e69578089f278a46790" +version = "0.23.0" +source = "git+https://github.com/foundry-rs/starknet-foundry?tag=v0.23.0#f2bff8f796763ada77fe6033ec1b034ceee22abd"