Merge pull request #4 from IgorVolochay/frontend
This commit is contained in:
@@ -2,18 +2,26 @@
|
||||
display: flex;
|
||||
gap: var(--space-md);
|
||||
padding: var(--space-md) 0;
|
||||
transition: background var(--duration-fast) var(--ease-smooth);
|
||||
}
|
||||
|
||||
.comment-item + .comment-item {
|
||||
border-top: 1px solid rgba(255, 255, 255, 0.06);
|
||||
}
|
||||
|
||||
.comment-item--me {
|
||||
background: rgba(124, 58, 237, 0.04);
|
||||
border-radius: var(--radius-sm);
|
||||
padding: var(--space-sm) var(--space-xs);
|
||||
}
|
||||
|
||||
.comment-avatar {
|
||||
flex-shrink: 0;
|
||||
width: 40px;
|
||||
height: 40px;
|
||||
width: 38px;
|
||||
height: 38px;
|
||||
border-radius: 50%;
|
||||
overflow: hidden;
|
||||
border: 1px solid var(--glass-border);
|
||||
}
|
||||
|
||||
.comment-avatar-img {
|
||||
@@ -28,13 +36,17 @@
|
||||
justify-content: center;
|
||||
width: 100%;
|
||||
height: 100%;
|
||||
background: linear-gradient(135deg, var(--color-card-a-to), var(--color-card-b-to));
|
||||
background: linear-gradient(135deg, var(--color-card-a-from), var(--color-card-b-from));
|
||||
font-family: var(--font-display);
|
||||
font-size: 16px;
|
||||
font-size: 15px;
|
||||
font-weight: 700;
|
||||
color: var(--color-text);
|
||||
}
|
||||
|
||||
.comment-avatar-fallback--me {
|
||||
background: linear-gradient(135deg, var(--color-card-a-to), var(--color-card-b-to));
|
||||
}
|
||||
|
||||
.comment-body {
|
||||
flex: 1;
|
||||
min-width: 0;
|
||||
@@ -42,20 +54,43 @@
|
||||
|
||||
.comment-header {
|
||||
display: flex;
|
||||
align-items: baseline;
|
||||
align-items: center;
|
||||
justify-content: space-between;
|
||||
gap: var(--space-sm);
|
||||
margin-bottom: 4px;
|
||||
}
|
||||
|
||||
.comment-author-group {
|
||||
display: flex;
|
||||
align-items: center;
|
||||
gap: 6px;
|
||||
min-width: 0;
|
||||
}
|
||||
|
||||
.comment-author {
|
||||
font-weight: 600;
|
||||
font-size: 14px;
|
||||
color: var(--color-stat-a);
|
||||
white-space: nowrap;
|
||||
overflow: hidden;
|
||||
text-overflow: ellipsis;
|
||||
}
|
||||
|
||||
.comment-me-badge {
|
||||
font-size: 10px;
|
||||
font-weight: 700;
|
||||
padding: 1px 6px;
|
||||
border-radius: 4px;
|
||||
background: linear-gradient(135deg, var(--color-card-a-to), var(--color-card-b-to));
|
||||
color: #FFFFFF;
|
||||
text-transform: uppercase;
|
||||
letter-spacing: 0.5px;
|
||||
}
|
||||
|
||||
.comment-date {
|
||||
font-size: 12px;
|
||||
font-size: 11px;
|
||||
color: var(--color-muted);
|
||||
flex-shrink: 0;
|
||||
}
|
||||
|
||||
.comment-text {
|
||||
|
||||
@@ -1,31 +1,57 @@
|
||||
import React from 'react';
|
||||
import React, { useState, useEffect } from 'react';
|
||||
import { currentUser } from '../../services/auth';
|
||||
import './CommentItem.css';
|
||||
|
||||
export default function CommentItem({ comment, author }) {
|
||||
const displayName = author?.username || author?.first_name || 'Аноним';
|
||||
const date = comment.creation_date
|
||||
const [imageError, setImageError] = useState(false);
|
||||
const isMe = comment.author_id === currentUser?.id;
|
||||
|
||||
const displayName = isMe
|
||||
? (currentUser?.username ? `@${currentUser.username}` : currentUser?.first_name || 'Вы')
|
||||
: (author?.username ? `@${author.username}` : author?.first_name || `Игрок #${comment.author_id}`);
|
||||
|
||||
const photoUrl = isMe ? currentUser?.photo_url : author?.photo_url;
|
||||
|
||||
useEffect(() => {
|
||||
setImageError(false);
|
||||
}, [photoUrl]);
|
||||
|
||||
const dateStr = comment.creation_date
|
||||
? new Date(comment.creation_date).toLocaleDateString('ru-RU', {
|
||||
day: '2-digit',
|
||||
month: '2-digit',
|
||||
year: 'numeric',
|
||||
hour: '2-digit',
|
||||
minute: '2-digit',
|
||||
})
|
||||
: '';
|
||||
|
||||
const initial = displayName.replace(/^@/, '')[0]?.toUpperCase() || '?';
|
||||
|
||||
return (
|
||||
<div className="comment-item">
|
||||
<div className={`comment-item ${isMe ? 'comment-item--me' : ''}`}>
|
||||
<div className="comment-avatar">
|
||||
{author?.photo_url ? (
|
||||
<img src={author.photo_url} alt="" className="comment-avatar-img" />
|
||||
{photoUrl && !imageError ? (
|
||||
<img
|
||||
src={photoUrl}
|
||||
alt=""
|
||||
className="comment-avatar-img"
|
||||
referrerPolicy="no-referrer"
|
||||
onError={() => setImageError(true)}
|
||||
/>
|
||||
) : (
|
||||
<span className="comment-avatar-fallback">
|
||||
{displayName[0]?.toUpperCase() || '?'}
|
||||
<span className={`comment-avatar-fallback ${isMe ? 'comment-avatar-fallback--me' : ''}`}>
|
||||
{initial}
|
||||
</span>
|
||||
)}
|
||||
</div>
|
||||
|
||||
<div className="comment-body">
|
||||
<div className="comment-header">
|
||||
<span className="comment-author">{displayName}</span>
|
||||
<span className="comment-date">{date}</span>
|
||||
<div className="comment-author-group">
|
||||
<span className="comment-author">{displayName}</span>
|
||||
{isMe && <span className="comment-me-badge">Вы</span>}
|
||||
</div>
|
||||
<span className="comment-date">{dateStr}</span>
|
||||
</div>
|
||||
<p className="comment-text">{comment.comment_text || comment.commet_text}</p>
|
||||
</div>
|
||||
|
||||
@@ -7,6 +7,8 @@
|
||||
background: var(--color-bg);
|
||||
transform: translateY(100%);
|
||||
transition: transform var(--duration-normal) var(--ease-smooth);
|
||||
padding-top: max(0px, env(safe-area-inset-top));
|
||||
padding-bottom: max(0px, env(safe-area-inset-bottom));
|
||||
}
|
||||
|
||||
.comments-panel--open {
|
||||
@@ -40,6 +42,15 @@
|
||||
font-weight: 600;
|
||||
}
|
||||
|
||||
.comments-count {
|
||||
font-size: 12px;
|
||||
font-weight: 700;
|
||||
color: var(--color-muted);
|
||||
background: rgba(255, 255, 255, 0.08);
|
||||
padding: 2px 8px;
|
||||
border-radius: var(--radius-full);
|
||||
}
|
||||
|
||||
/* List */
|
||||
.comments-list {
|
||||
flex: 1;
|
||||
@@ -47,8 +58,31 @@
|
||||
padding: 0 var(--space-lg);
|
||||
}
|
||||
|
||||
.comments-placeholder {
|
||||
.comments-loading {
|
||||
display: flex;
|
||||
flex-direction: column;
|
||||
align-items: center;
|
||||
justify-content: center;
|
||||
padding: var(--space-xl);
|
||||
gap: var(--space-md);
|
||||
}
|
||||
|
||||
.comments-spinner {
|
||||
width: 28px;
|
||||
height: 28px;
|
||||
border: 3px solid rgba(255, 255, 255, 0.1);
|
||||
border-top-color: var(--color-card-a-to);
|
||||
border-radius: 50%;
|
||||
animation: spin 0.8s linear infinite;
|
||||
}
|
||||
|
||||
@keyframes spin {
|
||||
to {
|
||||
transform: rotate(360deg);
|
||||
}
|
||||
}
|
||||
|
||||
.comments-placeholder {
|
||||
text-align: center;
|
||||
color: var(--color-muted);
|
||||
font-size: 14px;
|
||||
@@ -60,9 +94,15 @@
|
||||
align-items: center;
|
||||
justify-content: center;
|
||||
height: 100%;
|
||||
min-height: 200px;
|
||||
gap: var(--space-sm);
|
||||
}
|
||||
|
||||
.comments-empty-icon {
|
||||
font-size: 32px;
|
||||
margin-bottom: var(--space-xs);
|
||||
}
|
||||
|
||||
.comments-empty-text {
|
||||
font-family: var(--font-display);
|
||||
font-size: 16px;
|
||||
@@ -89,15 +129,20 @@
|
||||
.comments-input {
|
||||
flex: 1;
|
||||
resize: none;
|
||||
padding: var(--space-sm) var(--space-md);
|
||||
padding: 10px var(--space-md);
|
||||
background: var(--glass-bg);
|
||||
border: 1px solid var(--glass-border);
|
||||
border-radius: var(--radius-sm);
|
||||
color: var(--color-text);
|
||||
font-size: 14px;
|
||||
line-height: 1.4;
|
||||
min-height: 40px;
|
||||
min-height: 42px;
|
||||
max-height: 100px;
|
||||
transition: border-color var(--duration-fast) var(--ease-smooth);
|
||||
}
|
||||
|
||||
.comments-input:focus {
|
||||
border-color: var(--color-card-a-to);
|
||||
}
|
||||
|
||||
.comments-input::placeholder {
|
||||
@@ -108,8 +153,8 @@
|
||||
display: flex;
|
||||
align-items: center;
|
||||
justify-content: center;
|
||||
width: 40px;
|
||||
height: 40px;
|
||||
width: 42px;
|
||||
height: 42px;
|
||||
border-radius: 50%;
|
||||
background: var(--glass-bg);
|
||||
color: var(--color-muted);
|
||||
@@ -118,10 +163,21 @@
|
||||
}
|
||||
|
||||
.comments-send--active {
|
||||
background: var(--color-like);
|
||||
color: var(--color-text);
|
||||
background: linear-gradient(135deg, var(--color-card-a-to), var(--color-card-b-to));
|
||||
color: #FFFFFF;
|
||||
box-shadow: 0 0 12px rgba(124, 58, 237, 0.3);
|
||||
}
|
||||
|
||||
.comments-send:disabled {
|
||||
cursor: default;
|
||||
opacity: 0.6;
|
||||
}
|
||||
|
||||
.comments-btn-spinner {
|
||||
width: 18px;
|
||||
height: 18px;
|
||||
border: 2px solid rgba(255, 255, 255, 0.2);
|
||||
border-top-color: #FFFFFF;
|
||||
border-radius: 50%;
|
||||
animation: spin 0.8s linear infinite;
|
||||
}
|
||||
|
||||
@@ -1,14 +1,23 @@
|
||||
import React, { useState, useEffect, useRef } from 'react';
|
||||
import React, { useState, useEffect, useRef, useCallback } from 'react';
|
||||
import { useApp } from '../../context/AppContext';
|
||||
import { showBackButton } from '../../services/auth';
|
||||
import { showBackButton, currentUser, hapticNotification, hapticImpact } from '../../services/auth';
|
||||
import { api } from '../../services/api';
|
||||
import { currentUser } from '../../services/auth';
|
||||
import CommentItem from './CommentItem';
|
||||
import './CommentsPanel.css';
|
||||
|
||||
export default function CommentsPanel() {
|
||||
const { isCommentsOpen, setIsCommentsOpen, currentCard, showToast } = useApp();
|
||||
const {
|
||||
isCommentsOpen,
|
||||
setIsCommentsOpen,
|
||||
currentCard,
|
||||
showToast,
|
||||
getUserProfile,
|
||||
handleApiResponse,
|
||||
syncCardComments,
|
||||
addCommentToCard,
|
||||
} = useApp();
|
||||
const [comments, setComments] = useState([]);
|
||||
const [authors, setAuthors] = useState({});
|
||||
const [isLoading, setIsLoading] = useState(false);
|
||||
const [newComment, setNewComment] = useState('');
|
||||
const [isSending, setIsSending] = useState(false);
|
||||
@@ -17,59 +26,100 @@ export default function CommentsPanel() {
|
||||
// Telegram BackButton
|
||||
useEffect(() => {
|
||||
if (isCommentsOpen) {
|
||||
const cleanup = showBackButton(() => setIsCommentsOpen(false));
|
||||
const cleanup = showBackButton(() => {
|
||||
hapticImpact('light');
|
||||
setIsCommentsOpen(false);
|
||||
});
|
||||
return cleanup;
|
||||
}
|
||||
}, [isCommentsOpen, setIsCommentsOpen]);
|
||||
|
||||
// Load comments when panel opens
|
||||
useEffect(() => {
|
||||
if (isCommentsOpen && currentCard) {
|
||||
loadComments();
|
||||
}
|
||||
// eslint-disable-next-line react-hooks/exhaustive-deps
|
||||
}, [isCommentsOpen, currentCard?.card_id]);
|
||||
const cardId = currentCard?.card_id;
|
||||
|
||||
async function loadComments() {
|
||||
const loadComments = useCallback(async () => {
|
||||
if (!cardId) return;
|
||||
setIsLoading(true);
|
||||
|
||||
try {
|
||||
// TODO: Replace with real GET /get_comments when backend implements it
|
||||
const result = await api.getComments(currentCard.card_id);
|
||||
if (!result.error) {
|
||||
setComments(result.result || []);
|
||||
const result = await api.getComments(cardId);
|
||||
handleApiResponse(result);
|
||||
|
||||
if (!result.error && Array.isArray(result.result)) {
|
||||
const loadedComments = result.result;
|
||||
setComments(loadedComments);
|
||||
syncCardComments(cardId, loadedComments.map((c) => c.comment_id));
|
||||
|
||||
// Fetch author profiles for all unique authors
|
||||
const uniqueAuthorIds = Array.from(new Set(loadedComments.map((c) => c.author_id)));
|
||||
const authorsData = {};
|
||||
|
||||
await Promise.all(
|
||||
uniqueAuthorIds.map(async (authorId) => {
|
||||
const profile = await getUserProfile(authorId);
|
||||
if (profile) {
|
||||
authorsData[authorId] = profile;
|
||||
}
|
||||
})
|
||||
);
|
||||
|
||||
setAuthors(authorsData);
|
||||
} else {
|
||||
setComments([]);
|
||||
}
|
||||
} catch (err) {
|
||||
console.error('Load comments error:', err);
|
||||
} finally {
|
||||
setIsLoading(false);
|
||||
}
|
||||
}
|
||||
}, [cardId, getUserProfile, handleApiResponse, syncCardComments]);
|
||||
|
||||
// Load comments only when panel opens or active card ID changes
|
||||
useEffect(() => {
|
||||
if (isCommentsOpen && cardId) {
|
||||
loadComments();
|
||||
}
|
||||
}, [isCommentsOpen, cardId, loadComments]);
|
||||
|
||||
async function handleSend() {
|
||||
if (!newComment.trim() || isSending) return;
|
||||
const text = newComment.trim();
|
||||
if (!text || isSending || !currentCard) return;
|
||||
|
||||
setIsSending(true);
|
||||
hapticImpact('light');
|
||||
|
||||
try {
|
||||
const result = await api.addComment(currentUser.id, currentCard.card_id, newComment.trim());
|
||||
if (!result.error) {
|
||||
const result = await api.addComment(currentUser.id, currentCard.card_id, text);
|
||||
handleApiResponse(result);
|
||||
|
||||
if (!result.error && result.result) {
|
||||
setNewComment('');
|
||||
showToast('Комментарий отправлен');
|
||||
|
||||
// Optimistically add the new comment to the list
|
||||
if (result.result) {
|
||||
setComments(prev => [...prev, result.result]);
|
||||
|
||||
// Scroll to bottom after adding
|
||||
setTimeout(() => {
|
||||
if (listRef.current) {
|
||||
listRef.current.scrollTop = listRef.current.scrollHeight;
|
||||
}
|
||||
}, 100);
|
||||
}
|
||||
showToast('Комментарий опубликован');
|
||||
hapticNotification('success');
|
||||
|
||||
const createdComment = result.result;
|
||||
setComments((prev) => [...prev, createdComment]);
|
||||
addCommentToCard(currentCard.card_id, createdComment.comment_id);
|
||||
|
||||
// Add current user to authors map
|
||||
setAuthors((prev) => ({
|
||||
...prev,
|
||||
[currentUser.id]: currentUser,
|
||||
}));
|
||||
|
||||
// Scroll to bottom
|
||||
setTimeout(() => {
|
||||
if (listRef.current) {
|
||||
listRef.current.scrollTop = listRef.current.scrollHeight;
|
||||
}
|
||||
}, 100);
|
||||
} else {
|
||||
showToast('Ошибка: ' + (result.result || 'неизвестная'));
|
||||
showToast(typeof result.result === 'string' ? result.result : 'Ошибка отправки комментария');
|
||||
hapticNotification('error');
|
||||
}
|
||||
} catch (err) {
|
||||
console.error('Send comment error:', err);
|
||||
showToast('Ошибка отправки');
|
||||
hapticNotification('error');
|
||||
} finally {
|
||||
setIsSending(false);
|
||||
}
|
||||
@@ -82,30 +132,44 @@ export default function CommentsPanel() {
|
||||
}
|
||||
}
|
||||
|
||||
const handleClose = () => {
|
||||
hapticImpact('light');
|
||||
setIsCommentsOpen(false);
|
||||
};
|
||||
|
||||
return (
|
||||
<div className={`comments-panel ${isCommentsOpen ? 'comments-panel--open' : ''}`}>
|
||||
<div className="comments-header">
|
||||
<button
|
||||
className="comments-close"
|
||||
onClick={() => setIsCommentsOpen(false)}
|
||||
onClick={handleClose}
|
||||
aria-label="Закрыть"
|
||||
>
|
||||
←
|
||||
</button>
|
||||
<h2 className="comments-title">Комментарии</h2>
|
||||
<span className="comments-count">{comments.length}</span>
|
||||
</div>
|
||||
|
||||
<div className="comments-list custom-scroll" ref={listRef}>
|
||||
{isLoading ? (
|
||||
<p className="comments-placeholder">Загрузка...</p>
|
||||
<div className="comments-loading">
|
||||
<div className="comments-spinner" />
|
||||
<p className="comments-placeholder">Загрузка комментариев...</p>
|
||||
</div>
|
||||
) : comments.length === 0 ? (
|
||||
<div className="comments-empty">
|
||||
<div className="comments-empty-icon">💬</div>
|
||||
<p className="comments-empty-text">Комментариев пока нет</p>
|
||||
<p className="comments-empty-sub">Будь первым!</p>
|
||||
<p className="comments-empty-sub">Будь первым, кто поделится мнением!</p>
|
||||
</div>
|
||||
) : (
|
||||
comments.map((comment, i) => (
|
||||
<CommentItem key={comment.comment_id || i} comment={comment} author={null} />
|
||||
<CommentItem
|
||||
key={comment.comment_id || i}
|
||||
comment={comment}
|
||||
author={authors[comment.author_id] || null}
|
||||
/>
|
||||
))
|
||||
)}
|
||||
</div>
|
||||
@@ -113,22 +177,27 @@ export default function CommentsPanel() {
|
||||
<div className="comments-input-area">
|
||||
<textarea
|
||||
className="comments-input"
|
||||
placeholder="Ваш комментарий..."
|
||||
placeholder="Напишите комментарий..."
|
||||
value={newComment}
|
||||
onChange={(e) => setNewComment(e.target.value)}
|
||||
onKeyDown={handleKeyDown}
|
||||
maxLength={300}
|
||||
rows={1}
|
||||
disabled={isSending}
|
||||
/>
|
||||
<button
|
||||
className={`comments-send ${newComment.trim() ? 'comments-send--active' : ''}`}
|
||||
className={`comments-send ${newComment.trim() && !isSending ? 'comments-send--active' : ''}`}
|
||||
onClick={handleSend}
|
||||
disabled={!newComment.trim() || isSending}
|
||||
aria-label="Отправить"
|
||||
>
|
||||
<svg viewBox="0 0 24 24" fill="currentColor" width="20" height="20">
|
||||
<path d="M2.01 21L23 12 2.01 3 2 10l15 2-15 2z" />
|
||||
</svg>
|
||||
{isSending ? (
|
||||
<span className="comments-btn-spinner" />
|
||||
) : (
|
||||
<svg viewBox="0 0 24 24" fill="currentColor" width="20" height="20">
|
||||
<path d="M2.01 21L23 12 2.01 3 2 10l15 2-15 2z" />
|
||||
</svg>
|
||||
)}
|
||||
</button>
|
||||
</div>
|
||||
</div>
|
||||
|
||||
Reference in New Issue
Block a user