Skip to content

Commit

Permalink
Fix/client requests (keep-starknet-strange#135)
Browse files Browse the repository at this point in the history
* shim nostr-tools

* Replace nostr-tools with NDK

* Remove shim

* Remove unused event kind

* Fix useSendNote hook
  • Loading branch information
ugur-eren authored May 30, 2024
1 parent 0b18362 commit 6d77c3d
Show file tree
Hide file tree
Showing 23 changed files with 113 additions and 492 deletions.
4 changes: 2 additions & 2 deletions JoyboyCommunity/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,8 @@
},
"dependencies": {
"@expo/metro-runtime": "~3.2.1",
"@nostr-dev-kit/ndk": "^2.8.1",
"@noble/curves": "^1.4.0",
"@nostr-dev-kit/ndk": "^2.8.2",
"@react-native-async-storage/async-storage": "^1.23.1",
"@react-navigation/bottom-tabs": "^6.5.20",
"@react-navigation/native": "^6.1.17",
Expand All @@ -36,7 +37,6 @@
"expo-splash-screen": "~0.27.4",
"expo-status-bar": "~1.12.1",
"fast-text-encoding": "^1.0.6",
"nostr-tools": "^2.5.2",
"pbkdf2": "^3.1.2",
"react": "18.2.0",
"react-dom": "18.2.0",
Expand Down
3 changes: 3 additions & 0 deletions JoyboyCommunity/src/app/App.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -4,19 +4,22 @@ import * as SplashScreen from 'expo-splash-screen';
import {useCallback, useEffect, useState} from 'react';
import {StatusBar, View} from 'react-native';

import {useNostrContext} from '../context/NostrContext';
import {Router} from './Router';

// Keep the splash screen visible while we fetch resources
SplashScreen.preventAutoHideAsync();

export default function App() {
const [appIsReady, setAppIsReady] = useState(false);
const {ndk} = useNostrContext();

useEffect(() => {
async function prepare() {
try {
// Pre-load fonts, make any API calls you need to do here
await Font.loadAsync(Entypo.font);
await ndk.connect();
} catch (e) {
console.warn(e);
} finally {
Expand Down
12 changes: 8 additions & 4 deletions JoyboyCommunity/src/context/NostrContext.tsx
Original file line number Diff line number Diff line change
@@ -1,20 +1,24 @@
import {SimplePool} from 'nostr-tools';
import NDK from '@nostr-dev-kit/ndk';
import {createContext, useContext, useMemo} from 'react';

import {JOYBOY_RELAYS} from '../utils/relay';

export type NostrContextType = {
pool: SimplePool;
ndk: NDK;
relays: string[];
};

export const NostrContext = createContext<NostrContextType | null>(null);

export const NostrProvider: React.FC<React.PropsWithChildren> = ({children}) => {
const pool = useMemo(() => new SimplePool(), []);
const relays = JOYBOY_RELAYS;
const ndk = useMemo(() => {
return new NDK({
explicitRelayUrls: relays,
});
}, [relays]);

return <NostrContext.Provider value={{pool, relays}}>{children}</NostrContext.Provider>;
return <NostrContext.Provider value={{ndk, relays}}>{children}</NostrContext.Provider>;
};

export const useNostrContext = () => {
Expand Down
1 change: 1 addition & 0 deletions JoyboyCommunity/src/hooks/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,3 +4,4 @@ export {useReactions} from './useReactions';
export {useReplyNotes} from './useReplyNotes';
export {useReposts} from './useReposts';
export {useRootNotes} from './useRootNotes';
export {useSendNote} from './useSendNote';
242 changes: 0 additions & 242 deletions JoyboyCommunity/src/hooks/useNostr.tsx

This file was deleted.

8 changes: 4 additions & 4 deletions JoyboyCommunity/src/hooks/useNote.ts
Original file line number Diff line number Diff line change
@@ -1,20 +1,20 @@
import {NDKKind} from '@nostr-dev-kit/ndk';
import {useQuery} from '@tanstack/react-query';

import {useNostrContext} from '../context/NostrContext';
import {EventKind} from '../types';

export type UseNoteOptions = {
noteId: string;
};

export const useNote = (options: UseNoteOptions) => {
const {pool, relays} = useNostrContext();
const {ndk} = useNostrContext();

return useQuery({
queryKey: ['note', options.noteId],
queryFn: async () => {
const note = await pool.get(relays, {
kinds: [EventKind.Note],
const note = await ndk.fetchEvent({
kinds: [NDKKind.Text],
ids: [options.noteId],
});

Expand Down
8 changes: 4 additions & 4 deletions JoyboyCommunity/src/hooks/useProfile.ts
Original file line number Diff line number Diff line change
@@ -1,20 +1,20 @@
import {NDKKind} from '@nostr-dev-kit/ndk';
import {useQuery} from '@tanstack/react-query';

import {useNostrContext} from '../context/NostrContext';
import {EventKind} from '../types';

export type UseProfileOptions = {
publicKey: string;
};

export const useProfile = (options: UseProfileOptions) => {
const {pool, relays} = useNostrContext();
const {ndk} = useNostrContext();

return useQuery({
queryKey: ['profile', options.publicKey],
queryFn: async () => {
const profileEvent = await pool.get(relays, {
kinds: [EventKind.Metadata],
const profileEvent = await ndk.fetchEvent({
kinds: [NDKKind.Metadata],
authors: [options.publicKey],
});

Expand Down
Loading

0 comments on commit 6d77c3d

Please sign in to comment.