Files
conve-x/src/components/Post.tsx
T

145 lines
4.3 KiB
TypeScript
Raw Normal View History

'use client';
2023-09-11 13:51:35 -05:00
import { PostWithUserDto } from '@/app/types';
2023-09-11 00:40:14 -05:00
import { Text } from '@codemirror/state';
import { useMutation } from 'convex/react';
2023-09-11 00:40:14 -05:00
import Image from 'next/image';
import Link from 'next/link';
import { useEffect, useRef, useState } from 'react';
2023-09-12 16:56:07 -05:00
import { BiDotsHorizontalRounded, BiSolidLike } from 'react-icons/bi';
import { BsFillReplyFill } from 'react-icons/bs';
import { GoDotFill } from 'react-icons/go';
import { api } from '../../convex/_generated/api';
2023-09-11 00:40:14 -05:00
import PostContent from './PostContent';
2023-09-11 22:05:12 -05:00
export default function Post({
post,
limit = true,
2023-09-12 16:56:07 -05:00
showReply = true,
2023-09-11 22:05:12 -05:00
}: {
post: PostWithUserDto;
limit?: boolean;
2023-09-12 16:56:07 -05:00
showReply?: boolean;
2023-09-11 22:05:12 -05:00
}) {
const deletePostById = useMutation(api.posts.deletePostById);
const modalRef = useRef<HTMLDivElement>(null);
const [modalOpen, setModalOpen] = useState(false);
const username = post.user.clerkUsername ?? post.user._id;
2023-09-11 00:40:14 -05:00
const createdPostDate = new Date(post._creationTime);
const postYear = createdPostDate.getFullYear();
const postMonth = createdPostDate.toLocaleString(undefined, {
month: 'short',
});
const dayOfMonth = createdPostDate.getDate();
2023-09-11 22:05:12 -05:00
const content = limit ? post.content.slice(0, 10) : post.content;
2023-09-12 16:56:07 -05:00
const postLink = `/posts/${post._id}`;
2023-09-11 00:40:14 -05:00
// 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 });
}
2023-09-11 00:40:14 -05:00
return (
<div className='flex w-full gap-4 p-8 bg-white dark:bg-secondary-gray'>
2023-09-11 00:40:14 -05:00
<div>
<Image
alt='user profile image'
src={post.user.clerkImageUrl}
2023-09-11 00:40:14 -05:00
width={40}
height={40}
className='rounded-full object-cover border-4 border-primary-accent'
/>
</div>
<div className='flex flex-1 flex-col gap-2'>
<div className='flex items-center justify-between gap-2'>
<div className='flex items-center gap-2'>
<div>{username}</div>
<GoDotFill className='w-3 h-3' />
<div className='text-xs'>
{`${postMonth} ${dayOfMonth}, ${postYear}`}
</div>
</div>
<div className='relative'>
<button onClick={() => setModalOpen(!modalOpen)}>
<BiDotsHorizontalRounded />
</button>
<div
ref={modalRef}
className={`${
modalOpen ? '' : 'hidden'
} max-w-min whitespace-nowrap absolute top-5 right-0 rounded-md py-2 pl-4 pr-10 shadow-md bg-white dark:bg-primary-gray`}
>
<ul>
<li>
2023-09-11 21:23:37 -05:00
<Link
onClick={() => setModalOpen(false)}
2023-09-12 16:56:07 -05:00
href={`${postLink}/edit`}
2023-09-11 21:23:37 -05:00
>
Edit post
</Link>
</li>
<li className='text-red-600'>
<button
onClick={handleDeleteClick}
type='button'
>
Delete post
</button>
</li>
</ul>
</div>
</div>
2023-09-11 00:40:14 -05:00
</div>
2023-09-11 22:05:12 -05:00
<div
className={`${limit ? 'max-h-80' : ''} overflow-hidden text-ellipsis`}
>
<PostContent content={Text.of(content).toString()} />
</div>
2023-09-11 00:40:14 -05:00
<div>
2023-09-11 22:05:12 -05:00
{limit && post.content.length > 10 ? (
<Link
className='font-semibold text-primary-accent'
2023-09-12 16:56:07 -05:00
href={postLink}
>
Show more
</Link>
) : null}
2023-09-11 00:40:14 -05:00
</div>
2023-09-12 16:56:07 -05:00
<div className='flex items-center gap-4'>
{showReply ? (
<Link href={{ pathname: postLink, query: { reply: true } }}>
<BsFillReplyFill />
</Link>
) : null}
<button>
<BiSolidLike />
</button>
</div>
2023-09-11 00:40:14 -05:00
</div>
</div>
);
}