From 169ab373043b91d6f36b6d33b689be9a01311469 Mon Sep 17 00:00:00 2001 From: enes Date: Tue, 5 Nov 2024 14:42:22 +0100 Subject: [PATCH] fix: get multiple tag values --- src/utils/blog.ts | 4 ++-- src/utils/nostr.ts | 18 +++++++++++++++++- 2 files changed, 19 insertions(+), 3 deletions(-) diff --git a/src/utils/blog.ts b/src/utils/blog.ts index 0de30db..df11b63 100644 --- a/src/utils/blog.ts +++ b/src/utils/blog.ts @@ -1,6 +1,6 @@ import { NDKEvent } from '@nostr-dev-kit/ndk' import { BlogCardDetails, BlogDetails } from 'types' -import { getFirstTagValue, getFirstTagValueAsInt, getTagValue } from './nostr' +import { getFirstTagValue, getFirstTagValueAsInt, getTagValues } from './nostr' import { kinds, nip19 } from 'nostr-tools' export const extractBlogDetails = (event: NDKEvent): Partial => ({ @@ -17,7 +17,7 @@ export const extractBlogDetails = (event: NDKEvent): Partial => ({ rTag: getFirstTagValue(event, 'r') || 'N/A', dTag: getFirstTagValue(event, 'd'), aTag: getFirstTagValue(event, 'a'), - tTags: getTagValue(event, 't') || [] + tTags: getTagValues(event, 't') || [] }) export const extractBlogCardDetails = ( diff --git a/src/utils/nostr.ts b/src/utils/nostr.ts index b1fd0e1..772c37b 100644 --- a/src/utils/nostr.ts +++ b/src/utils/nostr.ts @@ -65,11 +65,27 @@ export const getTagValue = ( return null } +export const getTagValues = ( + event: Event | NDKEvent, + tagIdentifier: string +): string[] | null => { + // Find the tag in the event's tags array where the first element matches the tagIdentifier. + const tags = event.tags.filter((item) => item[0] === tagIdentifier) + + // If a matching tag is found, return the rest of the elements in the tag (i.e., the values). + if (tags && tags.length) { + return tags.map((item) => item[1]) // Returning only the values + } + + // Return null if no matching tag is found. + return null +} + export const getFirstTagValue = ( event: Event | NDKEvent, tagIdentifier: string ) => { - const tags = getTagValue(event, tagIdentifier) + const tags = getTagValues(event, tagIdentifier) return tags && tags.length ? tags[0] : undefined }