fix: make highlight creation resilient to offline/flight mode

- Wrap relay publish in try-catch to handle failures gracefully
- Attempt to publish to local relay even when no relays are connected
- Always return highlight object even if publish fails completely
- Add detailed logging to track publish status and failures
- Mark highlights as local-only when publish fails or only local relays available
- Ensure UI always displays newly created highlights immediately
This commit is contained in:
Gigi
2025-10-09 12:45:51 +01:00
parent 6636d540aa
commit bdab9c06e4
2 changed files with 37 additions and 13 deletions

View File

@@ -54,6 +54,8 @@ export const useHighlightCreation = ({
? currentArticle.content ? currentArticle.content
: readerContent?.markdown || readerContent?.html : readerContent?.markdown || readerContent?.html
console.log('🎯 Creating highlight...', { text: text.substring(0, 50) + '...' })
const newHighlight = await createHighlight( const newHighlight = await createHighlight(
text, text,
source, source,
@@ -64,12 +66,18 @@ export const useHighlightCreation = ({
settings settings
) )
console.log('✅ Highlight created successfully!') console.log('✅ Highlight created successfully!', {
highlightButtonRef.current?.clearSelection() id: newHighlight.id,
isLocalOnly: newHighlight.isLocalOnly,
publishedRelays: newHighlight.publishedRelays
})
highlightButtonRef.current?.clearSelection()
onHighlightCreated(newHighlight) onHighlightCreated(newHighlight)
} catch (error) { } catch (error) {
console.error('Failed to create highlight:', error) console.error('Failed to create highlight:', error)
// Re-throw to allow parent to handle
throw error
} }
}, [activeAccount, relayPool, currentArticle, selectedUrl, readerContent, onHighlightCreated, settings]) }, [activeAccount, relayPool, currentArticle, selectedUrl, readerContent, onHighlightCreated, settings])

View File

@@ -109,26 +109,42 @@ export async function createHighlight(
const connectedRelays = Array.from(relayPool.relays.values()).map(relay => relay.url) const connectedRelays = Array.from(relayPool.relays.values()).map(relay => relay.url)
// Determine which relays we're publishing to (intersection of RELAYS and connected relays) // Determine which relays we're publishing to (intersection of RELAYS and connected relays)
const publishingRelays = RELAYS.filter(url => connectedRelays.includes(url)) let publishingRelays = RELAYS.filter(url => connectedRelays.includes(url))
// If no relays are connected, fallback to just local relay if available // If no relays are connected, try local relay anyway (might still work)
const targetRelays = publishingRelays.length > 0 ? publishingRelays : RELAYS.filter(r => r.includes('localhost') || r.includes('127.0.0.1')) if (publishingRelays.length === 0) {
const localRelays = RELAYS.filter(r => r.includes('localhost') || r.includes('127.0.0.1'))
publishingRelays = localRelays.length > 0 ? localRelays : RELAYS
}
// Publish to relays (including local relay) const targetRelays = publishingRelays
await relayPool.publish(targetRelays, signedEvent)
// Attempt to publish - don't block UI on publish failures
let actualPublishedRelays: string[] = []
try {
await relayPool.publish(targetRelays, signedEvent)
actualPublishedRelays = targetRelays
console.log('✅ Highlight published to', targetRelays.length, 'relay(s):', targetRelays)
} catch (error) {
console.warn('⚠️ Failed to publish highlight to relays, but highlight will still be created locally:', error)
// Even if publish fails, treat it as local-only
const localRelays = RELAYS.filter(r => r.includes('localhost') || r.includes('127.0.0.1'))
actualPublishedRelays = localRelays
}
// Check if we're only publishing to local relays // Check if we're only publishing to local relays
const isLocalOnly = areAllRelaysLocal(targetRelays) const isLocalOnly = actualPublishedRelays.length === 0 || areAllRelaysLocal(actualPublishedRelays)
console.log(' Highlight published to', targetRelays.length, 'relays:', { console.log('📍 Highlight relay status:', {
relays: targetRelays, targetRelays,
actualPublishedRelays,
isLocalOnly, isLocalOnly,
event: signedEvent eventId: signedEvent.id
}) })
// Convert to Highlight with relay tracking info // Convert to Highlight with relay tracking info
const highlight = eventToHighlight(signedEvent) const highlight = eventToHighlight(signedEvent)
highlight.publishedRelays = targetRelays highlight.publishedRelays = actualPublishedRelays
highlight.isLocalOnly = isLocalOnly highlight.isLocalOnly = isLocalOnly
// Return the highlight for immediate UI updates // Return the highlight for immediate UI updates