mirror of
https://github.com/dergigi/boris.git
synced 2025-12-17 22:54:30 +01:00
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:
45
src/hooks/useProfileLabels.ts
Normal file
45
src/hooks/useProfileLabels.ts
Normal 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])
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user