refactor: convert library scan to Server Action

- Add src/app/actions/library.ts with scanLibrary
- Update ScanButton to use Server Action
- Remove POST from api/komga/libraries/[libraryId]/scan route
This commit is contained in:
2026-02-28 10:53:41 +01:00
parent d56b0fd7ae
commit 6180f9abb1
4 changed files with 35 additions and 57 deletions

View File

@@ -0,0 +1,28 @@
"use server";
import { revalidatePath } from "next/cache";
import { LibraryService } from "@/lib/services/library.service";
import { ERROR_CODES } from "@/constants/errorCodes";
import { AppError } from "@/utils/errors";
/**
* Lance un scan de bibliothèque
*/
export async function scanLibrary(
libraryId: string
): Promise<{ success: boolean; message: string }> {
try {
await LibraryService.scanLibrary(libraryId, false);
// Invalider le cache de la bibliothèque
revalidatePath(`/libraries/${libraryId}`);
revalidatePath("/libraries");
return { success: true, message: "Scan lancé" };
} catch (error) {
if (error instanceof AppError) {
return { success: false, message: error.message };
}
return { success: false, message: "Erreur lors du scan" };
}
}

View File

@@ -1,45 +0,0 @@
import { NextResponse } from "next/server";
import { LibraryService } from "@/lib/services/library.service";
import { ERROR_CODES } from "@/constants/errorCodes";
import { AppError } from "@/utils/errors";
import { getErrorMessage } from "@/utils/errors";
import type { NextRequest } from "next/server";
import logger from "@/lib/logger";
export async function POST(
request: NextRequest,
{ params }: { params: Promise<{ libraryId: string }> }
) {
try {
const libraryId: string = (await params).libraryId;
// Scan library with deep=false
await LibraryService.scanLibrary(libraryId, false);
return NextResponse.json({ success: true });
} catch (error) {
logger.error({ err: error }, "API Library Scan - Erreur");
if (error instanceof AppError) {
return NextResponse.json(
{
error: {
code: error.code,
name: "Library scan error",
message: getErrorMessage(error.code),
},
},
{ status: 500 }
);
}
return NextResponse.json(
{
error: {
code: ERROR_CODES.LIBRARY.SCAN_ERROR,
name: "Library scan error",
message: getErrorMessage(ERROR_CODES.LIBRARY.SCAN_ERROR),
},
},
{ status: 500 }
);
}
}

View File

@@ -8,6 +8,7 @@ import { cn } from "@/lib/utils";
import { useTranslation } from "react-i18next";
import { useRouter } from "next/navigation";
import logger from "@/lib/logger";
import { scanLibrary } from "@/app/actions/library";
interface ScanButtonProps {
libraryId: string;
@@ -22,12 +23,10 @@ export function ScanButton({ libraryId }: ScanButtonProps) {
const handleScan = async () => {
setIsScanning(true);
try {
const response = await fetch(`/api/komga/libraries/${libraryId}/scan`, {
method: "POST",
});
const result = await scanLibrary(libraryId);
if (!response.ok) {
throw new Error("Failed to scan library");
if (!result.success) {
throw new Error(result.message);
}
toast({
@@ -35,14 +34,9 @@ export function ScanButton({ libraryId }: ScanButtonProps) {
description: t("library.scan.success.description"),
});
// Attendre 5 secondes pour que le scan se termine, puis invalider le cache et rafraîchir
// Attendre 5 secondes pour que le scan se termine, puis rafraîchir
setTimeout(async () => {
try {
// Invalider le cache
await fetch(`/api/komga/libraries/${libraryId}/series`, {
method: "DELETE",
});
// Rafraîchir la page pour voir les changements
router.refresh();
@@ -52,7 +46,7 @@ export function ScanButton({ libraryId }: ScanButtonProps) {
description: t("library.scan.complete.description"),
});
} catch (error) {
logger.error({ err: error }, "Error invalidating cache after scan:");
logger.error({ err: error }, "Error refreshing after scan:");
toast({
variant: "destructive",
title: t("library.scan.error.title"),