fix: исправлена ошибка notifications.slice is not a function

- Добавлена проверка Array.isArray() в NotificationCenter
- Добавлена защита от non-array в useNotifications хук
- Исправлен импорт date-fns/locale для версии 4+
- В случае ошибки API устанавливается пустой массив []
This commit is contained in:
igor_nadein 2026-02-27 19:41:20 +03:00
parent 98a5095b0e
commit acc511f4b8
2 changed files with 10 additions and 4 deletions

View File

@ -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<HTMLDivElement>(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(() => {

View File

@ -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);
}