import { PostMedia, PostReaction } from '../posts/posts.ts' import { useEffect, useState } from 'react' import { Link } from 'react-router-dom' import { PostInfo } from '../posts/usePostViewModel.ts' import { useUserStore } from '../../user/user.ts' interface PostItemProps { post: PostInfo reactions: PostReaction[] addReaction: (emoji: string) => void clearReaction: (emoji: string) => void hideViewButton?: boolean } export default function PostItem({ post, reactions, addReaction, clearReaction, hideViewButton = false, }: PostItemProps) { const formattedDate = post.createdAt.toLocaleString('en-US', { year: 'numeric', month: 'short', day: 'numeric', hour: '2-digit', minute: '2-digit', }) const [visible, setVisible] = useState(false) useEffect(() => { const timeout = setTimeout(() => setVisible(true)) return () => { clearTimeout(timeout) } }, []) const opacity = visible ? 'opacity-100' : 'opacity-0' return (
@{post.authorName}• {formattedDate} {!hideViewButton && ( <> {' • '} View )}
{post.content}
{post.media.length > 0 && (
{post.media.map((media) => ( ))}
)}
) } interface PostReactionsProps { post: PostInfo reactions: PostReaction[] addReaction: (emoji: string) => void clearReaction: (emoji: string) => void } function PostReactions({ post, reactions, addReaction, clearReaction }: PostReactionsProps) { const username = useUserStore((state) => state.user?.username) return (
{post.possibleReactions.map((emoji) => { const count = reactions.filter((r) => r.emoji === emoji).length const didReact = reactions.some((r) => r.emoji == emoji && r.authorName == username) const onClick = () => { if (didReact) { clearReaction(emoji) } else { addReaction(emoji) } } return ( ) })}
) } interface PostReactionButtonProps { emoji: string didReact: boolean count: number onClick: () => void } function PostReactionButton({ emoji, didReact, onClick, count }: PostReactionButtonProps) { const formattedCount = count < 100 ? count.toString() : `99+` return ( ) } interface PostMediaProps { media: PostMedia } function PostMediaItem({ media }: PostMediaProps) { const url = new URL(media.url.toString()) if (location.protocol === 'https:' && url.protocol !== 'https:') { url.protocol = 'https:' } const width = media.width ?? undefined const height = media.height ?? undefined return ( ) }