import React, { useState, useEffect } from 'react'; import { Search as SearchIcon } from 'lucide-react'; 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 { Plugin } from '@/types/plugin'; type SearchNovelsSectionProps = { onNavigateToParseNovel?: () => void; }; export default function SearchNovelsSection({ onNavigateToParseNovel, }: SearchNovelsSectionProps) { const plugin = useAppStore(state => state.plugin); const setParseNovelPath = useAppStore(state => state.setParseNovelPath); const [searchTerm, setSearchTerm] = useState(''); const [currentPage, setCurrentPage] = useState(1); const [novels, setNovels] = useState([]); const [loading, setLoading] = useState(false); const [fetchError, setFetchError] = useState(''); const fetchNovels = async (page: number) => { if (plugin && searchTerm.trim()) { setLoading(true); setFetchError(''); try { const results = await plugin.searchNovels(searchTerm, page); setNovels(results); setCurrentPage(page); } catch (error) { const errorMessage = error instanceof Error ? error.message : 'Failed to fetch novels'; setFetchError(errorMessage); console.error('Error searching novels:', error); } finally { setLoading(false); } } }; useEffect(() => { setCurrentPage(1); setNovels([]); setSearchTerm(''); setFetchError(''); }, [plugin]); const handleKeyPress = (e: React.KeyboardEvent) => { if (e.key === 'Enter' && searchTerm.trim()) { fetchNovels(1); } }; const handleParseNovel = (path: string) => { setParseNovelPath(path, true); onNavigateToParseNovel?.(); }; return (

Search Novels

{plugin ? 'Search for novels by title or keywords' : 'Select a plugin to search novels'}

{currentPage > 1 && ( Page {currentPage} )}
setSearchTerm(e.target.value)} onKeyPress={handleKeyPress} className="flex-1" disabled={!plugin} />
{fetchError && (

{fetchError}

)} {loading ? (
{Array.from({ length: 12 }).map((_, index) => (
))}
) : novels.length === 0 ? (

{searchTerm ? 'No results found' : plugin ? 'Ready to search' : 'No plugin selected'}

{searchTerm ? 'Try adjusting your search term or check the spelling. Different sources may have different availability.' : plugin ? 'Enter a search term in the field above and click "Search" to find novels.' : 'Please select a plugin from the sidebar to get started.'}

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