forked from keep-starknet-strange/joyboy
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: new nostr hooks (keep-starknet-strange#132)
* new nostr hooks * new hooks * fix type error * Fix conflicts * fix query keys for hooks
- Loading branch information
Showing
9 changed files
with
218 additions
and
9 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
export {useProfile} from './useProfile'; | ||
export {useReactions} from './useReactions'; | ||
export {useReplyNotes} from './useReplyNotes'; | ||
export {useReposts} from './useReposts'; | ||
export {useRootNotes} from './useRootNotes'; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,34 @@ | ||
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, othersRelays} = useNostrContext(); | ||
|
||
return useQuery({ | ||
queryKey: ['profile', options.publicKey], | ||
queryFn: async () => { | ||
const profileEvent = await pool.get(othersRelays, { | ||
kinds: [EventKind.Metadata], | ||
authors: [options?.publicKey], | ||
}); | ||
|
||
const profile = JSON.parse(profileEvent.content) as Record<string, string | undefined>; | ||
|
||
return { | ||
username: profile.name, | ||
displayName: profile.display_name ?? profile.displayName, | ||
website: profile.website, | ||
banner: profile.banner, | ||
about: profile.about, | ||
picture: profile.picture, | ||
}; | ||
}, | ||
placeholderData: {} as any, | ||
}); | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,38 @@ | ||
import {useInfiniteQuery} from '@tanstack/react-query'; | ||
|
||
import {useNostrContext} from '../context/NostrContext'; | ||
import {EventKind} from '../types'; | ||
|
||
export type UseReactionsOptions = { | ||
authors?: string[]; | ||
search?: string; | ||
}; | ||
|
||
export const useReactions = (options?: UseReactionsOptions) => { | ||
const {pool, othersRelays} = useNostrContext(); | ||
|
||
return useInfiniteQuery({ | ||
initialPageParam: Math.round(Date.now() / 1000), | ||
queryKey: ['reactions', options?.authors, options?.search], | ||
getNextPageParam: (lastPage: any, allPages, lastPageParam) => { | ||
if (!lastPage?.length) return undefined; | ||
|
||
const pageParam = lastPage[lastPage.length - 1].created_at; | ||
|
||
if (!pageParam || pageParam === lastPageParam) return undefined; | ||
return pageParam; | ||
}, | ||
queryFn: async ({pageParam}) => { | ||
const notes = await pool.querySync(othersRelays, { | ||
kinds: [EventKind.Reaction], | ||
authors: options?.authors, | ||
search: options?.search, | ||
until: pageParam, | ||
limit: 20, | ||
}); | ||
|
||
return notes; | ||
}, | ||
placeholderData: {pages: [], pageParams: []}, | ||
}); | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,41 @@ | ||
import {useInfiniteQuery} from '@tanstack/react-query'; | ||
|
||
import {useNostrContext} from '../context/NostrContext'; | ||
import {EventKind} from '../types'; | ||
|
||
export type UseReplyNotesOptions = { | ||
noteId?: string; | ||
authors?: string[]; | ||
search?: string; | ||
}; | ||
|
||
export const useReplyNotes = (options?: UseReplyNotesOptions) => { | ||
const {pool, othersRelays} = useNostrContext(); | ||
|
||
return useInfiniteQuery({ | ||
initialPageParam: Math.round(Date.now() / 1000), | ||
queryKey: ['replyNotes', options?.noteId, options?.authors, options?.search], | ||
getNextPageParam: (lastPage: any, allPages, lastPageParam) => { | ||
if (!lastPage?.length) return undefined; | ||
|
||
const pageParam = lastPage[lastPage.length - 1].created_at; | ||
|
||
if (!pageParam || pageParam === lastPageParam) return undefined; | ||
return pageParam; | ||
}, | ||
queryFn: async ({pageParam}) => { | ||
const notes = await pool.querySync(othersRelays, { | ||
kinds: [EventKind.Note], | ||
authors: options?.authors, | ||
search: options?.search, | ||
until: pageParam, | ||
limit: 20, | ||
|
||
'#e': options?.noteId ? [options.noteId] : undefined, | ||
}); | ||
|
||
return notes.filter((note) => note.tags.every((tag) => tag[0] === 'e')); | ||
}, | ||
placeholderData: {pages: [], pageParams: []}, | ||
}); | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,38 @@ | ||
import {useInfiniteQuery} from '@tanstack/react-query'; | ||
|
||
import {useNostrContext} from '../context/NostrContext'; | ||
import {EventKind} from '../types'; | ||
|
||
export type UseRepostsOptions = { | ||
authors?: string[]; | ||
search?: string; | ||
}; | ||
|
||
export const useReposts = (options?: UseRepostsOptions) => { | ||
const {pool, othersRelays} = useNostrContext(); | ||
|
||
return useInfiniteQuery({ | ||
initialPageParam: Math.round(Date.now() / 1000), | ||
queryKey: ['reposts', options?.authors, options?.search], | ||
getNextPageParam: (lastPage: any, allPages, lastPageParam) => { | ||
if (!lastPage?.length) return undefined; | ||
|
||
const pageParam = lastPage[lastPage.length - 1].created_at; | ||
|
||
if (!pageParam || pageParam === lastPageParam) return undefined; | ||
return pageParam; | ||
}, | ||
queryFn: async ({pageParam}) => { | ||
const reposts = await pool.querySync(othersRelays, { | ||
kinds: [EventKind.Repost], | ||
authors: options?.authors, | ||
search: options?.search, | ||
until: pageParam, | ||
limit: 20, | ||
}); | ||
|
||
return reposts; | ||
}, | ||
placeholderData: {pages: [], pageParams: []}, | ||
}); | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,38 @@ | ||
import {useInfiniteQuery} from '@tanstack/react-query'; | ||
|
||
import {useNostrContext} from '../context/NostrContext'; | ||
import {EventKind} from '../types'; | ||
|
||
export type UseRootNotesOptions = { | ||
authors?: string[]; | ||
search?: string; | ||
}; | ||
|
||
export const useRootNotes = (options?: UseRootNotesOptions) => { | ||
const {pool, othersRelays} = useNostrContext(); | ||
|
||
return useInfiniteQuery({ | ||
initialPageParam: Math.round(Date.now() / 1000), | ||
queryKey: ['rootNotes', options?.authors, options?.search], | ||
getNextPageParam: (lastPage: any, allPages, lastPageParam) => { | ||
if (!lastPage?.length) return undefined; | ||
|
||
const pageParam = lastPage[lastPage.length - 1].created_at; | ||
|
||
if (!pageParam || pageParam === lastPageParam) return undefined; | ||
return pageParam; | ||
}, | ||
queryFn: async ({pageParam}) => { | ||
const notes = await pool.querySync(othersRelays, { | ||
kinds: [EventKind.Note], | ||
authors: options?.authors, | ||
search: options?.search, | ||
until: pageParam, | ||
limit: 20, | ||
}); | ||
|
||
return notes.filter((note) => note.tags.every((tag) => tag[0] !== 'e')); | ||
}, | ||
placeholderData: {pages: [], pageParams: []}, | ||
}); | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,17 @@ | ||
export enum EventKind { | ||
Metadata = 0, | ||
Note = 1, | ||
Follow = 3, | ||
EncryptedDM = 4, | ||
EventDeletion = 5, | ||
Repost = 6, | ||
Reaction = 7, | ||
BadgeAward = 8, | ||
GroupChatMessage = 9, | ||
GroupChatThreadedReply = 10, | ||
GroupThread = 11, | ||
GroupThreadReply = 12, | ||
Seal = 13, | ||
DirectMessage = 14, | ||
GenericRepost = 16, | ||
} |