"use client"; import { SeriesGrid } from "./SeriesGrid"; import { SeriesList } from "./SeriesList"; import { Pagination } from "@/components/ui/Pagination"; import { useRouter, usePathname, useSearchParams } from "next/navigation"; import { useState, useEffect, useCallback } from "react"; import type { KomgaSeries } from "@/types/komga"; import { SearchInput } from "./SearchInput"; import { useTranslate } from "@/hooks/useTranslate"; import { useDisplayPreferences } from "@/hooks/useDisplayPreferences"; import { usePreferences } from "@/contexts/PreferencesContext"; import { PageSizeSelect } from "@/components/common/PageSizeSelect"; import { CompactModeButton } from "@/components/common/CompactModeButton"; import { ViewModeButton } from "@/components/common/ViewModeButton"; import { UnreadFilterButton } from "@/components/common/UnreadFilterButton"; interface PaginatedSeriesGridProps { series: KomgaSeries[]; currentPage: number; totalPages: number; totalElements: number; defaultShowOnlyUnread: boolean; showOnlyUnread: boolean; pageSize?: number; } export function PaginatedSeriesGrid({ series, currentPage, totalPages, totalElements: _totalElements, defaultShowOnlyUnread, showOnlyUnread: initialShowOnlyUnread, pageSize, }: PaginatedSeriesGridProps) { const router = useRouter(); const pathname = usePathname(); const searchParams = useSearchParams(); const [showOnlyUnread, setShowOnlyUnread] = useState(initialShowOnlyUnread); const { isCompact, itemsPerPage: displayItemsPerPage, viewMode } = useDisplayPreferences(); const { updatePreferences } = usePreferences(); // Utiliser la taille de page effective (depuis l'URL ou les préférences) const effectivePageSize = pageSize || displayItemsPerPage; const { t } = useTranslate(); const updateUrlParams = useCallback( async (updates: Record, replace: boolean = false) => { const params = new URLSearchParams(searchParams.toString()); Object.entries(updates).forEach(([key, value]) => { if (value === null) { params.delete(key); } else { params.set(key, value); } }); if (replace) { await router.replace(`${pathname}?${params.toString()}`); } else { await router.push(`${pathname}?${params.toString()}`); } }, [router, pathname, searchParams] ); // Update local state when prop changes useEffect(() => { setShowOnlyUnread(initialShowOnlyUnread); }, [initialShowOnlyUnread]); // Apply default filter on initial load useEffect(() => { if (defaultShowOnlyUnread && !searchParams.has("unread")) { updateUrlParams({ page: "1", unread: "true" }, true); } }, [defaultShowOnlyUnread, pathname, router, searchParams, updateUrlParams]); const handlePageChange = async (page: number) => { await updateUrlParams({ page: page.toString() }); }; const handleUnreadFilter = async () => { const newUnreadState = !showOnlyUnread; setShowOnlyUnread(newUnreadState); await updateUrlParams({ page: "1", unread: newUnreadState ? "true" : "false", }); // Sauvegarder la préférence dans la base de données try { await updatePreferences({ showOnlyUnread: newUnreadState }); } catch (error) { // Log l'erreur mais ne bloque pas l'utilisateur console.error("Erreur lors de la sauvegarde de la préférence:", error); } }; const handlePageSizeChange = async (size: number) => { await updateUrlParams({ page: "1", size: size.toString(), }); }; // Calculate start and end indices for display const startIndex = (currentPage - 1) * effectivePageSize + 1; const endIndex = Math.min(currentPage * effectivePageSize, _totalElements); const getShowingText = () => { if (!_totalElements) return t("series.empty"); return t("books.display.showing", { start: startIndex, end: endIndex, total: _totalElements, }); }; return (

{getShowingText()}

{viewMode === "grid" ? ( ) : ( )}

{t("series.display.page", { current: currentPage, total: totalPages })}

); }