2024-09-23 20:57:29 +05:00
|
|
|
import {
|
2024-10-24 21:07:18 +05:00
|
|
|
getRelayListForUser,
|
2024-10-14 19:20:43 +05:00
|
|
|
NDKFilter,
|
|
|
|
NDKKind,
|
2024-10-24 21:07:18 +05:00
|
|
|
NDKRelaySet,
|
2024-10-14 19:20:43 +05:00
|
|
|
NDKSubscription,
|
|
|
|
NDKSubscriptionCacheUsage
|
|
|
|
} from '@nostr-dev-kit/ndk'
|
|
|
|
import { useEffect, useState } from 'react'
|
2024-11-05 13:57:39 +01:00
|
|
|
import { CommentEvent, UserRelaysType } from 'types'
|
2024-10-24 21:07:18 +05:00
|
|
|
import { log, LogType, timeout } from 'utils'
|
2024-10-14 19:20:43 +05:00
|
|
|
import { useNDKContext } from './useNDKContext'
|
2024-09-23 20:57:29 +05:00
|
|
|
|
2024-11-05 13:57:39 +01:00
|
|
|
export const useComments = (
|
|
|
|
author: string | undefined,
|
|
|
|
aTag: string | undefined
|
|
|
|
) => {
|
2024-10-14 19:20:43 +05:00
|
|
|
const { ndk } = useNDKContext()
|
2024-09-23 20:57:29 +05:00
|
|
|
const [commentEvents, setCommentEvents] = useState<CommentEvent[]>([])
|
|
|
|
|
2024-10-14 19:20:43 +05:00
|
|
|
useEffect(() => {
|
2024-11-05 13:57:39 +01:00
|
|
|
if (!(author && aTag)) {
|
|
|
|
// Author and aTag are required
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2024-10-14 19:20:43 +05:00
|
|
|
let subscription: NDKSubscription // Define the subscription variable here for cleanup
|
2024-09-23 20:57:29 +05:00
|
|
|
|
2024-10-14 19:20:43 +05:00
|
|
|
const setupSubscription = async () => {
|
2024-10-24 21:07:18 +05:00
|
|
|
// Find the mod author's relays.
|
|
|
|
|
|
|
|
const authorReadRelays = await Promise.race([
|
2024-11-05 13:57:39 +01:00
|
|
|
getRelayListForUser(author, ndk),
|
2024-10-24 21:07:18 +05:00
|
|
|
timeout(10 * 1000) // add a 10 sec timeout
|
|
|
|
])
|
|
|
|
.then((ndkRelayList) => {
|
|
|
|
if (ndkRelayList) return ndkRelayList[UserRelaysType.Read]
|
|
|
|
return [] // Return an empty array if ndkRelayList is undefined
|
|
|
|
})
|
|
|
|
.catch((err) => {
|
|
|
|
log(
|
2024-12-26 16:40:40 +01:00
|
|
|
false, // Too many failed requests, turned off for clarity
|
2024-10-24 21:07:18 +05:00
|
|
|
LogType.Error,
|
2024-11-05 13:57:39 +01:00
|
|
|
`An error occurred in fetching user's (${author}) ${UserRelaysType.Read}`,
|
2024-10-24 21:07:18 +05:00
|
|
|
err
|
|
|
|
)
|
|
|
|
return [] as string[]
|
|
|
|
})
|
|
|
|
|
2024-10-14 19:20:43 +05:00
|
|
|
const filter: NDKFilter = {
|
|
|
|
kinds: [NDKKind.Text],
|
2024-11-05 13:57:39 +01:00
|
|
|
'#a': [aTag]
|
2024-10-14 19:20:43 +05:00
|
|
|
}
|
|
|
|
|
2024-10-24 21:07:18 +05:00
|
|
|
const relayUrls = new Set<string>()
|
|
|
|
|
|
|
|
ndk.pool.urls().forEach((relayUrl) => {
|
|
|
|
relayUrls.add(relayUrl)
|
2024-10-24 20:29:57 +05:00
|
|
|
})
|
|
|
|
|
2024-10-24 21:07:18 +05:00
|
|
|
authorReadRelays.forEach((relayUrl) => relayUrls.add(relayUrl))
|
|
|
|
|
|
|
|
subscription = ndk.subscribe(
|
|
|
|
filter,
|
|
|
|
{
|
|
|
|
closeOnEose: false,
|
|
|
|
cacheUsage: NDKSubscriptionCacheUsage.CACHE_FIRST
|
|
|
|
},
|
2024-11-20 13:50:49 +01:00
|
|
|
relayUrls.size
|
|
|
|
? NDKRelaySet.fromRelayUrls(Array.from(relayUrls), ndk)
|
|
|
|
: undefined
|
2024-10-24 21:07:18 +05:00
|
|
|
)
|
2024-09-23 20:57:29 +05:00
|
|
|
|
2024-10-14 19:20:43 +05:00
|
|
|
subscription.on('event', (ndkEvent) => {
|
2024-09-23 20:57:29 +05:00
|
|
|
setCommentEvents((prev) => {
|
2024-10-14 19:20:43 +05:00
|
|
|
if (prev.find((e) => e.id === ndkEvent.id)) {
|
2024-09-23 20:57:29 +05:00
|
|
|
return [...prev]
|
|
|
|
}
|
|
|
|
|
2024-10-14 19:20:43 +05: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 20:57:29 +05:00
|
|
|
})
|
2024-10-14 19:20:43 +05:00
|
|
|
})
|
|
|
|
|
|
|
|
subscription.start()
|
|
|
|
}
|
|
|
|
|
|
|
|
setupSubscription()
|
|
|
|
|
|
|
|
// Cleanup function to stop the subscription on unmount
|
|
|
|
return () => {
|
|
|
|
if (subscription) {
|
|
|
|
subscription.stop()
|
2024-09-23 20:57:29 +05:00
|
|
|
}
|
2024-10-14 19:20:43 +05:00
|
|
|
}
|
2024-11-05 13:57:39 +01:00
|
|
|
}, [aTag, author, ndk])
|
2024-09-23 20:57:29 +05:00
|
|
|
|
|
|
|
return {
|
|
|
|
commentEvents,
|
|
|
|
setCommentEvents
|
|
|
|
}
|
|
|
|
}
|