import React, { useState, useEffect } from 'react'; import { Filter, BookOpen } from 'lucide-react'; import { FiltersSheet } from '@/components/filters/filters-sheet'; import { NovelCard } from '@/components/novel-card'; import { Badge } from '@/components/ui/badge'; import { Button } from '@/components/ui/button'; import { Card } from '@/components/ui/card'; import { Input } from '@/components/ui/input'; import { Skeleton } from '@/components/ui/skeleton'; import { useAppStore } from '@/store'; import { FilterToValues, Filters } from '@libs/filterInputs'; import { Plugin } from '@/types/plugin'; type PopularNovelsSectionProps = { onNavigateToParseNovel?: () => void; }; export default function PopularNovelsSection({ onNavigateToParseNovel, }: PopularNovelsSectionProps) { const plugin = useAppStore(state => state.plugin); const setParseNovelPath = useAppStore(state => state.setParseNovelPath); const [novels, setNovels] = useState([]); const [loading, setLoading] = useState(false); const [currentIndex, setCurrentIndex] = useState(0); const [maxIndex, setMaxIndex] = useState(0); const [isLatest, setIsLatest] = useState(true); const [filtersOpen, setFiltersOpen] = useState(false); const [filterValues, setFilterValues] = useState< FilterToValues | undefined >(); const fetchNovelsByIndex = async (index: number) => { if (plugin && index) { setLoading(true); try { const fetchedNovels = await plugin.popularNovels(index, { filters: filterValues || {}, showLatestNovels: isLatest, }); if (fetchedNovels.length !== 0) { setCurrentIndex(index); if (index > maxIndex) { setMaxIndex(index); } setNovels(fetchedNovels); } } catch (error) { console.error('Error fetching novels:', error); } finally { setLoading(false); } } }; useEffect(() => { if (plugin) { setCurrentIndex(1); setMaxIndex(1); fetchNovelsByIndex(1); } }, [isLatest]); useEffect(() => { // Reset when changing plugins setCurrentIndex(0); setMaxIndex(0); setNovels([]); if (plugin?.filters) { const filters: FilterToValues = {}; for (const fKey in plugin.filters) { filters[fKey as keyof typeof filters] = { type: plugin.filters[fKey].type, value: plugin.filters[fKey].value, }; } setFilterValues(filters); } }, [plugin]); const handleParseNovel = (path: string) => { setParseNovelPath(path, true); onNavigateToParseNovel?.(); }; return (

Popular Novels

{plugin ? `Browse ${isLatest ? 'latest' : 'popular'} novels` : 'Select a plugin to browse novels'}

{['Latest', 'Popular'].map(option => ( setIsLatest(option === 'Latest')} > {option} ))} {currentIndex > 0 && (
Page ) => { const page = parseInt(e.target.value); if (page > 0 && page <= maxIndex) { fetchNovelsByIndex(page); } }} className="w-16 h-7 text-center text-xs" disabled={loading} /> of {maxIndex}+
)}
{loading ? (
{Array.from({ length: 12 }).map((_, index) => (
))}
) : novels.length === 0 ? (

{plugin ? 'No novels to display' : 'No plugin selected'}

{plugin ? 'Click the "Fetch" button above to load the latest or popular novels from this source.' : 'Please select a plugin from the sidebar to get started.'}

) : (
{novels.map((novel, index) => ( ))}
)}
fetchNovelsByIndex(1)} />
); }