From 13492cea8463e13212d91e8238f6c98c046e6dec Mon Sep 17 00:00:00 2001 From: Julien Froidefond Date: Fri, 28 Feb 2025 17:44:22 +0100 Subject: [PATCH] feat: Clearing cache automatically after quitting e read session --- .../clear/[libraryId]/[seriesId]/route.ts | 39 +++++++++++++++++++ src/components/layout/Header.tsx | 2 +- src/components/reader/ClientBookReader.tsx | 1 + src/components/reader/ClientBookWrapper.tsx | 3 ++ src/types/komga.ts | 1 + 5 files changed, 45 insertions(+), 1 deletion(-) create mode 100644 src/app/api/komga/cache/clear/[libraryId]/[seriesId]/route.ts diff --git a/src/app/api/komga/cache/clear/[libraryId]/[seriesId]/route.ts b/src/app/api/komga/cache/clear/[libraryId]/[seriesId]/route.ts new file mode 100644 index 0000000..34e8d06 --- /dev/null +++ b/src/app/api/komga/cache/clear/[libraryId]/[seriesId]/route.ts @@ -0,0 +1,39 @@ +import { NextResponse } from "next/server"; +import { ERROR_CODES } from "@/constants/errorCodes"; +import { getErrorMessage } from "@/utils/errors"; +import { LibraryService } from "@/lib/services/library.service"; +import { HomeService } from "@/lib/services/home.service"; +import { SeriesService } from "@/lib/services/series.service"; + +export async function POST( + request: Request, + { params }: { params: { libraryId: string; seriesId: string } } +) { + try { + const { libraryId, seriesId } = params; + + await HomeService.invalidateHomeCache(); + + if (libraryId) { + await LibraryService.invalidateLibrarySeriesCache(libraryId); + } + if (seriesId) { + await SeriesService.invalidateSeriesBooksCache(seriesId); + await SeriesService.invalidateSeriesCache(seriesId); + } + + return NextResponse.json({ message: "🧹 Cache vidé avec succès" }); + } catch (error) { + console.error("Erreur lors de la suppression du cache:", error); + return NextResponse.json( + { + error: { + code: ERROR_CODES.CACHE.CLEAR_ERROR, + name: "Cache clear error", + message: getErrorMessage(ERROR_CODES.CACHE.CLEAR_ERROR), + }, + }, + { status: 500 } + ); + } +} diff --git a/src/components/layout/Header.tsx b/src/components/layout/Header.tsx index 53e55fd..cbdcc71 100644 --- a/src/components/layout/Header.tsx +++ b/src/components/layout/Header.tsx @@ -41,7 +41,7 @@ export function Header({ onToggleSidebar }: HeaderProps) { className="px-2 py-1.5 hover:bg-accent hover:text-accent-foreground rounded-md" aria-label="Toggle theme" > -
+
diff --git a/src/components/reader/ClientBookReader.tsx b/src/components/reader/ClientBookReader.tsx index 9648662..53af965 100644 --- a/src/components/reader/ClientBookReader.tsx +++ b/src/components/reader/ClientBookReader.tsx @@ -21,6 +21,7 @@ export function ClientBookReader({ book, pages }: ClientBookReaderProps) { const handleCloseReader = () => { setIsReading(false); + //Fetch une nouvelle route pour rafraichir les différents caches router.back(); }; diff --git a/src/components/reader/ClientBookWrapper.tsx b/src/components/reader/ClientBookWrapper.tsx index bc6eec8..14bffe3 100644 --- a/src/components/reader/ClientBookWrapper.tsx +++ b/src/components/reader/ClientBookWrapper.tsx @@ -13,6 +13,9 @@ export function ClientBookWrapper({ book, pages }: ClientBookWrapperProps) { const router = useRouter(); const handleCloseReader = () => { + fetch(`/api/komga/cache/clear/${book.libraryId}/${book.seriesId}`, { + method: "POST", + }); router.back(); }; diff --git a/src/types/komga.ts b/src/types/komga.ts index 2897f97..f64fd26 100644 --- a/src/types/komga.ts +++ b/src/types/komga.ts @@ -109,6 +109,7 @@ export interface KomgaBook { id: string; seriesId: string; seriesTitle: string; + libraryId: string; name: string; url: string; number: number;