From a19c4bca6295e027ef081609961de486bdad0299 Mon Sep 17 00:00:00 2001 From: glpshchn <464976@niuitmo.ru> Date: Fri, 5 Dec 2025 00:39:03 +0300 Subject: [PATCH] Update files --- frontend/src/components/CommentsModal.jsx | 151 +++++++++++----------- 1 file changed, 78 insertions(+), 73 deletions(-) diff --git a/frontend/src/components/CommentsModal.jsx b/frontend/src/components/CommentsModal.jsx index af0b5c0..e94f2be 100644 --- a/frontend/src/components/CommentsModal.jsx +++ b/frontend/src/components/CommentsModal.jsx @@ -72,7 +72,7 @@ export default function CommentsModal({ post, onClose, onUpdate }) { return () => { cancelled = true } - }, [post?._id || null]) // Только ID поста в зависимостях, используем null для стабильности + }, [post?._id]) // Используем просто post?._id без || null // Проверка на существование поста ПОСЛЕ хуков const displayPost = fullPost || post @@ -104,10 +104,6 @@ export default function CommentsModal({ post, onClose, onUpdate }) { setComment('') hapticFeedback('success') - // Обновить данные поста для синхронизации (но не блокируем UI) - // Перезагружаем через useEffect, который сработает при изменении post._id - // Но так как post._id не меняется, просто обновим локально - if (onUpdate) { onUpdate() } @@ -124,6 +120,7 @@ export default function CommentsModal({ post, onClose, onUpdate }) { } const formatDate = (date) => { + if (!date) return 'только что' const d = new Date(date) const now = new Date() const diff = Math.floor((now - d) / 1000) // секунды @@ -142,17 +139,15 @@ export default function CommentsModal({ post, onClose, onUpdate }) { } } - // Если нет валидного поста, не рендерим модалку вообще - if (!hasValidPost) { - return null - } - + // ВСЕГДА рендерим createPortal, даже если пост не валиден + // Это критично для соблюдения правил хуков return createPortal(
e.stopPropagation()} onTouchStart={(e) => e.stopPropagation()} onClick={handleOverlayClick} + style={{ display: hasValidPost ? 'flex' : 'none' }} >
e.stopPropagation()}> {/* Хедер */} @@ -165,7 +160,13 @@ export default function CommentsModal({ post, onClose, onUpdate }) {
{/* Пост */} - {loadingPost ? ( + {!hasValidPost ? ( +
+
+

Загрузка...

+
+
+ ) : loadingPost ? (
@@ -205,71 +206,75 @@ export default function CommentsModal({ post, onClose, onUpdate }) { )} {/* Список комментариев */} -
- {comments.length === 0 ? ( -
-

Пока нет комментариев

- Будьте первым! -
- ) : ( - comments - .filter(c => { - // Фильтруем комментарии без автора или с неполным автором - return c && c.author && (typeof c.author === 'object') && c.content - }) - .map((c, index) => { - // Используем _id если есть, иначе index - const commentId = c._id || c.id || `comment-${index}` - // Проверяем, что автор полностью загружен - if (!c.author || typeof c.author !== 'object') { - console.warn('[CommentsModal] Комментарий без автора:', c) - return null - } - return ( -
- {c.author?.username { e.target.src = '/default-avatar.png' }} - /> -
-
- - {c.author?.firstName || ''} {c.author?.lastName || ''} - {!c.author?.firstName && !c.author?.lastName && 'Пользователь'} - - {formatDate(c.createdAt)} -
-

{decodeHtmlEntities(c.content)}

-
+ {hasValidPost && ( +
+ {comments.length === 0 ? ( +
+

Пока нет комментариев

+ Будьте первым!
- ) - }) - .filter(Boolean) // Убираем null значения - )} -
+ ) : ( + comments + .filter(c => { + // Фильтруем комментарии без автора или с неполным автором + return c && c.author && (typeof c.author === 'object') && c.content + }) + .map((c, index) => { + // Используем _id если есть, иначе index + const commentId = c._id || c.id || `comment-${index}` + // Проверяем, что автор полностью загружен + if (!c.author || typeof c.author !== 'object') { + console.warn('[CommentsModal] Комментарий без автора:', c) + return null + } + return ( +
+ {c.author?.username { e.target.src = '/default-avatar.png' }} + /> +
+
+ + {c.author?.firstName || ''} {c.author?.lastName || ''} + {!c.author?.firstName && !c.author?.lastName && 'Пользователь'} + + {formatDate(c.createdAt)} +
+

{decodeHtmlEntities(c.content)}

+
+
+ ) + }) + .filter(Boolean) // Убираем null значения + )} +
+ )} {/* Форма добавления комментария */} -
- setComment(e.target.value)} - onKeyPress={e => e.key === 'Enter' && handleSubmit()} - maxLength={500} - /> - -
+ {hasValidPost && ( +
+ setComment(e.target.value)} + onKeyPress={e => e.key === 'Enter' && handleSubmit()} + maxLength={500} + /> + +
+ )}
-
+
, + document.body ) } -