refacto: review keys of config komga and code

This commit is contained in:
Julien Froidefond
2025-02-13 10:51:08 +01:00
parent addc42d1f6
commit 74cb8126bb
8 changed files with 192 additions and 76 deletions

View File

@@ -1,5 +1,6 @@
import { cookies } from "next/headers";
import { PaginatedSeriesGrid } from "@/components/library/PaginatedSeriesGrid";
import { komgaConfigService } from "@/lib/services/komga-config.service";
interface PageProps {
params: { libraryId: string };
@@ -9,38 +10,24 @@ interface PageProps {
const PAGE_SIZE = 20;
async function getLibrarySeries(libraryId: string, page: number = 1, unreadOnly: boolean = false) {
const configCookie = cookies().get("komgaCredentials");
if (!configCookie) {
throw new Error("Configuration Komga manquante");
}
try {
const config = JSON.parse(atob(configCookie.value));
if (!config.serverUrl || !config.credentials?.username || !config.credentials?.password) {
throw new Error("Configuration Komga invalide ou incomplète");
}
const cookiesStore = cookies();
const config = komgaConfigService.validateAndGetConfig(cookiesStore);
// Paramètres de pagination
const pageIndex = page - 1; // L'API Komga utilise un index base 0
// Construire l'URL avec les paramètres
let url = `${config.serverUrl}/api/v1/series?library_id=${libraryId}&page=${pageIndex}&size=${PAGE_SIZE}`;
// Ajouter le filtre pour les séries non lues et en cours si nécessaire
let path = `series?library_id=${libraryId}&page=${pageIndex}&size=${PAGE_SIZE}`;
if (unreadOnly) {
url += "&read_status=UNREAD&read_status=IN_PROGRESS";
path += "&read_status=UNREAD&read_status=IN_PROGRESS";
}
const credentials = `${config.credentials.username}:${config.credentials.password}`;
const auth = Buffer.from(credentials).toString("base64");
const url = komgaConfigService.buildApiUrl(path, cookiesStore);
const headers = komgaConfigService.getAuthHeaders(cookiesStore);
const response = await fetch(url, {
headers: {
Authorization: `Basic ${auth}`,
Accept: "application/json",
},
headers,
next: { revalidate: 300 }, // Cache de 5 minutes
});

View File

@@ -2,6 +2,7 @@ import { cookies } from "next/headers";
import { PaginatedBookGrid } from "@/components/series/PaginatedBookGrid";
import { SeriesHeader } from "@/components/series/SeriesHeader";
import { KomgaSeries, KomgaBook } from "@/types/komga";
import { komgaConfigService } from "@/lib/services/komga-config.service";
interface PageProps {
params: { seriesId: string };
@@ -14,19 +15,9 @@ export default async function SeriesPage({ params, searchParams }: PageProps) {
const currentPage = searchParams.page ? parseInt(searchParams.page) : 1;
const unreadOnly = searchParams.unread === "true";
const configCookie = cookies().get("komgaCredentials");
if (!configCookie) {
throw new Error("Configuration Komga manquante");
}
try {
const config = JSON.parse(atob(configCookie.value));
if (!config.serverUrl || !config.credentials?.username || !config.credentials?.password) {
throw new Error("Configuration Komga invalide ou incomplète");
}
const credentials = `${config.credentials.username}:${config.credentials.password}`;
const auth = Buffer.from(credentials).toString("base64");
const cookiesStore = cookies();
const config = komgaConfigService.validateAndGetConfig(cookiesStore);
// Paramètres de pagination
const pageIndex = currentPage - 1; // L'API Komga utilise un index base 0
@@ -34,25 +25,22 @@ export default async function SeriesPage({ params, searchParams }: PageProps) {
// Appels API parallèles pour les détails de la série et les tomes
const [seriesResponse, booksResponse] = await Promise.all([
// Détails de la série
fetch(`${config.serverUrl}/api/v1/series/${params.seriesId}`, {
headers: {
Authorization: `Basic ${auth}`,
Accept: "application/json",
},
fetch(komgaConfigService.buildApiUrl(`series/${params.seriesId}`, cookiesStore), {
headers: komgaConfigService.getAuthHeaders(cookiesStore),
next: { revalidate: 300 },
}),
// Liste des tomes avec pagination et filtre
fetch(
`${config.serverUrl}/api/v1/series/${
params.seriesId
}/books?page=${pageIndex}&size=${PAGE_SIZE}&sort=metadata.numberSort,asc${
unreadOnly ? "&read_status=UNREAD&read_status=IN_PROGRESS" : ""
}`,
komgaConfigService.buildApiUrl(
`series/${
params.seriesId
}/books?page=${pageIndex}&size=${PAGE_SIZE}&sort=metadata.numberSort,asc${
unreadOnly ? "&read_status=UNREAD&read_status=IN_PROGRESS" : ""
}`,
cookiesStore
),
{
headers: {
Authorization: `Basic ${auth}`,
Accept: "application/json",
},
headers: komgaConfigService.getAuthHeaders(cookiesStore),
next: { revalidate: 300 },
}
),

View File

@@ -6,6 +6,7 @@ import { useRouter } from "next/navigation";
import { storageService } from "@/lib/services/storage.service";
import { AuthError } from "@/types/auth";
import { useToast } from "@/components/ui/use-toast";
import { komgaConfigService } from "@/lib/services/komga-config.service";
interface ErrorMessage {
message: string;
@@ -136,15 +137,21 @@ export default function SettingsPage() {
password,
};
storageService.setKomgaConfig(
{
serverUrl: newConfig.serverUrl,
credentials: { username: newConfig.username, password: newConfig.password },
const komgaConfig = {
serverUrl: newConfig.serverUrl,
credentials: {
username: newConfig.username,
password: newConfig.password,
},
true
);
};
komgaConfigService.setConfig(komgaConfig, true);
setConfig(newConfig);
// Émettre un événement pour notifier les autres composants
const configChangeEvent = new CustomEvent("komga-config-changed", { detail: komgaConfig });
window.dispatchEvent(configChangeEvent);
toast({
title: "Configuration sauvegardée",
description: "La configuration a été sauvegardée avec succès",