diff --git a/frontend/src/App.tsx b/frontend/src/App.tsx index 2430a6b..e5c2536 100644 --- a/frontend/src/App.tsx +++ b/frontend/src/App.tsx @@ -6354,13 +6354,54 @@ function NegotiationPage({ ); } +// Nieprzeczytane wiadomosci (rozmowy o ogloszeniach + rozmowa z administracja) dla kazdego +// zalogowanego konta - admin tez moze sprzedawac. Wspolne dla menu konta i paska gornego; +// odswiezane co 25 s oraz po zmianie strony, np. po otwarciu rozmowy. +function useMessagesUnreadCount(): number { + const { user } = useAuth(); + const { pathname } = useLocation(); + const [count, setCount] = useState(0); + + useEffect(() => { + if (!user) { + setCount(0); + return; + } + + let cancelled = false; + const loadUnread = () => { + apiFetch<{ count: number }>('/messages/unread-count') + .then((data) => { + if (!cancelled) { + setCount(data.count); + } + }) + .catch(() => { + if (!cancelled) { + setCount(0); + } + }); + }; + + loadUnread(); + const intervalId = window.setInterval(loadUnread, 25000); + + return () => { + cancelled = true; + window.clearInterval(intervalId); + }; + }, [user, pathname]); + + return count; +} + function AccountSidebar() { const { user } = useAuth(); const location = useLocation(); // Ustawienia obejmuja takze podglad profilu publicznego - podswietlamy je wspolna pozycja menu. const isSettingsSection = location.pathname === ROUTES.accountSettings || location.pathname === ROUTES.accountPublicProfile; - const [messagesUnreadCount, setMessagesUnreadCount] = useState(0); + const messagesUnreadCount = useMessagesUnreadCount(); // Liczba ogloszen konta pochodzi z serwera (/listings/mine) - tak samo jak lista na /konto/moje-oferty. const [allListingsCount, setAllListingsCount] = useState(0); // Spotkania zaplanowane na dzisiaj (odwolane sie nie licza). @@ -6394,37 +6435,6 @@ function AccountSidebar() { + readUserJsonStorage(user, LISTING_PRICE_ALERTS_STORAGE_SUFFIX, []).length : 0; - // Admin tez moze sprzedawac, wiec licznik rozmow o ogloszeniach dotyczy kazdego konta. - useEffect(() => { - if (!user) { - setMessagesUnreadCount(0); - return; - } - - let cancelled = false; - const loadUnread = () => { - apiFetch<{ count: number }>('/messages/unread-count') - .then((data) => { - if (!cancelled) { - setMessagesUnreadCount(data.count); - } - }) - .catch(() => { - if (!cancelled) { - setMessagesUnreadCount(0); - } - }); - }; - - loadUnread(); - const intervalId = window.setInterval(loadUnread, 25000); - - return () => { - cancelled = true; - window.clearInterval(intervalId); - }; - }, [user]); - useEffect(() => { if (!user) { setAllListingsCount(0); @@ -23368,6 +23378,7 @@ function Header({ const { notifications: serverNotifications, unreadCount } = useNotifications(); const isLoggedIn = Boolean(user); const isAdmin = user?.role === 'ADMIN'; + const messagesUnreadCount = useMessagesUnreadCount(); const [isNotificationsOpen, setIsNotificationsOpen] = useState(false); const notificationPopoverRef = useRef(null); const [isAccountMenuOpen, setIsAccountMenuOpen] = useState(false); @@ -23482,6 +23493,17 @@ function Header({ Ulubione {hasUnreadFavorites &&