From 62ac90746ef04c07aaa6fb481f74425fe4dd5b99 Mon Sep 17 00:00:00 2001 From: Alex Date: Wed, 15 Oct 2025 17:33:13 +0100 Subject: [PATCH] fix: improve error handling and loading state in fetchChunks function --- frontend/src/components/Chunks.tsx | 55 +++++++++++++++--------------- 1 file changed, 27 insertions(+), 28 deletions(-) diff --git a/frontend/src/components/Chunks.tsx b/frontend/src/components/Chunks.tsx index f58446d8..bdb1c4c5 100644 --- a/frontend/src/components/Chunks.tsx +++ b/frontend/src/components/Chunks.tsx @@ -137,36 +137,35 @@ const Chunks: React.FC = ({ const pathParts = path ? path.split('/') : []; const fetchChunks = async () => { - setLoading(true); - try { - const response = await userService.getDocumentChunks( - documentId, - page, - perPage, - token, - path, - searchTerm - ); + setLoading(true); + try { + const response = await userService.getDocumentChunks( + documentId, + page, + perPage, + token, + path, + searchTerm, + ); - if (!response.ok) { - throw new Error('Failed to fetch chunks data'); + if (!response.ok) { + throw new Error('Failed to fetch chunks data'); + } + + const data = await response.json(); + + setPage(data.page); + setPerPage(data.per_page); + setTotalChunks(data.total); + setPaginatedChunks(data.chunks); + } catch (error) { + setPaginatedChunks([]); + console.error(error); + } finally { + // ✅ always runs, success or failure + setLoading(false); } - - const data = await response.json(); - - setPage(data.page); - setPerPage(data.per_page); - setTotalChunks(data.total); - setPaginatedChunks(data.chunks); - } catch (error) { - setPaginatedChunks([]); - console.error(error); - } finally { - // ✅ always runs, success or failure - setLoading(false); - } -}; - + }; const handleAddChunk = (title: string, text: string) => { if (!text.trim()) {