85 lines
No EOL
2.2 KiB
TypeScript
85 lines
No EOL
2.2 KiB
TypeScript
import { ref, computed } from 'vue'
|
|
import { useNostrFeed, type NostrFeedConfig } from './useNostrFeed'
|
|
|
|
export function useNostrFeedPreloader(config: NostrFeedConfig = {}) {
|
|
const feed = useNostrFeed(config)
|
|
|
|
// Preload state
|
|
const isPreloading = ref(false)
|
|
const isPreloaded = ref(false)
|
|
const preloadError = ref<Error | null>(null)
|
|
|
|
// Check if feed data is available in localStorage
|
|
const hasStoredData = computed(() => {
|
|
const storageKey = `nostr-feed-${config.feedType || 'all'}`
|
|
const storedData = localStorage.getItem(storageKey)
|
|
return storedData !== null
|
|
})
|
|
|
|
// Check if we should show loading (only if no cached data and actually loading)
|
|
const shouldShowLoading = computed(() => {
|
|
return (feed.isLoading.value || isPreloading.value) && feed.notes.value.length === 0
|
|
})
|
|
|
|
// Preload feed data
|
|
const preloadFeed = async () => {
|
|
if (isPreloaded.value || isPreloading.value) {
|
|
return
|
|
}
|
|
|
|
try {
|
|
isPreloading.value = true
|
|
preloadError.value = null
|
|
|
|
console.log('Preloading Nostr feed...')
|
|
|
|
// Connect and load notes
|
|
await feed.connectToFeed()
|
|
await feed.loadNotes()
|
|
|
|
// Subscribe to updates
|
|
feed.subscribeToFeedUpdates()
|
|
|
|
isPreloaded.value = true
|
|
console.log('Nostr feed preloaded successfully')
|
|
|
|
} catch (error) {
|
|
console.error('Failed to preload Nostr feed:', error)
|
|
preloadError.value = error instanceof Error ? error : new Error('Failed to preload feed')
|
|
} finally {
|
|
isPreloading.value = false
|
|
}
|
|
}
|
|
|
|
// Reset preload state
|
|
const resetPreload = () => {
|
|
isPreloaded.value = false
|
|
isPreloading.value = false
|
|
preloadError.value = null
|
|
feed.resetFeedState()
|
|
}
|
|
|
|
// Auto-preload if we have stored data
|
|
const autoPreload = async () => {
|
|
if (hasStoredData.value && !isPreloaded.value && !isPreloading.value) {
|
|
await preloadFeed()
|
|
}
|
|
}
|
|
|
|
return {
|
|
// State
|
|
isPreloading: computed(() => isPreloading.value),
|
|
isPreloaded: computed(() => isPreloaded.value),
|
|
preloadError: computed(() => preloadError.value),
|
|
hasStoredData,
|
|
shouldShowLoading,
|
|
|
|
// Actions
|
|
preloadFeed,
|
|
resetPreload,
|
|
autoPreload,
|
|
|
|
// Expose feed methods
|
|
...feed
|
|
}
|
|
}
|