refactor: replace custom NIP-19 parsing with applesauce helpers and add progressive profile resolution

- Replace custom regex patterns with Tokens.nostrLink from applesauce-content
- Use getContentPointers() and getPubkeyFromDecodeResult() from applesauce helpers
- Add useProfileLabels hook for shared profile resolution logic
- Implement progressive profile name updates in markdown articles
- Remove unused ContentWithResolvedProfiles component
- Simplify useMarkdownToHTML by extracting profile resolution to shared hook
- Fix TypeScript and ESLint errors
This commit is contained in:
Gigi
2025-11-02 20:01:51 +01:00
parent 5896a5d6db
commit b7cda7a351
7 changed files with 167 additions and 115 deletions

View File

@@ -0,0 +1,45 @@
import { useMemo } from 'react'
import { useEventModel } from 'applesauce-react/hooks'
import { Models, Helpers } from 'applesauce-core'
import { getContentPointers } from 'applesauce-factory/helpers'
const { getPubkeyFromDecodeResult, encodeDecodeResult } = Helpers
/**
* 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
*/
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)
}, [content])
// Fetch profiles for all found pubkeys (progressive loading)
const profiles = profileData.map(({ pubkey }) =>
useEventModel(Models.ProfileModel, pubkey ? [pubkey] : null)
)
// Build profile labels map that updates reactively as profiles load
return useMemo(() => {
const labels = new Map<string, string>()
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}`)
}
}
})
return labels
}, [profileData, profiles])
}