'use client'; import { PostWithUserDto } from '@/app/types'; import { Text } from '@codemirror/state'; import { useMutation } from 'convex/react'; import Image from 'next/image'; import Link from 'next/link'; import { useEffect, useRef, useState } from 'react'; import { BiDotsHorizontalRounded } from 'react-icons/bi'; import { GoDotFill } from 'react-icons/go'; import { api } from '../../convex/_generated/api'; import PostContent from './PostContent'; export default function Post({ post, limit = true, }: { post: PostWithUserDto; limit?: boolean; }) { const deletePostById = useMutation(api.posts.deletePostById); const modalRef = useRef(null); const [modalOpen, setModalOpen] = useState(false); const username = post.user.clerkUsername ?? post.user._id; const createdPostDate = new Date(post._creationTime); const postYear = createdPostDate.getFullYear(); const postMonth = createdPostDate.toLocaleString(undefined, { month: 'short', }); const dayOfMonth = createdPostDate.getDate(); const content = limit ? post.content.slice(0, 10) : post.content; // TODO: Add reply and like buttons useEffect(() => { function handleClickOutside(e: MouseEvent) { if ( modalOpen && modalRef.current && modalRef.current.contains(e.target as Node) === false ) { setModalOpen(false); } } document.addEventListener('click', handleClickOutside); return () => document.removeEventListener('click', handleClickOutside); }, [modalOpen]); async function handleDeleteClick() { setModalOpen(false); const result = confirm('Are you sure you want to delete this post?'); if (result === false) { return; } await deletePostById({ id: post._id }); } return (
user profile image
{username}
{`${postMonth} ${dayOfMonth}, ${postYear}`}
  • setModalOpen(false)} href={`/posts/${post._id}/edit`} > Edit post
{limit && post.content.length > 10 ? ( Show more ) : null}
); }