2024-09-23 15:57:29 +00:00
|
|
|
import {
|
2024-10-14 14:20:43 +00:00
|
|
|
getRelayListForUser,
|
|
|
|
NDKFilter,
|
|
|
|
NDKKind,
|
|
|
|
NDKRelaySet,
|
|
|
|
NDKSubscription,
|
|
|
|
NDKSubscriptionCacheUsage
|
|
|
|
} from '@nostr-dev-kit/ndk'
|
|
|
|
import { UserRelaysType } from 'controllers'
|
|
|
|
import { useEffect, useState } from 'react'
|
2024-09-23 15:57:29 +00:00
|
|
|
import { CommentEvent, ModDetails } from 'types'
|
2024-10-14 14:20:43 +00:00
|
|
|
import { log, LogType } from 'utils'
|
|
|
|
import { useNDKContext } from './useNDKContext'
|
2024-09-23 15:57:29 +00:00
|
|
|
|
|
|
|
export const useComments = (mod: ModDetails) => {
|
2024-10-14 14:20:43 +00:00
|
|
|
const { ndk } = useNDKContext()
|
2024-09-23 15:57:29 +00:00
|
|
|
const [commentEvents, setCommentEvents] = useState<CommentEvent[]>([])
|
|
|
|
|
2024-10-14 14:20:43 +00:00
|
|
|
useEffect(() => {
|
|
|
|
let subscription: NDKSubscription // Define the subscription variable here for cleanup
|
2024-09-23 15:57:29 +00:00
|
|
|
|
2024-10-14 14:20:43 +00:00
|
|
|
const setupSubscription = async () => {
|
|
|
|
// Find the mod author's relays.
|
|
|
|
const authorReadRelays = await getRelayListForUser(mod.author, ndk)
|
|
|
|
.then((ndkRelayList) => {
|
|
|
|
if (ndkRelayList) return ndkRelayList[UserRelaysType.Read]
|
|
|
|
return [] // Return an empty array if ndkRelayList is undefined
|
|
|
|
})
|
|
|
|
.catch((err) => {
|
|
|
|
log(
|
|
|
|
true,
|
|
|
|
LogType.Error,
|
|
|
|
`An error occurred in fetching user's (${mod.author}) ${UserRelaysType.Read}`,
|
|
|
|
err
|
|
|
|
)
|
|
|
|
return [] as string[]
|
|
|
|
})
|
2024-09-23 15:57:29 +00:00
|
|
|
|
2024-10-14 14:20:43 +00:00
|
|
|
const filter: NDKFilter = {
|
|
|
|
kinds: [NDKKind.Text],
|
|
|
|
'#a': [mod.aTag]
|
|
|
|
}
|
|
|
|
|
|
|
|
subscription = ndk.subscribe(
|
|
|
|
filter,
|
|
|
|
{
|
|
|
|
closeOnEose: false,
|
|
|
|
cacheUsage: NDKSubscriptionCacheUsage.CACHE_FIRST
|
|
|
|
},
|
|
|
|
NDKRelaySet.fromRelayUrls(authorReadRelays, ndk, true)
|
|
|
|
)
|
2024-09-23 15:57:29 +00:00
|
|
|
|
2024-10-14 14:20:43 +00:00
|
|
|
subscription.on('event', (ndkEvent) => {
|
2024-09-23 15:57:29 +00:00
|
|
|
setCommentEvents((prev) => {
|
2024-10-14 14:20:43 +00:00
|
|
|
if (prev.find((e) => e.id === ndkEvent.id)) {
|
2024-09-23 15:57:29 +00:00
|
|
|
return [...prev]
|
|
|
|
}
|
|
|
|
|
2024-10-14 14:20:43 +00:00
|
|
|
const commentEvent: CommentEvent = {
|
|
|
|
kind: NDKKind.Text,
|
|
|
|
tags: ndkEvent.tags,
|
|
|
|
content: ndkEvent.content,
|
|
|
|
created_at: ndkEvent.created_at!,
|
|
|
|
pubkey: ndkEvent.pubkey,
|
|
|
|
id: ndkEvent.id,
|
|
|
|
sig: ndkEvent.sig!
|
|
|
|
}
|
|
|
|
|
|
|
|
return [commentEvent, ...prev]
|
2024-09-23 15:57:29 +00:00
|
|
|
})
|
2024-10-14 14:20:43 +00:00
|
|
|
})
|
|
|
|
|
|
|
|
subscription.start()
|
|
|
|
}
|
|
|
|
|
|
|
|
setupSubscription()
|
|
|
|
|
|
|
|
// Cleanup function to stop the subscription on unmount
|
|
|
|
return () => {
|
|
|
|
if (subscription) {
|
|
|
|
subscription.stop()
|
2024-09-23 15:57:29 +00:00
|
|
|
}
|
2024-10-14 14:20:43 +00:00
|
|
|
}
|
|
|
|
}, [mod.aTag, mod.author, ndk])
|
2024-09-23 15:57:29 +00:00
|
|
|
|
|
|
|
return {
|
|
|
|
commentEvents,
|
|
|
|
setCommentEvents
|
|
|
|
}
|
|
|
|
}
|