Update files
This commit is contained in:
parent
602b23017a
commit
f6baf7ed3e
|
|
@ -14,37 +14,57 @@ export default function CommentsModal({ post, onClose, onUpdate }) {
|
||||||
const [loadingPost, setLoadingPost] = useState(false)
|
const [loadingPost, setLoadingPost] = useState(false)
|
||||||
|
|
||||||
// Загрузить полные данные поста с комментариями
|
// Загрузить полные данные поста с комментариями
|
||||||
const loadFullPost = useCallback(async () => {
|
const loadFullPost = useCallback(async (postId) => {
|
||||||
if (!post?._id) {
|
if (!postId) {
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
try {
|
try {
|
||||||
setLoadingPost(true)
|
setLoadingPost(true)
|
||||||
const response = await getPosts()
|
// Загрузить посты с фильтром по автору поста для оптимизации
|
||||||
const foundPost = response.posts?.find(p => p._id === post._id)
|
// Если это не помогает, загружаем больше постов
|
||||||
|
const authorId = post?.author?._id || post?.author
|
||||||
|
const response = authorId
|
||||||
|
? await getPosts({ userId: authorId, limit: 100 })
|
||||||
|
: await getPosts({ limit: 200 })
|
||||||
|
|
||||||
|
const foundPost = response.posts?.find(p => p._id === postId)
|
||||||
if (foundPost) {
|
if (foundPost) {
|
||||||
|
// Проверяем, что комментарии populate'ены с авторами
|
||||||
|
const commentsWithAuthors = (foundPost.comments || []).filter(c => {
|
||||||
|
return c && c.author && (typeof c.author === 'object')
|
||||||
|
})
|
||||||
|
setComments(commentsWithAuthors)
|
||||||
setFullPost(foundPost)
|
setFullPost(foundPost)
|
||||||
// Убедимся, что комментарии загружены с авторами
|
} else {
|
||||||
const commentsWithAuthors = foundPost.comments || []
|
// Если не нашли, используем переданные данные
|
||||||
|
const commentsWithAuthors = (post.comments || []).filter(c => {
|
||||||
|
return c && c.author && (typeof c.author === 'object')
|
||||||
|
})
|
||||||
setComments(commentsWithAuthors)
|
setComments(commentsWithAuthors)
|
||||||
}
|
}
|
||||||
} catch (error) {
|
} catch (error) {
|
||||||
console.error('[CommentsModal] Ошибка загрузки поста:', error)
|
console.error('[CommentsModal] Ошибка загрузки поста:', error)
|
||||||
|
// Fallback на переданные данные
|
||||||
|
const commentsWithAuthors = (post.comments || []).filter(c => {
|
||||||
|
return c && c.author && (typeof c.author === 'object')
|
||||||
|
})
|
||||||
|
setComments(commentsWithAuthors)
|
||||||
} finally {
|
} finally {
|
||||||
setLoadingPost(false)
|
setLoadingPost(false)
|
||||||
}
|
}
|
||||||
}, [post?._id])
|
}, [post?.author])
|
||||||
|
|
||||||
useEffect(() => {
|
useEffect(() => {
|
||||||
if (post) {
|
if (post && post._id) {
|
||||||
// Сначала установим переданные данные
|
// Сначала установим переданные данные
|
||||||
setFullPost(post)
|
setFullPost(post)
|
||||||
setComments(post.comments || [])
|
const initialComments = (post.comments || []).filter(c => {
|
||||||
// Затем загрузим полные данные
|
return c && c.author && (typeof c.author === 'object')
|
||||||
if (post._id) {
|
})
|
||||||
loadFullPost()
|
setComments(initialComments)
|
||||||
}
|
// Затем загрузим полные данные для обновления
|
||||||
|
loadFullPost(post._id)
|
||||||
}
|
}
|
||||||
}, [post?._id, loadFullPost])
|
}, [post?._id, loadFullPost])
|
||||||
|
|
||||||
|
|
@ -63,24 +83,47 @@ export default function CommentsModal({ post, onClose, onUpdate }) {
|
||||||
hapticFeedback('light')
|
hapticFeedback('light')
|
||||||
|
|
||||||
const result = await commentPost(post._id, comment)
|
const result = await commentPost(post._id, comment)
|
||||||
if (result && result.comments) {
|
console.log('[CommentsModal] Результат добавления комментария:', result)
|
||||||
// Обновить комментарии из ответа API
|
|
||||||
setComments(result.comments)
|
if (result && result.comments && Array.isArray(result.comments)) {
|
||||||
|
// Фильтруем комментарии с авторами (проверяем, что author - объект)
|
||||||
|
const commentsWithAuthors = result.comments.filter(c => {
|
||||||
|
return c && c.author && (typeof c.author === 'object')
|
||||||
|
})
|
||||||
|
|
||||||
|
console.log('[CommentsModal] Отфильтрованные комментарии:', commentsWithAuthors.length, 'из', result.comments.length)
|
||||||
|
|
||||||
|
setComments(commentsWithAuthors)
|
||||||
// Обновить полный пост
|
// Обновить полный пост
|
||||||
if (fullPost) {
|
if (fullPost) {
|
||||||
setFullPost({ ...fullPost, comments: result.comments })
|
setFullPost({ ...fullPost, comments: commentsWithAuthors })
|
||||||
}
|
}
|
||||||
}
|
setComment('')
|
||||||
setComment('')
|
hapticFeedback('success')
|
||||||
hapticFeedback('success')
|
|
||||||
// Обновить данные поста
|
// Обновить данные поста для синхронизации (но не блокируем UI)
|
||||||
await loadFullPost()
|
loadFullPost(post._id).catch(err => {
|
||||||
if (onUpdate) {
|
console.error('[CommentsModal] Ошибка при обновлении после добавления:', err)
|
||||||
onUpdate()
|
})
|
||||||
|
|
||||||
|
if (onUpdate) {
|
||||||
|
onUpdate()
|
||||||
|
}
|
||||||
|
} else {
|
||||||
|
console.error('[CommentsModal] Неожиданный формат ответа:', result)
|
||||||
|
hapticFeedback('error')
|
||||||
|
// Попробуем перезагрузить комментарии
|
||||||
|
await loadFullPost(post._id)
|
||||||
}
|
}
|
||||||
} catch (error) {
|
} catch (error) {
|
||||||
console.error('Ошибка добавления комментария:', error)
|
console.error('[CommentsModal] Ошибка добавления комментария:', error)
|
||||||
hapticFeedback('error')
|
hapticFeedback('error')
|
||||||
|
// Попробуем перезагрузить комментарии в случае ошибки
|
||||||
|
try {
|
||||||
|
await loadFullPost(post._id)
|
||||||
|
} catch (reloadError) {
|
||||||
|
console.error('[CommentsModal] Ошибка при перезагрузке:', reloadError)
|
||||||
|
}
|
||||||
} finally {
|
} finally {
|
||||||
setLoading(false)
|
setLoading(false)
|
||||||
}
|
}
|
||||||
|
|
@ -169,10 +212,18 @@ export default function CommentsModal({ post, onClose, onUpdate }) {
|
||||||
</div>
|
</div>
|
||||||
) : (
|
) : (
|
||||||
comments
|
comments
|
||||||
.filter(c => c && c.author) // Фильтруем комментарии без автора
|
.filter(c => {
|
||||||
|
// Фильтруем комментарии без автора или с неполным автором
|
||||||
|
return c && c.author && (typeof c.author === 'object') && c.content
|
||||||
|
})
|
||||||
.map((c, index) => {
|
.map((c, index) => {
|
||||||
// Используем _id если есть, иначе index
|
// Используем _id если есть, иначе index
|
||||||
const commentId = c._id || c.id || `comment-${index}`
|
const commentId = c._id || c.id || `comment-${index}`
|
||||||
|
// Проверяем, что автор полностью загружен
|
||||||
|
if (!c.author || typeof c.author !== 'object') {
|
||||||
|
console.warn('[CommentsModal] Комментарий без автора:', c)
|
||||||
|
return null
|
||||||
|
}
|
||||||
return (
|
return (
|
||||||
<div key={commentId} className="comment-item fade-in">
|
<div key={commentId} className="comment-item fade-in">
|
||||||
<img
|
<img
|
||||||
|
|
@ -194,6 +245,7 @@ export default function CommentsModal({ post, onClose, onUpdate }) {
|
||||||
</div>
|
</div>
|
||||||
)
|
)
|
||||||
})
|
})
|
||||||
|
.filter(Boolean) // Убираем null значения
|
||||||
)}
|
)}
|
||||||
</div>
|
</div>
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -103,16 +103,16 @@
|
||||||
}
|
}
|
||||||
|
|
||||||
.user-item-wrapper {
|
.user-item-wrapper {
|
||||||
padding: 8px 16px;
|
padding: 4px 12px;
|
||||||
}
|
}
|
||||||
|
|
||||||
.user-item {
|
.user-item {
|
||||||
display: flex;
|
display: flex;
|
||||||
align-items: center;
|
align-items: center;
|
||||||
gap: 12px;
|
gap: 10px;
|
||||||
cursor: pointer;
|
cursor: pointer;
|
||||||
transition: background 0.2s;
|
transition: background 0.2s;
|
||||||
padding: 8px;
|
padding: 6px;
|
||||||
border-radius: 8px;
|
border-radius: 8px;
|
||||||
position: relative;
|
position: relative;
|
||||||
}
|
}
|
||||||
|
|
@ -122,8 +122,8 @@
|
||||||
}
|
}
|
||||||
|
|
||||||
.user-avatar {
|
.user-avatar {
|
||||||
width: 36px;
|
width: 32px;
|
||||||
height: 36px;
|
height: 32px;
|
||||||
border-radius: 50%;
|
border-radius: 50%;
|
||||||
object-fit: cover;
|
object-fit: cover;
|
||||||
flex-shrink: 0;
|
flex-shrink: 0;
|
||||||
|
|
@ -134,23 +134,23 @@
|
||||||
min-width: 0;
|
min-width: 0;
|
||||||
display: flex;
|
display: flex;
|
||||||
flex-direction: column;
|
flex-direction: column;
|
||||||
gap: 2px;
|
gap: 1px;
|
||||||
}
|
}
|
||||||
|
|
||||||
.user-name {
|
.user-name {
|
||||||
font-size: 15px;
|
font-size: 14px;
|
||||||
font-weight: 600;
|
font-weight: 600;
|
||||||
color: var(--text-primary);
|
color: var(--text-primary);
|
||||||
line-height: 1.2;
|
line-height: 1.3;
|
||||||
white-space: nowrap;
|
white-space: nowrap;
|
||||||
overflow: hidden;
|
overflow: hidden;
|
||||||
text-overflow: ellipsis;
|
text-overflow: ellipsis;
|
||||||
}
|
}
|
||||||
|
|
||||||
.user-username {
|
.user-username {
|
||||||
font-size: 13px;
|
font-size: 12px;
|
||||||
color: var(--text-secondary);
|
color: var(--text-secondary);
|
||||||
line-height: 1.2;
|
line-height: 1.3;
|
||||||
white-space: nowrap;
|
white-space: nowrap;
|
||||||
overflow: hidden;
|
overflow: hidden;
|
||||||
text-overflow: ellipsis;
|
text-overflow: ellipsis;
|
||||||
|
|
@ -158,8 +158,8 @@
|
||||||
|
|
||||||
/* Follow Button Icon */
|
/* Follow Button Icon */
|
||||||
.follow-btn-icon {
|
.follow-btn-icon {
|
||||||
width: 36px;
|
width: 32px;
|
||||||
height: 36px;
|
height: 32px;
|
||||||
border-radius: 50%;
|
border-radius: 50%;
|
||||||
background: var(--bg-primary);
|
background: var(--bg-primary);
|
||||||
color: var(--text-primary);
|
color: var(--text-primary);
|
||||||
|
|
|
||||||
|
|
@ -112,9 +112,9 @@ export default function FollowListModal({ users, title, onClose, currentUser })
|
||||||
onClick={(e) => handleFollowToggle(user._id, e)}
|
onClick={(e) => handleFollowToggle(user._id, e)}
|
||||||
>
|
>
|
||||||
{isFollowing ? (
|
{isFollowing ? (
|
||||||
<UserMinus size={20} />
|
<UserMinus size={18} />
|
||||||
) : (
|
) : (
|
||||||
<UserPlus size={20} />
|
<UserPlus size={18} />
|
||||||
)}
|
)}
|
||||||
</button>
|
</button>
|
||||||
)}
|
)}
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue