import { useRef } from 'react' import type { Article } from '@/types/nostr' import { ArticleCard } from './ArticleCard' import { ErrorState, EmptyState, Skeleton, Button } from './ui' import { t } from '@/lib/i18n' import { useArrowNavigation } from '@/hooks/useArrowNavigation' import { usePagination } from '@/hooks/usePagination' interface ArticlesListProps { articles: Article[] allArticles: Article[] loading: boolean error: string | null onUnlock: (article: Article) => void unlockedArticles: Set } const ITEMS_PER_PAGE = 10 function ArticleCardSkeleton(): React.ReactElement { return (
) } function LoadingState(): React.ReactElement { return (
{Array.from({ length: 3 }, (_, index) => ( ))}
) } function ArticlesEmptyState({ hasAny }: { hasAny: boolean }): React.ReactElement { return ( ) } function ArticlesErrorState({ error }: { error: string }): React.ReactElement { const errorObj = new Error(error) return ( { window.location.reload() }} onCheckConnection={() => { if (navigator.onLine) { window.location.reload() } else { // eslint-disable-next-line no-alert alert(t('errors.network.offline')) } }} /> ) } function PaginationInfo({ currentPage, totalPages }: { currentPage: number; totalPages: number }): React.ReactElement { const stepText = t('pagination.page', { current: currentPage, total: totalPages }) return (
{stepText}
) } function PaginationButtons({ onNext, onPrevious, hasNext, hasPrevious, }: { onNext: () => void onPrevious: () => void hasNext: boolean hasPrevious: boolean }): React.ReactElement { return (
) } function PaginationControls({ currentPage, totalPages, onNext, onPrevious, hasNext, hasPrevious, }: { currentPage: number totalPages: number onNext: () => void onPrevious: () => void hasNext: boolean hasPrevious: boolean }): React.ReactElement { return (
) } function ArticlesListItems({ articles, allArticles, onUnlock, unlockedArticles, }: { articles: Article[] allArticles: Article[] onUnlock: (article: Article) => void unlockedArticles: Set }): React.ReactElement { return (
{articles.map((article) => (
))}
) } function ArticlesListContent({ articles, allArticles, onUnlock, unlockedArticles, containerRef, }: { articles: Article[] allArticles: Article[] onUnlock: (article: Article) => void unlockedArticles: Set containerRef: React.RefObject }): React.ReactElement { const pagination = usePagination({ items: articles, itemsPerPage: ITEMS_PER_PAGE }) const showingText = t('pagination.showing', { current: pagination.paginatedItems.length, total: articles.length, all: allArticles.length, }) return (
{showingText}
{pagination.totalPages > 1 && ( )}
) } export function ArticlesList({ articles, allArticles, loading, error, onUnlock, unlockedArticles, }: ArticlesListProps): React.ReactElement { const containerRef = useRef(null) useArrowNavigation({ itemCount: articles.length, containerRef, enabled: !loading && articles.length > 0 }) if (loading) { return } if (error) { return } if (articles.length === 0) { return 0} /> } return ( ) }