"use client"; import { useState, useTransition } from "react"; import { addComment, deleteComment } from "@/lib/actions/student-actions"; type Comment = { id: string; text: string; deleted: boolean; createdAt: Date; user: { id: string; name: string }; }; interface Props { lessonId: string; slug: string; comments: Comment[]; currentUserId: string; currentUserRole: string; } export function LessonComments({ lessonId, slug, comments, currentUserId, currentUserRole }: Props) { const [text, setText] = useState(""); const [isPending, startTransition] = useTransition(); const [error, setError] = useState(null); const canModerate = currentUserRole === "curator" || currentUserRole === "admin"; function handleAdd(e: React.FormEvent) { e.preventDefault(); if (!text.trim()) return; setError(null); startTransition(async () => { try { await addComment(lessonId, slug, text.trim()); setText(""); } catch { setError("Не удалось отправить комментарий. Попробуйте ещё раз."); } }); } function handleDelete(commentId: string) { startTransition(async () => { try { await deleteComment(commentId, lessonId, slug); } catch { // ignore } }); } return (
{/* Comment list */}
{comments.length === 0 && (

Пока нет комментариев. Будьте первым!

)} {comments.map((comment) => (
{/* Avatar */}
{comment.user.name[0]?.toUpperCase() ?? "?"}
{/* Body */}
{comment.user.name} {new Date(comment.createdAt).toLocaleDateString("ru-RU", { day: "numeric", month: "long", year: "numeric", hour: "2-digit", minute: "2-digit", })}
{comment.deleted ? (

[Комментарий удалён]

) : (

{comment.text}

)} {!comment.deleted && (comment.user.id === currentUserId || canModerate) && ( )}
))}
{/* Add comment form */}