Skip to content

Commit

Permalink
add comments and stash
Browse files Browse the repository at this point in the history
  • Loading branch information
MSghais committed May 28, 2024
1 parent 94451ee commit 6c610d7
Show file tree
Hide file tree
Showing 6 changed files with 163 additions and 23 deletions.
42 changes: 38 additions & 4 deletions JoyboyCommunity/src/hooks/useNostr.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -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});

Expand Down Expand Up @@ -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) {
Expand Down Expand Up @@ -187,6 +213,7 @@ export const useNostr = () => {
sk: Uint8Array,
content: string,
tags?: string[][],
kind: number = 1,
): Promise<{
event?: VerifiedEvent;
isValid?: boolean;
Expand All @@ -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,
Expand All @@ -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);
Expand Down
111 changes: 97 additions & 14 deletions JoyboyCommunity/src/shared/components/Comments.tsx
Original file line number Diff line number Diff line change
@@ -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: {
Expand Down Expand Up @@ -74,18 +79,96 @@ const mockEvents = [
sourceUser: null,
},
];
function Comments() {

interface IComments {
event?: EventNostr;
}
function Comments({event}: IComments) {
const [text, setText] = useState<string | undefined>();
const {privateKey, publicKey} = useAuth();

const [comments, setComments] = useState<EventNostr[] | undefined>([]);

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 (
<FlatList
horizontal={false}
scrollEnabled={false}
showsHorizontalScrollIndicator={false}
data={mockEvents}
ItemSeparatorComponent={() => <View style={{height: 18}} />}
renderItem={({item}) => {
return <PostComment event={item.event} post={item.post} sourceUser={item.sourceUser} />;
}}
/>
<View>
<ViewSendComment>
<Input placeholder="Enter your comment" value={text} onChangeText={setText} />
<SendComment
disabled={isCreateDisabled}
// style={{backgroundColor: isCreateDisabled && 'gray'}}
onPress={handleSendComment}
>
<MaterialCommunityIcons
name="send-circle"
size={24}
color={isCreateDisabled ? 'gray' : 'black'}
/>
</SendComment>
</ViewSendComment>

<FlatList
horizontal={false}
scrollEnabled={false}
showsHorizontalScrollIndicator={false}
data={[...comments]}
ItemSeparatorComponent={() => <View style={{height: 18}} />}
renderItem={({item}) => {
return <PostComment event={item} />;
// return <PostComment event={item?.event} post={item?.post} sourceUser={item.sourceUser} />;
}}
/>
</View>
);
}

Expand Down
2 changes: 1 addition & 1 deletion JoyboyCommunity/src/shared/components/PostDetails.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -128,7 +128,7 @@ function PostDetails({route}) {
<Icon as={MaterialIcons} name="more-horiz" size={18} color="#406686" />
</InteractionContainer>
</View>
<Comments />
<Comments event={event} />
</PostDetailsCard>
</ScrollView>
);
Expand Down
22 changes: 22 additions & 0 deletions JoyboyCommunity/src/shared/components/styled.ts
Original file line number Diff line number Diff line change
@@ -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;
`;

5 changes: 3 additions & 2 deletions JoyboyCommunity/src/utils/relay.ts
Original file line number Diff line number Diff line change
@@ -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',
];


Expand Down
4 changes: 2 additions & 2 deletions onchain/Scarb.lock
Original file line number Diff line number Diff line change
Expand Up @@ -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"

0 comments on commit 6c610d7

Please sign in to comment.