degmods.com/src/hooks/useComments.ts

47 lines
1.0 KiB
TypeScript
Raw Normal View History

import {
MetadataController,
RelayController,
UserRelaysType
} from 'controllers'
import { Filter, kinds } from 'nostr-tools'
import { useState } from 'react'
import { CommentEvent, ModDetails } from 'types'
import { useDidMount } from './useDidMount'
export const useComments = (mod: ModDetails) => {
const [commentEvents, setCommentEvents] = useState<CommentEvent[]>([])
useDidMount(async () => {
const metadataController = await MetadataController.getInstance()
const authorReadRelays = await metadataController.findUserRelays(
mod.author,
UserRelaysType.Read
)
const filter: Filter = {
kinds: [kinds.ShortTextNote],
'#a': [mod.aTag]
}
RelayController.getInstance().subscribeForEvents(
filter,
authorReadRelays,
(event) => {
setCommentEvents((prev) => {
if (prev.find((e) => e.id === event.id)) {
return [...prev]
}
return [event, ...prev]
})
}
)
})
return {
commentEvents,
setCommentEvents
}
}