fix: исправлена ошибка notifications.slice is not a function
- Добавлена проверка Array.isArray() в NotificationCenter - Добавлена защита от non-array в useNotifications хук - Исправлен импорт date-fns/locale для версии 4+ - В случае ошибки API устанавливается пустой массив []
This commit is contained in:
parent
f7ac5102de
commit
7fc56c9ee1
@ -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(() => {
|
||||
|
||||
@ -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);
|
||||
}
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user