Merge pull request #4 from IgorVolochay/frontend

This commit is contained in:
IgorVolochay
2026-09-01 17:26:42 +03:00
parent 73d16e9111
commit 61a2eb8076
52 changed files with 3785 additions and 752 deletions
@@ -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>