web-app/src/modules/nostr-feed/composables/useFeed.ts
padreug 6217e3b70a Add FeedService and integrate into NostrFeed module
- Introduced FeedService to manage feed functionality, including subscription and deduplication of posts.
- Updated NostrFeed module to register FeedService in the DI container and initialize it during installation.
- Refactored useFeed composable to utilize FeedService for managing feed state and loading posts.
- Enhanced NostrFeed component to display posts and handle loading states more effectively.
- Changed Home.vue to use the 'all' feed type for broader content display.

These changes improve the modularity and functionality of the feed system, providing a more robust user experience.
2025-09-23 23:59:37 +02:00

86 lines
No EOL
2.2 KiB
TypeScript

import { computed, ref, onMounted, onUnmounted } from 'vue'
import { injectService, SERVICE_TOKENS } from '@/core/di-container'
import type { FeedService, FeedConfig } from '../services/FeedService'
export interface UseFeedConfig {
feedType: 'announcements' | 'general' | 'mentions' | 'events' | 'all'
maxPosts?: number
refreshInterval?: number
adminPubkeys?: string[]
}
export function useFeed(config: UseFeedConfig) {
const feedService = injectService<FeedService>(SERVICE_TOKENS.FEED_SERVICE)
console.log('useFeed: FeedService injected:', !!feedService)
if (!feedService) {
console.error('useFeed: FeedService not available from DI container')
}
let refreshTimer: number | null = null
// Convert to FeedService config
const feedConfig: FeedConfig = {
feedType: config.feedType,
maxPosts: config.maxPosts,
adminPubkeys: config.adminPubkeys
}
const filteredPosts = computed(() => {
if (!feedService) return []
return feedService.getFilteredPosts(feedConfig)
})
const loadFeed = async () => {
console.log('useFeed: loadFeed called, feedService available:', !!feedService)
if (!feedService) {
console.error('FeedService not available')
return
}
console.log('useFeed: calling feedService.subscribeFeed with config:', feedConfig)
try {
await feedService.subscribeFeed(feedConfig)
console.log('useFeed: subscribeFeed completed')
} catch (err) {
console.error('Failed to load feed:', err)
}
}
const refreshFeed = async () => {
if (!feedService) return
await feedService.refreshFeed()
}
const startAutoRefresh = () => {
if (config.refreshInterval && config.refreshInterval > 0) {
refreshTimer = setInterval(refreshFeed, config.refreshInterval) as unknown as number
}
}
const stopAutoRefresh = () => {
if (refreshTimer) {
clearInterval(refreshTimer)
refreshTimer = null
}
}
// Lifecycle
onMounted(() => {
console.log('useFeed: onMounted called')
loadFeed()
startAutoRefresh()
})
onUnmounted(() => {
stopAutoRefresh()
})
return {
posts: filteredPosts,
isLoading: feedService?.isLoading ?? ref(false),
error: feedService?.error ?? ref(null),
refreshFeed,
loadFeed
}
}