'use client'; import { useState, useEffect } from 'react'; import { useRouter } from 'next/navigation'; import { searchApi, storyApi } from '../../lib/api'; import { Story, Tag, FacetCount } from '../../types/api'; import AppLayout from '../../components/layout/AppLayout'; import { Input } from '../../components/ui/Input'; import Button from '../../components/ui/Button'; import StoryMultiSelect from '../../components/stories/StoryMultiSelect'; import TagFilter from '../../components/stories/TagFilter'; import LoadingSpinner from '../../components/ui/LoadingSpinner'; type ViewMode = 'grid' | 'list'; type SortOption = 'createdAt' | 'title' | 'authorName' | 'rating' | 'wordCount' | 'lastRead'; export default function LibraryPage() { const router = useRouter(); const [stories, setStories] = useState([]); const [tags, setTags] = useState([]); const [loading, setLoading] = useState(false); const [searchLoading, setSearchLoading] = useState(false); const [randomLoading, setRandomLoading] = useState(false); const [searchQuery, setSearchQuery] = useState(''); const [selectedTags, setSelectedTags] = useState([]); const [viewMode, setViewMode] = useState('list'); const [sortOption, setSortOption] = useState('lastRead'); const [sortDirection, setSortDirection] = useState<'asc' | 'desc'>('desc'); const [page, setPage] = useState(0); const [totalPages, setTotalPages] = useState(1); const [totalElements, setTotalElements] = useState(0); const [refreshTrigger, setRefreshTrigger] = useState(0); // Convert facet counts to Tag objects for the UI const convertFacetsToTags = (facets?: Record): Tag[] => { if (!facets || !facets.tagNames) { return []; } return facets.tagNames.map(facet => ({ id: facet.value, // Use tag name as ID since we don't have actual IDs from search results name: facet.value, storyCount: facet.count })); }; // Debounce search to avoid too many API calls useEffect(() => { const debounceTimer = setTimeout(() => { const performSearch = async () => { try { // Use searchLoading for background search, loading only for initial load const isInitialLoad = stories.length === 0 && !searchQuery && selectedTags.length === 0; if (isInitialLoad) { setLoading(true); } else { setSearchLoading(true); } // Always use search API for consistency - use '*' for match-all when no query const result = await searchApi.search({ query: searchQuery.trim() || '*', page: page, // Use 0-based pagination consistently size: 20, tags: selectedTags.length > 0 ? selectedTags : undefined, sortBy: sortOption, sortDir: sortDirection, facetBy: ['tagNames'], // Request tag facets for the filter UI }); const currentStories = result?.results || []; setStories(currentStories); setTotalPages(Math.ceil((result?.totalHits || 0) / 20)); setTotalElements(result?.totalHits || 0); // Update tags from facets - these represent all matching stories, not just current page const resultTags = convertFacetsToTags(result?.facets); setTags(resultTags); } catch (error) { console.error('Failed to load stories:', error); setStories([]); } finally { setLoading(false); setSearchLoading(false); } }; performSearch(); }, searchQuery ? 500 : 0); // 500ms debounce for search, immediate for other changes return () => clearTimeout(debounceTimer); }, [searchQuery, selectedTags, page, sortOption, sortDirection, refreshTrigger]); // Reset page when search or filters change const resetPage = () => { if (page !== 0) { setPage(0); } }; const handleTagToggle = (tagName: string) => { setSelectedTags(prev => { const newTags = prev.includes(tagName) ? prev.filter(t => t !== tagName) : [...prev, tagName]; resetPage(); return newTags; }); }; const handleSearchChange = (e: React.ChangeEvent) => { setSearchQuery(e.target.value); resetPage(); }; const handleSortChange = (newSortOption: SortOption) => { setSortOption(newSortOption); // Set appropriate default direction for the sort option if (newSortOption === 'title' || newSortOption === 'authorName') { setSortDirection('asc'); // Alphabetical fields default to ascending } else { setSortDirection('desc'); // Numeric/date fields default to descending } resetPage(); }; const toggleSortDirection = () => { setSortDirection(prev => prev === 'asc' ? 'desc' : 'asc'); resetPage(); }; const clearFilters = () => { setSearchQuery(''); setSelectedTags([]); resetPage(); }; const handleStoryUpdate = () => { // Trigger reload by incrementing refresh trigger setRefreshTrigger(prev => prev + 1); }; const handleRandomStory = async () => { try { setRandomLoading(true); // Build filter parameters based on current UI state const filters: Record = {}; // Include search query if present if (searchQuery && searchQuery.trim() !== '' && searchQuery !== '*') { filters.searchQuery = searchQuery.trim(); } // Include all selected tags if (selectedTags.length > 0) { filters.tags = selectedTags; } console.log('Getting random story with filters:', filters); const randomStory = await storyApi.getRandomStory(filters); if (!randomStory) { // No stories match the current filters alert('No stories match your current filters. Try clearing some filters or adding more stories to your library.'); return; } // Navigate to the random story's reading page router.push(`/stories/${randomStory.id}/read`); } catch (error) { console.error('Failed to get random story:', error); alert('Failed to get a random story. Please try again.'); } finally { setRandomLoading(false); } }; if (loading) { return (
); } return (
{/* Header */}

Your Story Library

{totalElements} {totalElements === 1 ? 'story' : 'stories'} {searchQuery || selectedTags.length > 0 ? ` found` : ` total`}

{/* Search and Filters */}
{/* Search Bar */}
{searchLoading && (
)}
{/* View Mode Toggle */}
{/* Sort and Tag Filters */}
{/* Sort Options */}
{/* Sort Direction Toggle */}
{/* Clear Filters */} {(searchQuery || selectedTags.length > 0) && ( )}
{/* Tag Filter */}
{/* Stories Display */} {stories.length === 0 && !loading ? (
{searchQuery || selectedTags.length > 0 ? 'No stories match your filters' : 'No stories in your library yet' }
{searchQuery || selectedTags.length > 0 ? ( ) : ( )}
) : ( )} {/* Pagination */} {totalPages > 1 && (
Page {page + 1} of {totalPages}
)}
); }