"use client"; import { useEffect, useState } from "react"; interface PostProps { post: { id: string; timeStamp: string; reactions: { liked: boolean; warned: boolean }[]; userId: string; image: string; // Assuming the image is served as a base64 string or URL imageDes: string; // New field for the post description }; onLike: () => void; onWarning: () => void; onDelete: () => void; // Callback for deleting the post allowReactions: boolean; // New property to toggle reactions } export default function Post({ post, onLike, onWarning, onDelete, allowReactions, }: PostProps) { const [userData, setUserData] = useState<{ username: string; avatar: number } | null>({ username: "Loading...", avatar: 1, }); useEffect(() => { // Fetch the username and avatar based on the userId fetch(`/api/user/${post.userId}`) .then((res) => res.json()) .then((data) => { if (data.user) { setUserData({ username: data.user.username, avatar: data.user.avatar || "/default-avatar.png", // Fallback to a default avatar }); } else { setUserData({ username: "Unknown User", avatar: 1, }); } }) .catch((err) => { console.error("Error fetching user data:", err); setUserData({ username: "Unknown User", avatar: 1, }); }); }, [post.userId]); return (
{userData?.username || "Loading..."}
{/* Timestamp */}{new Date(post.timeStamp).toLocaleString()}
{post.imageDes}
)} {/* Post Actions */}