From b9d7ebc2ae2b03bb98c18b825f729ba4de5c3d07 Mon Sep 17 00:00:00 2001 From: Julien Froidefond Date: Thu, 13 Feb 2025 12:36:47 +0100 Subject: [PATCH] fix: sidebar refresh lib --- src/components/layout/Sidebar.tsx | 30 ++++++++++-------------------- 1 file changed, 10 insertions(+), 20 deletions(-) diff --git a/src/components/layout/Sidebar.tsx b/src/components/layout/Sidebar.tsx index 45e6b78..9caec95 100644 --- a/src/components/layout/Sidebar.tsx +++ b/src/components/layout/Sidebar.tsx @@ -3,7 +3,7 @@ import Link from "next/link"; import { usePathname, useRouter } from "next/navigation"; import { cn } from "@/lib/utils"; import { authService } from "@/lib/services/auth.service"; -import { useEffect, useState } from "react"; +import { useEffect, useState, useCallback } from "react"; import { KomgaLibrary } from "@/types/komga"; import { storageService } from "@/lib/services/storage.service"; @@ -35,40 +35,30 @@ export function Sidebar({ isOpen }: SidebarProps) { initAuth(); }, []); - // Effet séparé pour charger les bibliothèques - const fetchLibraries = async () => { - if (!isAuthenticated) { - setIsLoading(false); - return; - } - + const fetchLibraries = useCallback(async () => { + if (!isAuthenticated) return; + setIsLoading(true); + console.log("Sidebar - Fetching libraries..."); try { - console.log("Sidebar - Fetching libraries..."); const response = await fetch("/api/komga/libraries"); if (!response.ok) { - throw new Error(`Erreur ${response.status} lors de la récupération des bibliothèques`); + throw new Error("Erreur lors de la récupération des bibliothèques"); } const data = await response.json(); console.log("Sidebar - Libraries fetched:", data.length); setLibraries(data); } catch (error) { - console.error("Sidebar - Error fetching libraries:", error); - if ( - error instanceof Error && - (error.message.includes("401") || error.message.includes("403")) - ) { - console.log("Sidebar - Auth error, logging out"); - handleLogout(); - } + console.error("Erreur:", error); + setLibraries([]); } finally { setIsLoading(false); setIsRefreshing(false); } - }; + }, [isAuthenticated]); useEffect(() => { fetchLibraries(); - }, [isAuthenticated, pathname]); + }, [isAuthenticated, pathname, fetchLibraries]); const handleRefresh = async () => { setIsRefreshing(true);