fix: sidebar refresh lib
This commit is contained in:
@@ -3,7 +3,7 @@ import Link from "next/link";
|
|||||||
import { usePathname, useRouter } from "next/navigation";
|
import { usePathname, useRouter } from "next/navigation";
|
||||||
import { cn } from "@/lib/utils";
|
import { cn } from "@/lib/utils";
|
||||||
import { authService } from "@/lib/services/auth.service";
|
import { authService } from "@/lib/services/auth.service";
|
||||||
import { useEffect, useState } from "react";
|
import { useEffect, useState, useCallback } from "react";
|
||||||
import { KomgaLibrary } from "@/types/komga";
|
import { KomgaLibrary } from "@/types/komga";
|
||||||
import { storageService } from "@/lib/services/storage.service";
|
import { storageService } from "@/lib/services/storage.service";
|
||||||
|
|
||||||
@@ -35,40 +35,30 @@ export function Sidebar({ isOpen }: SidebarProps) {
|
|||||||
initAuth();
|
initAuth();
|
||||||
}, []);
|
}, []);
|
||||||
|
|
||||||
// Effet séparé pour charger les bibliothèques
|
const fetchLibraries = useCallback(async () => {
|
||||||
const fetchLibraries = async () => {
|
if (!isAuthenticated) return;
|
||||||
if (!isAuthenticated) {
|
setIsLoading(true);
|
||||||
setIsLoading(false);
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
|
|
||||||
try {
|
|
||||||
console.log("Sidebar - Fetching libraries...");
|
console.log("Sidebar - Fetching libraries...");
|
||||||
|
try {
|
||||||
const response = await fetch("/api/komga/libraries");
|
const response = await fetch("/api/komga/libraries");
|
||||||
if (!response.ok) {
|
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();
|
const data = await response.json();
|
||||||
console.log("Sidebar - Libraries fetched:", data.length);
|
console.log("Sidebar - Libraries fetched:", data.length);
|
||||||
setLibraries(data);
|
setLibraries(data);
|
||||||
} catch (error) {
|
} catch (error) {
|
||||||
console.error("Sidebar - Error fetching libraries:", error);
|
console.error("Erreur:", error);
|
||||||
if (
|
setLibraries([]);
|
||||||
error instanceof Error &&
|
|
||||||
(error.message.includes("401") || error.message.includes("403"))
|
|
||||||
) {
|
|
||||||
console.log("Sidebar - Auth error, logging out");
|
|
||||||
handleLogout();
|
|
||||||
}
|
|
||||||
} finally {
|
} finally {
|
||||||
setIsLoading(false);
|
setIsLoading(false);
|
||||||
setIsRefreshing(false);
|
setIsRefreshing(false);
|
||||||
}
|
}
|
||||||
};
|
}, [isAuthenticated]);
|
||||||
|
|
||||||
useEffect(() => {
|
useEffect(() => {
|
||||||
fetchLibraries();
|
fetchLibraries();
|
||||||
}, [isAuthenticated, pathname]);
|
}, [isAuthenticated, pathname, fetchLibraries]);
|
||||||
|
|
||||||
const handleRefresh = async () => {
|
const handleRefresh = async () => {
|
||||||
setIsRefreshing(true);
|
setIsRefreshing(true);
|
||||||
|
|||||||
Reference in New Issue
Block a user