"use client";
import { useState } from "react";
import { FaReply, FaTrash, FaEye, FaEyeSlash, FaEdit, FaSave, FaTimes } from "react-icons/fa";
type Reply = {
id: string;
text: string;
timestamp: string;
username: string;
hidden?: boolean;
like_count?: number;
};
type Comment = {
id: string;
text: string;
username: string;
timestamp: string;
like_count?: number;
hidden?: boolean;
replies?: { data: Reply[] };
};
interface SocialCommentItemProps {
comment: Comment;
onReply: (commentId: string, text: string) => void;
onDelete: (commentId: string, isReply?: boolean, parentId?: string) => void;
onHide: (commentId: string, currentHiddenStatus: boolean, isReply?: boolean) => void;
onEdit: (commentId: string, newText: string, isReply: boolean, parentId?: string) => void;
}
const SocialCommentItem = ({ comment, onReply, onDelete, onHide, onEdit }: SocialCommentItemProps) => {
const [replyText, setReplyText] = useState("");
const [isReplying, setIsReplying] = useState(false);
const [isEditing, setIsEditing] = useState(false);
const [editText, setEditText] = useState(comment.text);
const handlePostReply = () => {
if (!replyText.trim()) return;
onReply(comment.id, replyText);
setReplyText("");
setIsReplying(false);
};
const handleSaveEdit = () => {
if (!editText.trim()) return;
onEdit(comment.id, editText, false, undefined);
setIsEditing(false);
};
return (
{/* TOP LEVEL COMMENT */}
{comment.username}
{new Date(comment.timestamp).toLocaleString()}
{comment.hidden && (
Hidden
)}
{isEditing ? (
) : (
{comment.text}
)}
{/* ACTIONS BAR */}
{/* REPLY INPUT AREA */}
{isReplying && (
setReplyText(e.target.value)}
className="flex-1 bg-[#111111] border border-white/10 rounded-lg px-3 py-2 text-white focus:outline-none focus:border-indigo-500"
/>
)}
{/* REPLIES LIST */}
{comment.replies?.data && comment.replies.data.length > 0 && (
{comment.replies.data.map((reply) => (
))}
)}
);
};
const ReplyItem = ({
reply,
parentId,
onDelete,
onHide,
onEdit
}: {
reply: Reply;
parentId: string;
onDelete: (id: string, isReply: boolean, parentId?: string) => void; // parentId needed to update state optimistically? Maybe not for DB but for state.
onHide: (id: string, currentStatus: boolean, isReply: boolean) => void;
onEdit: (id: string, text: string, isReply: boolean, parentId: string) => void;
}) => {
const [isEditing, setIsEditing] = useState(false);
const [editText, setEditText] = useState(reply.text);
const handleSaveEdit = () => {
if (!editText.trim()) return;
onEdit(reply.id, editText, true, parentId);
setIsEditing(false);
};
return (
{reply.username}
{new Date(reply.timestamp).toLocaleString()}
{reply.hidden && (
Hidden
)}
{isEditing ? (
) : (
{reply.text}
)}
{/* ACTION BUTTONS FOR REPLY */}
);
};
export default SocialCommentItem;