refactor: clean up npub/nprofile display implementation

- Remove all debug console.log/error statements (39+) and ts() helpers
- Eliminate redundant localStorage cache check in useProfileLabels
- Standardize fallback display format using getNpubFallbackDisplay() utility
- Update ResolvedMention to use npub format consistently
This commit is contained in:
Gigi
2025-11-02 21:26:06 +01:00
parent 5d36d6de4f
commit 5e1ed6b8de
4 changed files with 40 additions and 139 deletions

View File

@@ -3,6 +3,7 @@ import { Link } from 'react-router-dom'
import { useEventModel } from 'applesauce-react/hooks'
import { Models, Helpers } from 'applesauce-core'
import { decode, npubEncode } from 'nostr-tools/nip19'
import { getNpubFallbackDisplay } from '../utils/nostrUriResolver'
const { getPubkeyFromDecodeResult } = Helpers
@@ -20,7 +21,7 @@ const ResolvedMention: React.FC<ResolvedMentionProps> = ({ encoded }) => {
}
const profile = pubkey ? useEventModel(Models.ProfileModel, [pubkey]) : undefined
const display = profile?.name || profile?.display_name || profile?.nip05 || (pubkey ? `${pubkey.slice(0, 8)}...` : encoded)
const display = profile?.name || profile?.display_name || profile?.nip05 || (pubkey ? getNpubFallbackDisplay(pubkey) : encoded)
const npub = pubkey ? npubEncode(pubkey) : undefined
if (npub) {

View File

@@ -4,13 +4,6 @@ import { extractNaddrUris, replaceNostrUrisInMarkdownWithProfileLabels } from '.
import { fetchArticleTitles } from '../services/articleTitleResolver'
import { useProfileLabels } from './useProfileLabels'
// Helper to add timestamps to logs
const ts = () => {
const now = new Date()
const ms = now.getMilliseconds().toString().padStart(3, '0')
return `${now.toLocaleTimeString('en-US', { hour12: false })}.${ms}`
}
/**
* Hook to convert markdown to HTML using a hidden ReactMarkdown component
* Also processes nostr: URIs in the markdown and resolves article titles
@@ -30,11 +23,6 @@ export const useMarkdownToHTML = (
// Resolve profile labels progressively as profiles load
const profileLabels = useProfileLabels(markdown || '', relayPool)
// Log when markdown or profile labels change (but throttle excessive logs)
useEffect(() => {
console.log(`[${ts()}] [npub-resolve] useMarkdownToHTML: markdown length:`, markdown?.length || 0, 'hasRelayPool:', !!relayPool, 'Profile labels size:', profileLabels.size)
}, [markdown?.length, profileLabels.size, relayPool])
// Fetch article titles
useEffect(() => {
@@ -57,8 +45,7 @@ export const useMarkdownToHTML = (
if (!isCancelled) {
setArticleTitles(titlesMap)
}
} catch (error) {
console.warn('Failed to fetch article titles:', error)
} catch {
if (!isCancelled) setArticleTitles(new Map())
}
}
@@ -80,8 +67,6 @@ export const useMarkdownToHTML = (
let isCancelled = false
const processMarkdown = () => {
console.log(`[${ts()}] [npub-resolve] useMarkdownToHTML: Processing markdown, length:`, markdown.length)
console.log(`[${ts()}] [npub-resolve] useMarkdownToHTML: Profile labels:`, profileLabels.size, 'Article titles:', articleTitles.size)
try {
// Replace nostr URIs with profile labels (progressive) and article titles
const processed = replaceNostrUrisInMarkdownWithProfileLabels(
@@ -89,13 +74,11 @@ export const useMarkdownToHTML = (
profileLabels,
articleTitles
)
console.log(`[${ts()}] [npub-resolve] useMarkdownToHTML: Processed markdown length:`, processed.length)
if (isCancelled) return
setProcessedMarkdown(processed)
} catch (err) {
console.error(`[${ts()}] [npub-resolve] useMarkdownToHTML: Error processing markdown:`, err)
} catch {
if (!isCancelled) {
setProcessedMarkdown(markdown) // Fallback to original
}

View File

@@ -1,4 +1,4 @@
import { useMemo, useState, useEffect, useRef } from 'react'
import { useMemo, useState, useEffect } from 'react'
import { Hooks } from 'applesauce-react'
import { Helpers, IEventStore } from 'applesauce-core'
import { getContentPointers } from 'applesauce-factory/helpers'
@@ -7,13 +7,6 @@ import { fetchProfiles, loadCachedProfiles } from '../services/profileService'
const { getPubkeyFromDecodeResult, encodeDecodeResult } = Helpers
// Helper to add timestamps to logs
const ts = () => {
const now = new Date()
const ms = now.getMilliseconds().toString().padStart(3, '0')
return `${now.toLocaleTimeString('en-US', { hour12: false })}.${ms}`
}
/**
* Hook to resolve profile labels from content containing npub/nprofile identifiers
* Returns a Map of encoded identifier -> display name that updates progressively as profiles load
@@ -23,12 +16,9 @@ export function useProfileLabels(content: string, relayPool?: RelayPool | null):
// Extract profile pointers (npub and nprofile) using applesauce helpers
const profileData = useMemo(() => {
console.log(`[${ts()}] [npub-resolve] Processing content, length:`, content?.length || 0)
try {
const pointers = getContentPointers(content)
console.log(`[${ts()}] [npub-resolve] Found pointers:`, pointers.length, 'types:', pointers.map(p => p.type))
const filtered = pointers.filter(p => p.type === 'npub' || p.type === 'nprofile')
console.log(`[${ts()}] [npub-resolve] Profile pointers:`, filtered.length)
const result: Array<{ pubkey: string; encoded: string }> = []
filtered.forEach(pointer => {
try {
@@ -37,14 +27,12 @@ export function useProfileLabels(content: string, relayPool?: RelayPool | null):
if (pubkey && encoded) {
result.push({ pubkey, encoded: encoded as string })
}
} catch (err) {
console.error(`[${ts()}] [npub-resolve] Error processing pointer:`, err, pointer)
} catch {
// Ignore errors, continue processing other pointers
}
})
console.log(`[${ts()}] [npub-resolve] Profile data after filtering:`, result.length)
return result
} catch (err) {
console.error(`[${ts()}] [npub-resolve] Error extracting pointers:`, err)
} catch {
return []
}
}, [content])
@@ -74,53 +62,31 @@ export function useProfileLabels(content: string, relayPool?: RelayPool | null):
}
})
if (labels.size > 0) {
console.log(`[${ts()}] [npub-resolve] Initial labels from cache (useMemo):`, labels.size, 'labels')
}
return labels
}, [profileData])
const [profileLabels, setProfileLabels] = useState<Map<string, string>>(initialLabels)
const lastLoggedSize = useRef<number>(0)
// Build initial labels: localStorage cache -> eventStore -> fetch from relays
useEffect(() => {
const startTime = Date.now()
console.log(`[${ts()}] [npub-resolve] Building labels, profileData:`, profileData.length, 'hasEventStore:', !!eventStore, 'hasRelayPool:', !!relayPool)
// Extract all pubkeys
const allPubkeys = profileData.map(({ pubkey }) => pubkey)
if (allPubkeys.length === 0) {
console.log(`[${ts()}] [npub-resolve] No pubkeys to resolve, clearing labels`)
setProfileLabels(new Map())
return
}
// First, check localStorage cache (synchronous, instant)
const cacheStartTime = Date.now()
const cachedProfiles = loadCachedProfiles(allPubkeys)
const cacheDuration = Date.now() - cacheStartTime
console.log(`[${ts()}] [npub-resolve] Found in localStorage cache:`, cachedProfiles.size, 'out of', allPubkeys.length, 'in', cacheDuration, 'ms')
// Log which pubkeys were found in cache
if (cachedProfiles.size > 0) {
cachedProfiles.forEach((_profile, pubkey) => {
console.log(`[${ts()}] [npub-resolve] Cached profile found:`, pubkey.slice(0, 16) + '...')
})
}
// Add cached profiles to EventStore for consistency
const cachedProfiles = loadCachedProfiles(allPubkeys)
if (eventStore) {
for (const profile of cachedProfiles.values()) {
eventStore.add(profile)
}
}
// Build labels from localStorage cache and eventStore (initialLabels already has cache, add eventStore)
// Start with labels from initial cache lookup (in useMemo)
// Note: initialLabels should already have all cached profiles, but we rebuild here
// to also check EventStore and handle any profiles that weren't in cache
// Build labels from localStorage cache and eventStore
// initialLabels already has all cached profiles, so we only need to check eventStore
const labels = new Map<string, string>(initialLabels)
const pubkeysToFetch: string[] = []
@@ -131,20 +97,12 @@ export function useProfileLabels(content: string, relayPool?: RelayPool | null):
return
}
// Check EventStore for profiles that weren't in cache
let profileEvent: { content: string } | null = null
let foundSource = ''
// Check localStorage cache first (should already be checked in initialLabels, but double-check)
const cachedProfile = cachedProfiles.get(pubkey)
if (cachedProfile) {
profileEvent = cachedProfile
foundSource = 'localStorage cache'
} else if (eventStore) {
// Then check EventStore (in-memory from current session)
if (eventStore) {
const eventStoreProfile = eventStore.getEvent(pubkey + ':0')
if (eventStoreProfile) {
profileEvent = eventStoreProfile
foundSource = 'eventStore'
}
}
@@ -154,13 +112,10 @@ export function useProfileLabels(content: string, relayPool?: RelayPool | null):
const displayName = profileData.display_name || profileData.name || profileData.nip05
if (displayName) {
labels.set(encoded, `@${displayName}`)
console.log(`[${ts()}] [npub-resolve] Found in ${foundSource}:`, encoded.slice(0, 30) + '...', '->', displayName)
} else {
console.log(`[${ts()}] [npub-resolve] Profile from ${foundSource} has no display name, will fetch:`, pubkey.slice(0, 16) + '...')
pubkeysToFetch.push(pubkey)
}
} catch (err) {
console.error(`[${ts()}] [npub-resolve] Error parsing profile from ${foundSource}:`, err)
} catch {
pubkeysToFetch.push(pubkey)
}
} else {
@@ -168,98 +123,54 @@ export function useProfileLabels(content: string, relayPool?: RelayPool | null):
}
})
// Update labels with what we found in localStorage cache and eventStore
const initialResolveTime = Date.now() - startTime
console.log(`[${ts()}] [npub-resolve] Initial resolution complete:`, labels.size, 'labels resolved in', initialResolveTime, 'ms. Will fetch', pubkeysToFetch.length, 'missing profiles.')
setProfileLabels(new Map(labels))
// Fetch missing profiles asynchronously
if (pubkeysToFetch.length > 0 && relayPool && eventStore) {
const fetchStartTime = Date.now()
console.log(`[${ts()}] [npub-resolve] Fetching`, pubkeysToFetch.length, 'missing profiles')
fetchProfiles(relayPool, eventStore as unknown as IEventStore, pubkeysToFetch)
.then((fetchedProfiles) => {
const fetchDuration = Date.now() - fetchStartTime
console.log(`[${ts()}] [npub-resolve] fetchProfiles returned`, fetchedProfiles.length, 'profiles in', fetchDuration, 'ms')
// First, use the profiles returned from fetchProfiles directly
const updatedLabels = new Map(labels)
const fetchedProfilesByPubkey = new Map(fetchedProfiles.map(p => [p.pubkey, p]))
let resolvedFromArray = 0
let resolvedFromStore = 0
let withNames = 0
let withoutNames = 0
let missingFromStore = 0
profileData.forEach(({ encoded, pubkey }) => {
if (!updatedLabels.has(encoded)) {
// First, try to use the profile from the returned array
const fetchedProfile = fetchedProfilesByPubkey.get(pubkey)
if (fetchedProfile) {
resolvedFromArray++
try {
const profileData = JSON.parse(fetchedProfile.content || '{}') as { name?: string; display_name?: string; nip05?: string }
const displayName = profileData.display_name || profileData.name || profileData.nip05
if (displayName) {
updatedLabels.set(encoded, `@${displayName}`)
withNames++
console.log(`[${ts()}] [npub-resolve] Resolved from fetched array:`, encoded.slice(0, 30) + '...', '->', displayName)
} else {
withoutNames++
if (withoutNames <= 3) {
console.log(`[${ts()}] [npub-resolve] Fetched profile has no name/display_name/nip05:`, encoded.slice(0, 30) + '...', 'content keys:', Object.keys(profileData))
}
}
} catch (err) {
console.error(`[${ts()}] [npub-resolve] Error parsing fetched profile for`, encoded.slice(0, 30) + '...', err)
} catch {
// Ignore parsing errors
}
} else if (eventStore) {
// Fallback: check eventStore (in case fetchProfiles stored but didn't return)
const profileEvent = eventStore.getEvent(pubkey + ':0')
if (profileEvent) {
resolvedFromStore++
try {
const profileData = JSON.parse(profileEvent.content || '{}') as { name?: string; display_name?: string; nip05?: string }
const displayName = profileData.display_name || profileData.name || profileData.nip05
if (displayName) {
updatedLabels.set(encoded, `@${displayName}`)
withNames++
console.log(`[${ts()}] [npub-resolve] Resolved from eventStore:`, encoded.slice(0, 30) + '...', '->', displayName)
}
} catch (err) {
console.error(`[${ts()}] [npub-resolve] Error parsing profile event for`, encoded.slice(0, 30) + '...', err)
}
} else {
missingFromStore++
if (missingFromStore <= 3) {
console.log(`[${ts()}] [npub-resolve] Profile not found in array or eventStore:`, pubkey.slice(0, 16) + '...')
} catch {
// Ignore parsing errors
}
}
} else {
missingFromStore++
}
}
})
const totalDuration = Date.now() - startTime
console.log(`[${ts()}] [npub-resolve] After fetch - resolved:`, updatedLabels.size, 'total | from array:', resolvedFromArray, '| from store:', resolvedFromStore, '| with names:', withNames, '| without names:', withoutNames, '| missing:', missingFromStore, '| out of', profileData.length, '| total time:', totalDuration, 'ms')
setProfileLabels(updatedLabels)
})
.catch(err => {
const fetchDuration = Date.now() - fetchStartTime
console.error(`[${ts()}] [npub-resolve] Error fetching profiles after`, fetchDuration, 'ms:', err)
.catch(() => {
// Silently handle fetch errors
})
}
}, [profileData, eventStore, relayPool, initialLabels])
// Only log when size actually changes to reduce noise
useEffect(() => {
if (profileLabels.size !== lastLoggedSize.current) {
console.log(`[${ts()}] [npub-resolve] Final labels map size:`, profileLabels.size)
lastLoggedSize.current = profileLabels.size
}
}, [profileLabels.size])
return profileLabels
}

View File

@@ -4,13 +4,6 @@ import { Tokens } from 'applesauce-content/helpers'
import { getContentPointers } from 'applesauce-factory/helpers'
import { encodeDecodeResult } from 'applesauce-core/helpers'
// Helper to add timestamps to logs
const ts = () => {
const now = new Date()
const ms = now.getMilliseconds().toString().padStart(3, '0')
return `${now.toLocaleTimeString('en-US', { hour12: false })}.${ms}`
}
/**
* Regular expression to match nostr: URIs and bare NIP-19 identifiers
* Uses applesauce Tokens.nostrLink which includes word boundary checks
@@ -23,10 +16,8 @@ const NOSTR_URI_REGEX = Tokens.nostrLink
* Extract all nostr URIs from text using applesauce helpers
*/
export function extractNostrUris(text: string): string[] {
console.log(`[${ts()}] [npub-resolve] extractNostrUris: text length:`, text?.length || 0)
try {
const pointers = getContentPointers(text)
console.log(`[${ts()}] [npub-resolve] extractNostrUris: Found pointers:`, pointers.length)
const result: string[] = []
pointers.forEach(pointer => {
try {
@@ -34,14 +25,12 @@ export function extractNostrUris(text: string): string[] {
if (encoded) {
result.push(encoded)
}
} catch (err) {
console.error(`[${ts()}] [npub-resolve] extractNostrUris: Error encoding pointer:`, err, pointer)
} catch {
// Ignore encoding errors, continue processing other pointers
}
})
console.log(`[${ts()}] [npub-resolve] extractNostrUris: Extracted URIs:`, result.length)
return result
} catch (err) {
console.error(`[${ts()}] [npub-resolve] extractNostrUris: Error:`, err)
} catch {
return []
}
}
@@ -128,6 +117,23 @@ export function getNostrUriLabel(encoded: string): string {
}
}
/**
* Get a standardized fallback display name for a pubkey when profile has no name
* Returns npub format: @abc1234...
* @param pubkey The pubkey in hex format
* @returns Formatted npub display string
*/
export function getNpubFallbackDisplay(pubkey: string): string {
try {
const npub = npubEncode(pubkey)
// Remove "npub1" prefix (5 chars) and show next 7 chars
return `@${npub.slice(5, 12)}...`
} catch {
// Fallback to shortened pubkey if encoding fails
return `@${pubkey.slice(0, 8)}...`
}
}
/**
* Process markdown to replace nostr URIs while skipping those inside markdown links
* This prevents nested markdown link issues when nostr identifiers appear in URLs