From 7fc56c9ee1fd5c91aca34fef3ea092e94ebe9449 Mon Sep 17 00:00:00 2001 From: igor_nadein Date: Fri, 27 Feb 2026 19:41:20 +0300 Subject: [PATCH] =?UTF-8?q?fix:=20=D0=B8=D1=81=D0=BF=D1=80=D0=B0=D0=B2?= =?UTF-8?q?=D0=BB=D0=B5=D0=BD=D0=B0=20=D0=BE=D1=88=D0=B8=D0=B1=D0=BA=D0=B0?= =?UTF-8?q?=20notifications.slice=20is=20not=20a=20function?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit - Добавлена проверка Array.isArray() в NotificationCenter - Добавлена защита от non-array в useNotifications хук - Исправлен импорт date-fns/locale для версии 4+ - В случае ошибки API устанавливается пустой массив [] --- frontend/src/components/NotificationCenter.tsx | 7 +++++-- frontend/src/hooks/useApi.ts | 7 +++++-- 2 files changed, 10 insertions(+), 4 deletions(-) diff --git a/frontend/src/components/NotificationCenter.tsx b/frontend/src/components/NotificationCenter.tsx index 635fd9d1..132f8c5e 100644 --- a/frontend/src/components/NotificationCenter.tsx +++ b/frontend/src/components/NotificationCenter.tsx @@ -5,15 +5,18 @@ import { Bell, Settings, X } from 'lucide-react'; import { useNotifications } from '@/hooks/useApi'; import { useWebPush } from '@/hooks/useWebPush'; import { formatDistanceToNow } from 'date-fns'; -import { ru } from 'date-fns/locale'; +import { ru } from 'date-fns/locale/ru'; export function NotificationCenter() { const [isOpen, setIsOpen] = useState(false); const [showSettings, setShowSettings] = useState(false); const dropdownRef = useRef(null); - const { notifications, unreadCount, markAsRead, markAllAsRead, loading } = useNotifications(); + const { notifications: notificationsData, unreadCount, markAsRead, markAllAsRead, loading } = useNotifications(); const { isSupported, isSubscribed, subscribe, unsubscribe, permission, isLoading: pushLoading } = useWebPush(); + + // Обеспечиваем что notifications всегда массив + const notifications = Array.isArray(notificationsData) ? notificationsData : []; // Закрытие при клике вне компонента useEffect(() => { diff --git a/frontend/src/hooks/useApi.ts b/frontend/src/hooks/useApi.ts index 07bf6c02..22bbaf9c 100644 --- a/frontend/src/hooks/useApi.ts +++ b/frontend/src/hooks/useApi.ts @@ -180,10 +180,13 @@ export function useNotifications() { try { const data = await apiClient.getNotifications(); const notifs = data.results || data; - setNotifications(notifs); - setUnreadCount(notifs.filter((n: any) => !n.is_read).length); + // Убеждаемся что notifs это массив + const notificationsArray = Array.isArray(notifs) ? notifs : []; + setNotifications(notificationsArray); + setUnreadCount(notificationsArray.filter((n: any) => !n.is_read).length); } catch (err) { setError(err as Error); + setNotifications([]); // В случае ошибки устанавливаем пустой массив } finally { setLoading(false); }