From 39ee917ef5ab117c1fe724e4fc46f09744394045 Mon Sep 17 00:00:00 2001 From: Dylan De Faoite Date: Sun, 1 Feb 2026 16:19:43 +0000 Subject: [PATCH] fix: page not loading if dataset was empty --- frontend/src/pages/Stats.tsx | 34 ++++++++++++++++++++++++++-------- 1 file changed, 26 insertions(+), 8 deletions(-) diff --git a/frontend/src/pages/Stats.tsx b/frontend/src/pages/Stats.tsx index e1fe123..af1bed4 100644 --- a/frontend/src/pages/Stats.tsx +++ b/frontend/src/pages/Stats.tsx @@ -29,27 +29,41 @@ const StatPage = () => { const inputRef = useRef(null); const getStats = () => { + setError(""); + Promise.all([ axios.get("http://localhost:5000/stats/time"), axios.get("http://localhost:5000/stats/content"), ]) .then(([timeRes, wordsRes]) => { + const eventsPerDay = Array.isArray(timeRes.data?.events_per_day) + ? timeRes.data.events_per_day + : []; - setPostsPerDay(timeRes.data["events_per_day"].filter( - (d: any) => new Date(d.date) >= new Date('2026-01-10') - )) + const weekdayHourHeatmap = Array.isArray(timeRes.data?.weekday_hour_heatmap) + ? timeRes.data.weekday_hour_heatmap + : []; - setHeatmapData(timeRes.data["weekday_hour_heatmap"]) + const wordFrequencies = Array.isArray(wordsRes.data?.word_frequencies) + ? wordsRes.data.word_frequencies + : []; + + setPostsPerDay( + eventsPerDay.filter( + (d: any) => new Date(d.date) >= new Date("2026-01-10") + ) + ); + + setHeatmapData(weekdayHourHeatmap); setWordFrequencyData( - wordsRes.data["word_frequencies"].map((d: BackendWord) => ({ + wordFrequencies.map((d: BackendWord) => ({ text: d.word, value: d.count, })) ); }) - .catch((e) => setError("Failed to load statistics: " + e)) - .finally(() => setLoading(false)); + .catch((e) => setError("Failed to load statistics: " + String(e))); }; const onSearch = () => { @@ -75,7 +89,11 @@ const StatPage = () => { }) }; - useEffect(getStats, []) + useEffect(() => { + setLoading(true); + getStats(); + setLoading(false); + }, []) if (loading) return

Loading insights…

; if (error) return

{error}

;