debug: add comprehensive [progress] logging throughout reading progress flow

- Add logs in readingProgressController: processing events, emitting to listeners
- Add logs in Explore component: receiving updates, looking up progress
- Add logs in BlogPostCard: rendering with progress
- Add detailed logs in processReadingProgress: event parsing, naddr conversion
- All logs prefixed with [progress] for easy filtering
This commit is contained in:
Gigi
2025-10-19 11:30:57 +02:00
parent 2a7fffd594
commit bff43f4a28
4 changed files with 57 additions and 11 deletions

View File

@@ -33,6 +33,11 @@ const BlogPostCard: React.FC<BlogPostCardProps> = ({ post, href, level, readingP
} else if (readingProgress && readingProgress > 0 && readingProgress <= 0.10) { } else if (readingProgress && readingProgress > 0 && readingProgress <= 0.10) {
progressColor = 'var(--color-text)' // Neutral text color (started) progressColor = 'var(--color-text)' // Neutral text color (started)
} }
// Debug log
if (readingProgress !== undefined) {
console.log('[progress] 🎴 Card render:', post.title.slice(0, 30), '=> progress:', progressPercent + '%', 'color:', progressColor)
}
return ( return (
<Link <Link

View File

@@ -177,10 +177,15 @@ const Explore: React.FC<ExploreProps> = ({ relayPool, eventStore, settings, acti
// Subscribe to reading progress controller // Subscribe to reading progress controller
useEffect(() => { useEffect(() => {
// Get initial state immediately // Get initial state immediately
setReadingProgressMap(readingProgressController.getProgressMap()) const initialMap = readingProgressController.getProgressMap()
console.log('[progress] 🎯 Explore: Initial progress map size:', initialMap.size)
setReadingProgressMap(initialMap)
// Subscribe to updates // Subscribe to updates
const unsubProgress = readingProgressController.onProgress(setReadingProgressMap) const unsubProgress = readingProgressController.onProgress((newMap) => {
console.log('[progress] 🎯 Explore: Received progress update, size:', newMap.size)
setReadingProgressMap(newMap)
})
return () => { return () => {
unsubProgress() unsubProgress()
@@ -606,7 +611,10 @@ const Explore: React.FC<ExploreProps> = ({ relayPool, eventStore, settings, acti
// Helper to get reading progress for a post // Helper to get reading progress for a post
const getReadingProgress = useCallback((post: BlogPostPreview): number | undefined => { const getReadingProgress = useCallback((post: BlogPostPreview): number | undefined => {
const dTag = post.event.tags.find(t => t[0] === 'd')?.[1] const dTag = post.event.tags.find(t => t[0] === 'd')?.[1]
if (!dTag) return undefined if (!dTag) {
console.log('[progress] ⚠️ No d-tag for post:', post.title)
return undefined
}
try { try {
const naddr = nip19.naddrEncode({ const naddr = nip19.naddrEncode({
@@ -614,8 +622,11 @@ const Explore: React.FC<ExploreProps> = ({ relayPool, eventStore, settings, acti
pubkey: post.author, pubkey: post.author,
identifier: dTag identifier: dTag
}) })
return readingProgressMap.get(naddr) const progress = readingProgressMap.get(naddr)
console.log('[progress] 🔍 Looking up:', naddr.slice(0, 50) + '... =>', progress ? Math.round(progress * 100) + '%' : 'not found')
return progress
} catch (err) { } catch (err) {
console.error('[progress] ❌ Error encoding naddr:', err)
return undefined return undefined
} }
}, [readingProgressMap]) }, [readingProgressMap])

View File

@@ -27,19 +27,31 @@ export function processReadingProgress(
events: NostrEvent[], events: NostrEvent[],
readsMap: Map<string, ReadItem> readsMap: Map<string, ReadItem>
): void { ): void {
console.log('[progress] 🔧 processReadingProgress called with', events.length, 'events')
for (const event of events) { for (const event of events) {
if (event.kind !== READING_PROGRESS_KIND) continue if (event.kind !== READING_PROGRESS_KIND) {
console.log('[progress] ⏭️ Skipping event with wrong kind:', event.kind)
continue
}
const dTag = event.tags.find(t => t[0] === 'd')?.[1] const dTag = event.tags.find(t => t[0] === 'd')?.[1]
if (!dTag) continue if (!dTag) {
console.log('[progress] ⚠️ Event missing d-tag:', event.id.slice(0, 8))
continue
}
console.log('[progress] 📝 Processing event:', event.id.slice(0, 8), 'd-tag:', dTag.slice(0, 50))
try { try {
const content = JSON.parse(event.content) const content = JSON.parse(event.content)
const position = content.progress || 0 const position = content.progress || 0
console.log('[progress] 📊 Progress value:', position, '(' + Math.round(position * 100) + '%)')
// Validate progress is between 0 and 1 (NIP-85 requirement) // Validate progress is between 0 and 1 (NIP-85 requirement)
if (position < 0 || position > 1) { if (position < 0 || position > 1) {
console.warn('Invalid progress value (must be 0-1):', position, 'event:', event.id.slice(0, 8)) console.warn('[progress] ❌ Invalid progress value (must be 0-1):', position, 'event:', event.id.slice(0, 8))
continue continue
} }
@@ -64,11 +76,13 @@ export function processReadingProgress(
}) })
itemId = naddr itemId = naddr
itemType = 'article' itemType = 'article'
console.log('[progress] ✅ Converted coordinate to naddr:', naddr.slice(0, 50))
} catch (e) { } catch (e) {
console.warn('Failed to encode naddr from coordinate:', dTag) console.warn('[progress] ❌ Failed to encode naddr from coordinate:', dTag)
continue continue
} }
} else { } else {
console.warn('[progress] ⚠️ Invalid coordinate format:', dTag)
continue continue
} }
} else if (dTag.startsWith('url:')) { } else if (dTag.startsWith('url:')) {
@@ -78,12 +92,13 @@ export function processReadingProgress(
itemUrl = atob(encoded.replace(/-/g, '+').replace(/_/g, '/')) itemUrl = atob(encoded.replace(/-/g, '+').replace(/_/g, '/'))
itemId = itemUrl itemId = itemUrl
itemType = 'external' itemType = 'external'
console.log('[progress] ✅ Decoded URL:', itemUrl.slice(0, 50))
} catch (e) { } catch (e) {
console.warn('Failed to decode URL from d tag:', dTag) console.warn('[progress] ❌ Failed to decode URL from d tag:', dTag)
continue continue
} }
} else { } else {
// Unknown format, skip console.warn('[progress] ⚠️ Unknown d-tag format:', dTag)
continue continue
} }
@@ -99,11 +114,16 @@ export function processReadingProgress(
readingProgress: position, readingProgress: position,
readingTimestamp: timestamp readingTimestamp: timestamp
}) })
console.log('[progress] ✅ Added/updated item in readsMap:', itemId.slice(0, 50), '=', Math.round(position * 100) + '%')
} else {
console.log('[progress] ⏭️ Skipping older event for:', itemId.slice(0, 50))
} }
} catch (error) { } catch (error) {
console.warn('Failed to parse reading progress event:', error) console.warn('[progress] ❌ Failed to parse reading progress event:', error)
} }
} }
console.log('[progress] 🏁 processReadingProgress finished, readsMap size:', readsMap.size)
} }
/** /**

View File

@@ -42,6 +42,7 @@ class ReadingProgressController {
} }
private emitProgress(progressMap: Map<string, number>): void { private emitProgress(progressMap: Map<string, number>): void {
console.log('[progress] 📡 Emitting to', this.progressListeners.length, 'listeners with', progressMap.size, 'items')
this.progressListeners.forEach(cb => cb(new Map(progressMap))) this.progressListeners.forEach(cb => cb(new Map(progressMap)))
} }
@@ -174,6 +175,8 @@ class ReadingProgressController {
* Process events and update progress map * Process events and update progress map
*/ */
private processEvents(events: any[]): void { private processEvents(events: any[]): void {
console.log('[progress] 🔄 Processing', events.length, 'events')
const readsMap = new Map<string, ReadItem>() const readsMap = new Map<string, ReadItem>()
// Merge with existing progress // Merge with existing progress
@@ -186,17 +189,24 @@ class ReadingProgressController {
}) })
} }
console.log('[progress] 📦 Starting with', readsMap.size, 'existing items')
// Process new events // Process new events
processReadingProgress(events, readsMap) processReadingProgress(events, readsMap)
console.log('[progress] 📦 After processing:', readsMap.size, 'items')
// Convert back to progress map (naddr -> progress) // Convert back to progress map (naddr -> progress)
const newProgressMap = new Map<string, number>() const newProgressMap = new Map<string, number>()
for (const [id, item] of readsMap.entries()) { for (const [id, item] of readsMap.entries()) {
if (item.readingProgress !== undefined && item.type === 'article') { if (item.readingProgress !== undefined && item.type === 'article') {
newProgressMap.set(id, item.readingProgress) newProgressMap.set(id, item.readingProgress)
console.log('[progress] ✅ Added:', id.slice(0, 50) + '...', '=', Math.round(item.readingProgress * 100) + '%')
} }
} }
console.log('[progress] 📊 Final progress map size:', newProgressMap.size)
this.currentProgressMap = newProgressMap this.currentProgressMap = newProgressMap
this.emitProgress(this.currentProgressMap) this.emitProgress(this.currentProgressMap)
} }