NostrNode.tsx raw
1 import { EmbeddedMention, EmbeddedNote } from '@/components/Embedded'
2 import { nip19 } from 'nostr-tools'
3 import { ComponentProps, useMemo } from 'react'
4 import { Components } from './types'
5
6 export default function NostrNode({ rawText, bech32Id }: ComponentProps<Components['nostr']>) {
7 const { type, id } = useMemo(() => {
8 if (!bech32Id) return { type: 'invalid', id: '' }
9 try {
10 const { type } = nip19.decode(bech32Id)
11 if (type === 'npub') {
12 return { type: 'mention', id: bech32Id }
13 }
14 if (type === 'nevent' || type === 'naddr' || type === 'note') {
15 return { type: 'note', id: bech32Id }
16 }
17 } catch (error) {
18 console.error('Invalid bech32 ID:', bech32Id, error)
19 }
20 return { type: 'invalid', id: '' }
21 }, [bech32Id])
22
23 if (type === 'invalid') return rawText
24
25 if (type === 'mention') {
26 return <EmbeddedMention userId={id} className="not-prose" />
27 }
28 return <EmbeddedNote noteId={id} className="not-prose" />
29 }
30