()
| 10 | |
| 11 | /** Post a NIP-22 (kind 1111) comment on an event. */ |
| 12 | export function usePostComment() { |
| 13 | const { mutateAsync: publishEvent } = useNostrPublish(); |
| 14 | const queryClient = useQueryClient(); |
| 15 | |
| 16 | return useMutation({ |
| 17 | mutationFn: async ({ root, reply, content }: PostCommentParams) => { |
| 18 | const tags: string[][] = []; |
| 19 | |
| 20 | // d-tag identifiers |
| 21 | const dRoot = root instanceof URL ? '' : root.tags.find(([name]) => name === 'd')?.[1] ?? ''; |
| 22 | const dReply = reply instanceof URL ? '' : reply?.tags.find(([name]) => name === 'd')?.[1] ?? ''; |
| 23 | |
| 24 | // Root event tags |
| 25 | if (root instanceof URL) { |
| 26 | tags.push(['I', root.toString()]); |
| 27 | } else if (NKinds.addressable(root.kind)) { |
| 28 | tags.push(['A', `${root.kind}:${root.pubkey}:${dRoot}`]); |
| 29 | } else if (NKinds.replaceable(root.kind)) { |
| 30 | tags.push(['A', `${root.kind}:${root.pubkey}:`]); |
| 31 | } else { |
| 32 | tags.push(['E', root.id]); |
| 33 | } |
| 34 | if (root instanceof URL) { |
| 35 | // NIP-73: URLs use 'web' as the kind value |
| 36 | tags.push(['K', 'web']); |
| 37 | } else { |
| 38 | tags.push(['K', root.kind.toString()]); |
| 39 | tags.push(['P', root.pubkey]); |
| 40 | } |
| 41 | |
| 42 | // Reply event tags |
| 43 | if (reply) { |
| 44 | if (reply instanceof URL) { |
| 45 | tags.push(['i', reply.toString()]); |
| 46 | } else if (NKinds.addressable(reply.kind)) { |
| 47 | tags.push(['a', `${reply.kind}:${reply.pubkey}:${dReply}`]); |
| 48 | } else if (NKinds.replaceable(reply.kind)) { |
| 49 | tags.push(['a', `${reply.kind}:${reply.pubkey}:`]); |
| 50 | } else { |
| 51 | tags.push(['e', reply.id]); |
| 52 | } |
| 53 | if (reply instanceof URL) { |
| 54 | // NIP-73: URLs use 'web' as the kind value |
| 55 | tags.push(['k', 'web']); |
| 56 | } else { |
| 57 | tags.push(['k', reply.kind.toString()]); |
| 58 | tags.push(['p', reply.pubkey]); |
| 59 | } |
| 60 | } else { |
| 61 | // If this is a top-level comment, use the root event's tags |
| 62 | if (root instanceof URL) { |
| 63 | tags.push(['i', root.toString()]); |
| 64 | } else if (NKinds.addressable(root.kind)) { |
| 65 | tags.push(['a', `${root.kind}:${root.pubkey}:${dRoot}`]); |
| 66 | } else if (NKinds.replaceable(root.kind)) { |
| 67 | tags.push(['a', `${root.kind}:${root.pubkey}:`]); |
| 68 | } else { |
| 69 | tags.push(['e', root.id]); |
no test coverage detected