story-research-zapwall/hooks/useArticles.ts
Nicolas Cantu 29cb20c614 Fix authors not loading from cache on startup
**Motivations:**
- Authors were not being loaded from cache, causing 'Aucun contenu trouvé' message even when authors exist
- useArticles only loaded articles from Nostr subscription, not authors from cache
- Authors should be loaded from cache first (cache-first architecture)

**Root causes:**
- useArticles hook only subscribed to articles from Nostr, not loading authors from cache
- No method to get all authors from cache
- Authors were only extracted from articles returned by subscription, which may not include author presentations

**Correctifs:**
- Added getAll method to objectCache to retrieve all objects of a type from cache
- Modified useArticles to load authors from cache on startup before subscribing to Nostr
- Authors are now loaded from cache and merged with articles from subscription
- totalSponsoring is calculated for each author when loading from cache

**Evolutions:**
- Authors are now available immediately from cache on page load
- Better user experience: no 'Aucun contenu trouvé' when authors exist in cache
- Cache-first architecture: authors loaded from cache before Nostr subscription

**Pages affectées:**
- lib/objectCache.ts
- hooks/useArticles.ts
2026-01-06 15:33:02 +01:00

136 lines
4.2 KiB
TypeScript

import { useEffect, useMemo, useRef, useState } from 'react'
import { nostrService } from '@/lib/nostr'
import type { Article } from '@/types/nostr'
import { applyFiltersAndSort } from '@/lib/articleFiltering'
import type { ArticleFilters } from '@/components/ArticleFilters'
import { t } from '@/lib/i18n'
import { objectCache } from '@/lib/objectCache'
import { getAuthorSponsoring } from '@/lib/sponsoring'
export function useArticles(searchQuery: string = '', filters: ArticleFilters | null = null): {
articles: Article[]
allArticles: Article[]
loading: boolean
error: string | null
loadArticleContent: (articleId: string, authorPubkey: string) => Promise<Article | null>
} {
const [articles, setArticles] = useState<Article[]>([])
const [loading, setLoading] = useState(true)
const [error, setError] = useState<string | null>(null)
const hasArticlesRef = useRef(false)
useEffect(() => {
setLoading(true)
setError(null)
// Load authors from cache first
const loadAuthorsFromCache = async (): Promise<void> => {
try {
const cachedAuthors = await objectCache.getAll('author')
const authors = cachedAuthors as Article[]
// Calculate totalSponsoring for each author
const authorsWithSponsoring = await Promise.all(
authors.map(async (author) => {
if (author.isPresentation && author.pubkey) {
author.totalSponsoring = await getAuthorSponsoring(author.pubkey)
}
return author
})
)
if (authorsWithSponsoring.length > 0) {
setArticles((prev) => {
// Merge with existing articles, avoiding duplicates
const existingIds = new Set(prev.map((a) => a.id))
const newAuthors = authorsWithSponsoring.filter((a) => !existingIds.has(a.id))
const merged = [...prev, ...newAuthors].sort((a, b) => b.createdAt - a.createdAt)
hasArticlesRef.current = merged.length > 0
return merged
})
}
} catch (error) {
console.error('Error loading authors from cache:', error)
}
}
void loadAuthorsFromCache()
const unsubscribe = nostrService.subscribeToArticles(
(article) => {
setArticles((prev) => {
if (prev.some((a) => a.id === article.id)) {
return prev
}
const next = [article, ...prev].sort((a, b) => b.createdAt - a.createdAt)
hasArticlesRef.current = next.length > 0
return next
})
setLoading(false)
},
50
)
const timeout = setTimeout(() => {
setLoading(false)
if (!hasArticlesRef.current) {
setError(t('common.error.noContent'))
}
}, 10000)
return () => {
unsubscribe()
clearTimeout(timeout)
}
}, [])
const loadArticleContent = async (articleId: string, authorPubkey: string): Promise<Article | null> => {
try {
const article = await nostrService.getArticleById(articleId)
if (article) {
// Try to decrypt article content using decryption key from private messages
const decryptedContent = await nostrService.getDecryptedArticleContent(articleId, authorPubkey)
if (decryptedContent) {
setArticles((prev) =>
prev.map((a) =>
(a.id === articleId
? { ...a, content: decryptedContent, paid: true }
: a)
)
)
}
return article
}
} catch (e) {
console.error('Error loading article content:', e)
setError(e instanceof Error ? e.message : 'Failed to load article')
}
return null
}
// Apply filters and sorting
const filteredArticles = useMemo(() => {
const effectiveFilters =
filters ??
({
authorPubkey: null,
sortBy: 'newest',
category: 'all',
} as const)
if (!filters && !searchQuery.trim()) {
return articles
}
return applyFiltersAndSort(articles, searchQuery, effectiveFilters)
}, [articles, searchQuery, filters])
return {
articles: filteredArticles,
allArticles: articles, // Return all articles for filters component
loading,
error,
loadArticleContent,
}
}