debug: add debug logs to trace NIP-19 parsing and profile resolution

- Add logs to useProfileLabels hook
- Add logs to useMarkdownToHTML hook
- Add logs to RichContent component
- Add logs to extractNostrUris function
- Add error handling with fallbacks
This commit is contained in:
Gigi
2025-11-02 20:04:01 +01:00
parent b7cda7a351
commit 056da1ad23
4 changed files with 85 additions and 28 deletions

View File

@@ -19,14 +19,18 @@ const RichContent: React.FC<RichContentProps> = ({
content,
className = 'bookmark-content'
}) => {
// Pattern to match:
// 1. nostr: URIs (nostr:npub1..., nostr:note1..., etc.) using applesauce Tokens.nostrLink
// 2. http(s) URLs
const nostrPattern = Tokens.nostrLink
const urlPattern = /https?:\/\/[^\s]+/gi
const combinedPattern = new RegExp(`(${nostrPattern.source}|${urlPattern.source})`, 'gi')
console.log('[RichContent] Rendering, content length:', content?.length || 0)
const parts = content.split(combinedPattern)
try {
// Pattern to match:
// 1. nostr: URIs (nostr:npub1..., nostr:note1..., etc.) using applesauce Tokens.nostrLink
// 2. http(s) URLs
const nostrPattern = Tokens.nostrLink
const urlPattern = /https?:\/\/[^\s]+/gi
const combinedPattern = new RegExp(`(${nostrPattern.source}|${urlPattern.source})`, 'gi')
const parts = content.split(combinedPattern)
console.log('[RichContent] Split into parts:', parts.length)
// Helper to check if a string is a nostr identifier (without mutating regex state)
const isNostrIdentifier = (str: string): boolean => {
@@ -76,7 +80,11 @@ const RichContent: React.FC<RichContentProps> = ({
return <React.Fragment key={index}>{part}</React.Fragment>
})}
</div>
)
)
} catch (err) {
console.error('[RichContent] Error rendering:', err)
return <div className={className}>Error rendering content</div>
}
}
export default RichContent

View File

@@ -21,8 +21,11 @@ export const useMarkdownToHTML = (
const [processedMarkdown, setProcessedMarkdown] = useState<string>('')
const [articleTitles, setArticleTitles] = useState<Map<string, string>>(new Map())
console.log('[useMarkdownToHTML] Hook called, markdown length:', markdown?.length || 0, 'hasRelayPool:', !!relayPool)
// Resolve profile labels progressively as profiles load
const profileLabels = useProfileLabels(markdown || '')
console.log('[useMarkdownToHTML] Profile labels size:', profileLabels.size)
// Fetch article titles
useEffect(() => {
@@ -68,16 +71,26 @@ export const useMarkdownToHTML = (
let isCancelled = false
const processMarkdown = () => {
// Replace nostr URIs with profile labels (progressive) and article titles
const processed = replaceNostrUrisInMarkdownWithProfileLabels(
markdown,
profileLabels,
articleTitles
)
if (isCancelled) return
setProcessedMarkdown(processed)
console.log('[useMarkdownToHTML] Processing markdown, length:', markdown.length)
console.log('[useMarkdownToHTML] Profile labels:', profileLabels.size, 'Article titles:', articleTitles.size)
try {
// Replace nostr URIs with profile labels (progressive) and article titles
const processed = replaceNostrUrisInMarkdownWithProfileLabels(
markdown,
profileLabels,
articleTitles
)
console.log('[useMarkdownToHTML] Processed markdown length:', processed.length)
if (isCancelled) return
setProcessedMarkdown(processed)
} catch (err) {
console.error('[useMarkdownToHTML] Error processing markdown:', err)
if (!isCancelled) {
setProcessedMarkdown(markdown) // Fallback to original
}
}
const rafId = requestAnimationFrame(() => {
if (previewRef.current && !isCancelled) {

View File

@@ -12,14 +12,31 @@ const { getPubkeyFromDecodeResult, encodeDecodeResult } = Helpers
export function useProfileLabels(content: string): Map<string, string> {
// Extract profile pointers (npub and nprofile) using applesauce helpers
const profileData = useMemo(() => {
const pointers = getContentPointers(content)
return pointers
.filter(p => p.type === 'npub' || p.type === 'nprofile')
.map(pointer => ({
pubkey: getPubkeyFromDecodeResult(pointer),
encoded: encodeDecodeResult(pointer)
}))
.filter(p => p.pubkey)
console.log('[useProfileLabels] Processing content, length:', content?.length || 0)
try {
const pointers = getContentPointers(content)
console.log('[useProfileLabels] Found pointers:', pointers.length, 'types:', pointers.map(p => p.type))
const filtered = pointers.filter(p => p.type === 'npub' || p.type === 'nprofile')
console.log('[useProfileLabels] Profile pointers:', filtered.length)
const result = filtered
.map(pointer => {
try {
return {
pubkey: getPubkeyFromDecodeResult(pointer),
encoded: encodeDecodeResult(pointer)
}
} catch (err) {
console.error('[useProfileLabels] Error processing pointer:', err, pointer)
return null
}
})
.filter((p): p is { pubkey: string; encoded: string } => p !== null && !!p.pubkey)
console.log('[useProfileLabels] Profile data after filtering:', result.length)
return result
} catch (err) {
console.error('[useProfileLabels] Error extracting pointers:', err)
return []
}
}, [content])
// Fetch profiles for all found pubkeys (progressive loading)
@@ -30,15 +47,18 @@ export function useProfileLabels(content: string): Map<string, string> {
// Build profile labels map that updates reactively as profiles load
return useMemo(() => {
const labels = new Map<string, string>()
console.log('[useProfileLabels] Building labels map, profileData:', profileData.length, 'profiles:', profiles.length)
profileData.forEach(({ encoded }, index) => {
const profile = profiles[index]
if (profile) {
const displayName = profile.name || profile.display_name || profile.nip05
if (displayName) {
labels.set(encoded, `@${displayName}`)
console.log('[useProfileLabels] Set label:', encoded, '->', displayName)
}
}
})
console.log('[useProfileLabels] Final labels map size:', labels.size)
return labels
}, [profileData, profiles])
}

View File

@@ -16,8 +16,24 @@ const NOSTR_URI_REGEX = Tokens.nostrLink
* Extract all nostr URIs from text using applesauce helpers
*/
export function extractNostrUris(text: string): string[] {
const pointers = getContentPointers(text)
return pointers.map(pointer => encodeDecodeResult(pointer))
console.log('[nostrUriResolver] extractNostrUris called, text length:', text?.length || 0)
try {
const pointers = getContentPointers(text)
console.log('[nostrUriResolver] Found pointers:', pointers.length)
const result = pointers.map(pointer => {
try {
return encodeDecodeResult(pointer)
} catch (err) {
console.error('[nostrUriResolver] Error encoding pointer:', err, pointer)
return null
}
}).filter((v): v is string => v !== null)
console.log('[nostrUriResolver] Extracted URIs:', result.length)
return result
} catch (err) {
console.error('[nostrUriResolver] Error in extractNostrUris:', err)
return []
}
}
/**