feat: refresh in libraries and books lists

This commit is contained in:
Julien Froidefond
2025-02-22 15:36:32 +01:00
parent b208a2aaf6
commit 448cdf6450
7 changed files with 135 additions and 8 deletions

View File

@@ -1,6 +1,8 @@
import { PaginatedSeriesGrid } from "@/components/library/PaginatedSeriesGrid"; import { PaginatedSeriesGrid } from "@/components/library/PaginatedSeriesGrid";
import { LibraryService } from "@/lib/services/library.service"; import { LibraryService } from "@/lib/services/library.service";
import { PreferencesService } from "@/lib/services/preferences.service"; import { PreferencesService } from "@/lib/services/preferences.service";
import { revalidatePath } from "next/cache";
import { RefreshButton } from "@/components/library/RefreshButton";
interface PageProps { interface PageProps {
params: { libraryId: string }; params: { libraryId: string };
@@ -9,6 +11,20 @@ interface PageProps {
const PAGE_SIZE = 20; const PAGE_SIZE = 20;
async function refreshLibrary(libraryId: string) {
"use server";
try {
await LibraryService.clearLibrarySeriesCache(libraryId);
revalidatePath(`/libraries/${libraryId}`);
return { success: true };
} catch (error) {
console.error("Erreur lors du rafraîchissement:", error);
return { success: false, error: "Erreur lors du rafraîchissement de la bibliothèque" };
}
}
async function getLibrarySeries(libraryId: string, page: number = 1, unreadOnly: boolean = false) { async function getLibrarySeries(libraryId: string, page: number = 1, unreadOnly: boolean = false) {
try { try {
const pageIndex = page - 1; const pageIndex = page - 1;
@@ -46,11 +62,14 @@ export default async function LibraryPage({ params, searchParams }: PageProps) {
<div className="container py-8 space-y-8"> <div className="container py-8 space-y-8">
<div className="flex items-center justify-between"> <div className="flex items-center justify-between">
<h1 className="text-3xl font-bold">{library.name}</h1> <h1 className="text-3xl font-bold">{library.name}</h1>
<div className="flex items-center gap-2">
{series.totalElements > 0 && ( {series.totalElements > 0 && (
<p className="text-sm text-muted-foreground"> <p className="text-sm text-muted-foreground">
{series.totalElements} série{series.totalElements > 1 ? "s" : ""} {series.totalElements} série{series.totalElements > 1 ? "s" : ""}
</p> </p>
)} )}
<RefreshButton libraryId={params.libraryId} refreshLibrary={refreshLibrary} />
</div>
</div> </div>
<PaginatedSeriesGrid <PaginatedSeriesGrid
series={series.content || []} series={series.content || []}
@@ -66,7 +85,10 @@ export default async function LibraryPage({ params, searchParams }: PageProps) {
} catch (error) { } catch (error) {
return ( return (
<div className="container py-8 space-y-8"> <div className="container py-8 space-y-8">
<div className="flex items-center justify-between">
<h1 className="text-3xl font-bold">Séries</h1> <h1 className="text-3xl font-bold">Séries</h1>
<RefreshButton libraryId={params.libraryId} refreshLibrary={refreshLibrary} />
</div>
<div className="rounded-md bg-destructive/15 p-4"> <div className="rounded-md bg-destructive/15 p-4">
<p className="text-sm text-destructive"> <p className="text-sm text-destructive">
{error instanceof Error ? error.message : "Erreur lors de la récupération des séries"} {error instanceof Error ? error.message : "Erreur lors de la récupération des séries"}

View File

@@ -2,6 +2,8 @@ import { PaginatedBookGrid } from "@/components/series/PaginatedBookGrid";
import { SeriesHeader } from "@/components/series/SeriesHeader"; import { SeriesHeader } from "@/components/series/SeriesHeader";
import { SeriesService } from "@/lib/services/series.service"; import { SeriesService } from "@/lib/services/series.service";
import { PreferencesService } from "@/lib/services/preferences.service"; import { PreferencesService } from "@/lib/services/preferences.service";
import { revalidatePath } from "next/cache";
import { RefreshButton } from "@/components/library/RefreshButton";
interface PageProps { interface PageProps {
params: { seriesId: string }; params: { seriesId: string };
@@ -23,6 +25,20 @@ async function getSeriesBooks(seriesId: string, page: number = 1, unreadOnly: bo
} }
} }
async function refreshSeries(seriesId: string) {
"use server";
try {
await SeriesService.clearSeriesBooksCache(seriesId);
await SeriesService.clearSeriesCache(seriesId);
revalidatePath(`/series/${seriesId}`);
return { success: true };
} catch (error) {
console.error("Erreur lors du rafraîchissement:", error);
return { success: false, error: "Erreur lors du rafraîchissement de la série" };
}
}
export default async function SeriesPage({ params, searchParams }: PageProps) { export default async function SeriesPage({ params, searchParams }: PageProps) {
const currentPage = searchParams.page ? parseInt(searchParams.page) : 1; const currentPage = searchParams.page ? parseInt(searchParams.page) : 1;
const preferences = await PreferencesService.getPreferences(); const preferences = await PreferencesService.getPreferences();
@@ -36,7 +52,7 @@ export default async function SeriesPage({ params, searchParams }: PageProps) {
return ( return (
<div className="container py-8 space-y-8"> <div className="container py-8 space-y-8">
<SeriesHeader series={series} /> <SeriesHeader series={series} refreshSeries={refreshSeries} />
<PaginatedBookGrid <PaginatedBookGrid
books={books.content || []} books={books.content || []}
currentPage={currentPage} currentPage={currentPage}

View File

@@ -0,0 +1,54 @@
"use client";
import { useState } from "react";
import { RefreshCw } from "lucide-react";
import { Button } from "@/components/ui/button";
import { useToast } from "@/components/ui/use-toast";
import { cn } from "@/lib/utils";
interface RefreshButtonProps {
libraryId: string;
refreshLibrary: (libraryId: string) => Promise<{ success: boolean; error?: string }>;
}
export function RefreshButton({ libraryId, refreshLibrary }: RefreshButtonProps) {
const [isRefreshing, setIsRefreshing] = useState(false);
const { toast } = useToast();
const handleRefresh = async () => {
setIsRefreshing(true);
try {
const result = await refreshLibrary(libraryId);
if (result.success) {
toast({
title: "Bibliothèque rafraîchie",
description: "La bibliothèque a été rafraîchie avec succès",
});
} else {
throw new Error(result.error);
}
} catch (error) {
toast({
variant: "destructive",
title: "Erreur",
description: error instanceof Error ? error.message : "Une erreur est survenue",
});
} finally {
setIsRefreshing(false);
}
};
return (
<Button
variant="ghost"
size="icon"
onClick={handleRefresh}
disabled={isRefreshing}
className="ml-2"
aria-label="Rafraîchir la bibliothèque"
>
<RefreshCw className={cn("h-4 w-4", isRefreshing && "animate-spin")} />
</Button>
);
}

View File

@@ -6,12 +6,14 @@ import { useState, useEffect } from "react";
import { Button } from "../ui/button"; import { Button } from "../ui/button";
import { useToast } from "@/components/ui/use-toast"; import { useToast } from "@/components/ui/use-toast";
import { Cover } from "@/components/ui/cover"; import { Cover } from "@/components/ui/cover";
import { RefreshButton } from "@/components/library/RefreshButton";
interface SeriesHeaderProps { interface SeriesHeaderProps {
series: KomgaSeries; series: KomgaSeries;
refreshSeries: (seriesId: string) => Promise<{ success: boolean; error?: string }>;
} }
export const SeriesHeader = ({ series }: SeriesHeaderProps) => { export const SeriesHeader = ({ series, refreshSeries }: SeriesHeaderProps) => {
const { toast } = useToast(); const { toast } = useToast();
const [isFavorite, setIsFavorite] = useState(false); const [isFavorite, setIsFavorite] = useState(false);
@@ -148,6 +150,7 @@ export const SeriesHeader = ({ series }: SeriesHeaderProps) => {
<StarOff className="w-5 h-5" /> <StarOff className="w-5 h-5" />
)} )}
</Button> </Button>
<RefreshButton libraryId={series.id} refreshLibrary={refreshSeries} />
</div> </div>
</div> </div>
</div> </div>

View File

@@ -1,6 +1,7 @@
import { BaseApiService } from "./base-api.service"; import { BaseApiService } from "./base-api.service";
import { Library, LibraryResponse } from "@/types/library"; import { Library, LibraryResponse } from "@/types/library";
import { Series } from "@/types/series"; import { Series } from "@/types/series";
import { serverCacheService } from "./server-cache.service";
export class LibraryService extends BaseApiService { export class LibraryService extends BaseApiService {
static async getLibraries(): Promise<Library[]> { static async getLibraries(): Promise<Library[]> {
@@ -53,4 +54,8 @@ export class LibraryService extends BaseApiService {
return this.handleError(error, "Impossible de récupérer les séries"); return this.handleError(error, "Impossible de récupérer les séries");
} }
} }
static async clearLibrarySeriesCache(libraryId: string) {
serverCacheService.delete(`library-${libraryId}-series`);
}
} }

View File

@@ -4,6 +4,7 @@ import { KomgaBook, KomgaSeries } from "@/types/komga";
import { BookService } from "./book.service"; import { BookService } from "./book.service";
import { ImageService } from "./image.service"; import { ImageService } from "./image.service";
import { PreferencesService } from "./preferences.service"; import { PreferencesService } from "./preferences.service";
import { serverCacheService } from "./server-cache.service";
export class SeriesService extends BaseApiService { export class SeriesService extends BaseApiService {
static async getSeries(seriesId: string): Promise<KomgaSeries> { static async getSeries(seriesId: string): Promise<KomgaSeries> {
@@ -22,6 +23,10 @@ export class SeriesService extends BaseApiService {
} }
} }
static async clearSeriesCache(seriesId: string) {
serverCacheService.delete(`series-${seriesId}`);
}
static async getSeriesBooks( static async getSeriesBooks(
seriesId: string, seriesId: string,
page: number = 0, page: number = 0,
@@ -48,6 +53,10 @@ export class SeriesService extends BaseApiService {
} }
} }
static async clearSeriesBooksCache(seriesId: string) {
serverCacheService.deleteAll(`series-${seriesId}-books`);
}
static async getFirstBook(seriesId: string): Promise<string> { static async getFirstBook(seriesId: string): Promise<string> {
try { try {
const config = await this.getKomgaConfig(); const config = await this.getKomgaConfig();

View File

@@ -283,6 +283,24 @@ class ServerCacheService {
} }
} }
/**
* Supprime toutes les entrées du cache qui commencent par un préfixe
*/
deleteAll(prefix: string): void {
if (this.config.mode === "memory") {
this.memoryCache.forEach((value, key) => {
if (key.startsWith(prefix)) {
this.memoryCache.delete(key);
}
});
} else {
const cacheDir = path.join(this.cacheDir, prefix);
if (fs.existsSync(cacheDir)) {
fs.rmdirSync(cacheDir, { recursive: true });
}
}
}
/** /**
* Vide le cache * Vide le cache
*/ */