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

102 lines
3.1 KiB
TypeScript
Raw Normal View History

'use client';
2023-09-11 13:51:35 -05:00
import { PostWithUserDto } from '@/app/types';
2023-09-14 21:47:40 -05:00
import { useUser } from '@clerk/nextjs';
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 { GoDotFill } from 'react-icons/go';
import { api } from '../../convex/_generated/api';
2023-09-14 21:47:40 -05:00
import PostActionButton from './PostActionButtons';
import PostActionModal from './PostActionModal';
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
}) {
2023-09-14 21:47:40 -05:00
const { isLoaded, user, isSignedIn } = useUser();
const deletePostById = useMutation(api.posts.deletePostById);
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
async function handleDeleteClick() {
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 (
2023-09-12 23:12:12 -05:00
<div className='flex w-full gap-4 p-8 bg-white dark:bg-secondary-gray flex-wrap'>
<div className='flex-shrink-0'>
<Link href={`/profile/${post.user.clerkUserId}`}>
<Image
alt='user profile image'
src={post.user.clerkImageUrl}
width={40}
height={40}
className='rounded-full object-cover border-4 border-primary-accent'
/>
</Link>
2023-09-11 00:40:14 -05:00
</div>
2023-09-12 23:12:12 -05:00
<div className='flex flex-1 flex-col gap-2 min-w-0'>
<div className='flex items-center justify-between gap-2'>
2023-09-12 23:12:12 -05:00
<div className='flex items-center gap-2 flex-wrap min-w-0'>
<div
className='overflow-hidden text-ellipsis'
title={username}
>
{username}
</div>
<div className='flex items-center gap-2'>
<GoDotFill className='w-3 h-3' />
<div className='text-xs'>
{`${postMonth} ${dayOfMonth}, ${postYear}`}
</div>
</div>
</div>
2023-09-14 21:47:40 -05:00
{isLoaded && isSignedIn && user.id === post.user.clerkUserId ? (
<PostActionModal postId={post._id} />
) : null}
2023-09-11 00:40:14 -05:00
</div>
2023-09-11 22:05:12 -05:00
<div
2023-09-12 23:12:12 -05:00
className={`${limit ? 'max-h-80' : ''} flex flex-col overflow-hidden`}
2023-09-11 22:05:12 -05:00
>
<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-14 21:47:40 -05:00
<PostActionButton
postId={post._id}
showReply={showReply}
/>
2023-09-11 00:40:14 -05:00
</div>
</div>
);
}