diff --git a/src/components/Layout.tsx b/src/components/Layout.tsx index e47adcd..0d82dfc 100644 --- a/src/components/Layout.tsx +++ b/src/components/Layout.tsx @@ -1,4 +1,4 @@ -import { useState, useEffect } from 'react'; +import { useCallback, useState, useEffect } from 'react'; import { Outlet, Link, useLocation } from 'react-router-dom'; import { LayoutDashboard, Users, BarChart3, ChevronLeft, ChevronRight, Package, Loader2, LogOut, Megaphone } from 'lucide-react'; import type { DateRange, OrderData, StockData } from '../types'; @@ -32,19 +32,22 @@ const Layout = () => { return saved ? Number(saved) : 0; }); - const loadData = async (showLoading = false) => { + const loadData = useCallback(async (showLoading = false) => { if (showLoading) setIsLoading(true); - const [data, stock] = await Promise.all([fetchData(), fetchStock()]); - setOrdersData(data); - setStockData(stock); - if (showLoading) setIsLoading(false); - }; + try { + const [data, stock] = await Promise.all([fetchData(), fetchStock()]); + setOrdersData(data); + setStockData(stock); + } finally { + if (showLoading) setIsLoading(false); + } + }, []); useEffect(() => { - // The dashboard has to fetch its initial server state after mount. + // The dashboard fetches its initial server state after mount without blocking route render. // eslint-disable-next-line react-hooks/set-state-in-effect - void loadData(true); - }, []); + void loadData(false).finally(() => setIsLoading(false)); + }, [loadData]); useEffect(() => { if (refreshInterval === 0) return; @@ -54,7 +57,7 @@ const Layout = () => { }, refreshInterval); return () => clearInterval(intervalId); - }, [refreshInterval]); + }, [loadData, refreshInterval]); useEffect(() => { localStorage.setItem('nexstar_refresh_interval', refreshInterval.toString()); @@ -146,13 +149,13 @@ const Layout = () => { {/* Content Area */}
Carregando cliente...
+Carregando produto...
+