debug: add comprehensive logging for reading position calculation and event publishing

- Add logs in useReadingPosition: scroll position calculation (throttled to 5% changes)
- Add logs for scheduling and triggering auto-save
- Add detailed logs in ContentPanel handleSavePosition
- Add logs in saveReadingPosition: event creation, signing, publishing
- Add logs in publishEvent: event store addition, relay status, publishing
- All logs prefixed with [progress] for easy filtering
- Shows complete flow from scroll → calculate → save → create event → publish to relays
This commit is contained in:
Gigi
2025-10-19 11:39:25 +02:00
parent bff43f4a28
commit 205879f948
4 changed files with 59 additions and 13 deletions

View File

@@ -151,7 +151,7 @@ const ContentPanel: React.FC<ContentPanelProps> = ({
// Callback to save reading position
const handleSavePosition = useCallback(async (position: number) => {
if (!activeAccount || !relayPool || !eventStore || !articleIdentifier) {
console.log('⏭️ [ContentPanel] Skipping save - missing requirements:', {
console.log('[progress] ⏭️ ContentPanel: Skipping save - missing requirements:', {
hasAccount: !!activeAccount,
hasRelayPool: !!relayPool,
hasEventStore: !!eventStore,
@@ -160,11 +160,18 @@ const ContentPanel: React.FC<ContentPanelProps> = ({
return
}
if (!settings?.syncReadingPosition) {
console.log('⏭️ [ContentPanel] Sync disabled in settings')
console.log('[progress] ⏭️ ContentPanel: Sync disabled in settings')
return
}
console.log('💾 [ContentPanel] Saving position:', Math.round(position * 100) + '%', 'for article:', selectedUrl?.slice(0, 50))
const scrollTop = window.pageYOffset || document.documentElement.scrollTop
console.log('[progress] 💾 ContentPanel: Saving position:', {
position,
percentage: Math.round(position * 100) + '%',
scrollTop,
articleIdentifier: articleIdentifier.slice(0, 50) + '...',
url: selectedUrl?.slice(0, 50)
})
try {
const factory = new EventFactory({ signer: activeAccount })
@@ -176,11 +183,12 @@ const ContentPanel: React.FC<ContentPanelProps> = ({
{
position,
timestamp: Math.floor(Date.now() / 1000),
scrollTop: window.pageYOffset || document.documentElement.scrollTop
scrollTop
}
)
console.log('[progress] ✅ ContentPanel: Save completed successfully')
} catch (error) {
console.error('❌ [ContentPanel] Failed to save reading position:', error)
console.error('[progress] ❌ ContentPanel: Failed to save reading position:', error)
}
}, [activeAccount, relayPool, eventStore, articleIdentifier, settings?.syncReadingPosition, selectedUrl])

View File

@@ -45,7 +45,9 @@ export const useReadingPosition = ({
}
// Schedule new save
console.log('[progress] ⏰ Scheduling save in', autoSaveInterval + 'ms for position:', Math.round(currentPosition * 100) + '%')
saveTimerRef.current = setTimeout(() => {
console.log('[progress] 💾 Auto-saving position:', Math.round(currentPosition * 100) + '%')
lastSavedPosition.current = currentPosition
onSave(currentPosition)
}, autoSaveInterval)
@@ -63,8 +65,11 @@ export const useReadingPosition = ({
// Save if position is meaningful (>= 5%)
if (position >= 0.05) {
console.log('[progress] 💾 Immediate save triggered for position:', Math.round(position * 100) + '%')
lastSavedPosition.current = position
onSave(position)
} else {
console.log('[progress] ⏭️ Skipping save - position too low:', Math.round(position * 100) + '%')
}
}, [syncEnabled, onSave, position])
@@ -89,6 +94,17 @@ export const useReadingPosition = ({
const isAtBottom = scrollTop + windowHeight >= documentHeight - 5
const clampedProgress = isAtBottom ? 1 : Math.max(0, Math.min(1, scrollProgress))
// Only log on significant changes (every 5%) to avoid flooding console
const prevPercent = Math.floor(position * 20) // Groups by 5%
const newPercent = Math.floor(clampedProgress * 20)
if (prevPercent !== newPercent) {
console.log('[progress] 📏 useReadingPosition:', Math.round(clampedProgress * 100) + '%', {
scrollTop,
documentHeight,
isAtBottom
})
}
setPosition(clampedProgress)
onPositionChange?.(clampedProgress)

View File

@@ -114,8 +114,8 @@ export async function saveReadingPosition(
articleIdentifier: string,
position: ReadingPosition
): Promise<void> {
console.log('💾 [ReadingProgress] Saving position:', {
identifier: articleIdentifier.slice(0, 32) + '...',
console.log('[progress] 💾 saveReadingPosition: Starting save:', {
identifier: articleIdentifier.slice(0, 50) + '...',
position: position.position,
positionPercent: Math.round(position.position * 100) + '%',
timestamp: position.timestamp,
@@ -133,6 +133,13 @@ export async function saveReadingPosition(
const tags = generateProgressTags(articleIdentifier)
console.log('[progress] 📝 Creating event with:', {
kind: READING_PROGRESS_KIND,
content: progressContent,
tags: tags.map(t => `[${t.join(', ')}]`).join(', '),
created_at: now
})
const draft = await factory.create(async () => ({
kind: READING_PROGRESS_KIND,
content: JSON.stringify(progressContent),
@@ -140,10 +147,20 @@ export async function saveReadingPosition(
created_at: now
}))
console.log('[progress] ✍️ Signing event...')
const signed = await factory.sign(draft)
console.log('[progress] 📡 Publishing event:', {
id: signed.id,
kind: signed.kind,
pubkey: signed.pubkey.slice(0, 8) + '...',
content: signed.content,
tags: signed.tags
})
await publishEvent(relayPool, eventStore, signed)
console.log('✅ [ReadingProgress] Saved, event ID:', signed.id.slice(0, 8))
console.log('[progress] ✅ Event published successfully, ID:', signed.id.slice(0, 16))
}
/**

View File

@@ -14,9 +14,12 @@ export async function publishEvent(
eventStore: IEventStore,
event: NostrEvent
): Promise<void> {
const isProgressEvent = event.kind === 39802
const logPrefix = isProgressEvent ? '[progress]' : ''
// Store the event in the local EventStore FIRST for immediate UI display
eventStore.add(event)
console.log('💾 Stored event in EventStore:', event.id.slice(0, 8), `(kind ${event.kind})`)
console.log(`${logPrefix} 💾 Stored event in EventStore:`, event.id.slice(0, 8), `(kind ${event.kind})`)
// Check current connection status - are we online or in flight mode?
const connectedRelays = Array.from(relayPool.relays.values())
@@ -32,12 +35,13 @@ export async function publishEvent(
const isLocalOnly = areAllRelaysLocal(expectedSuccessRelays)
console.log('📍 Event relay status:', {
console.log(`${logPrefix} 📍 Event relay status:`, {
targetRelays: RELAYS.length,
expectedSuccessRelays: expectedSuccessRelays.length,
isLocalOnly,
hasRemoteConnection,
eventId: event.id.slice(0, 8)
eventId: event.id.slice(0, 8),
connectedRelays: connectedRelays.length
})
// If we're in local-only mode, mark this event for later sync
@@ -46,12 +50,13 @@ export async function publishEvent(
}
// Publish to all configured relays in the background (non-blocking)
console.log(`${logPrefix} 📤 Publishing to relays:`, RELAYS)
relayPool.publish(RELAYS, event)
.then(() => {
console.log('✅ Event published to', RELAYS.length, 'relay(s):', event.id.slice(0, 8))
console.log(`${logPrefix} ✅ Event published to`, RELAYS.length, 'relay(s):', event.id.slice(0, 8))
})
.catch((error) => {
console.warn('⚠️ Failed to publish event to relays (event still saved locally):', error)
console.warn(`${logPrefix} ⚠️ Failed to publish event to relays (event still saved locally):`, error)
// Surface common bunker signing errors for debugging
if (error instanceof Error && error.message.includes('permission')) {