import React, { useState } from 'react' import axios from 'axios' /** * PostActions: Like toggle, Comment toggle, Share menu, Report */ export default function PostActions({ post, isLoggedIn, onCommentToggle, onReactionChange, }) { const [liked, setLiked] = useState(post.viewer_liked ?? false) const [likeCount, setLikeCount] = useState(post.reactions_count ?? 0) const [busy, setBusy] = useState(false) const [menuOpen, setMenuOpen] = useState(false) const [shareMsg, setShareMsg] = useState(null) const handleLike = async () => { if (!isLoggedIn) { window.location.href = '/login' return } if (busy) return setBusy(true) try { if (liked) { await axios.delete(`/api/posts/${post.id}/reactions/like`) setLiked(false) setLikeCount((c) => Math.max(0, c - 1)) onReactionChange?.({ liked: false, count: Math.max(0, likeCount - 1) }) } else { await axios.post(`/api/posts/${post.id}/reactions`, { reaction: 'like' }) setLiked(true) setLikeCount((c) => c + 1) onReactionChange?.({ liked: true, count: likeCount + 1 }) } } catch { // ignore } finally { setBusy(false) } } const handleCopyLink = () => { const url = `${window.location.origin}/@${post.author.username}/posts?post=${post.id}` navigator.clipboard?.writeText(url) setShareMsg('Link copied!') setTimeout(() => setShareMsg(null), 2000) setMenuOpen(false) } const handleReport = async () => { setMenuOpen(false) const reason = window.prompt('Why are you reporting this post? (required)') if (!reason?.trim()) return try { await axios.post(`/api/posts/${post.id}/report`, { reason: reason.trim() }) alert('Report submitted. Thank you!') } catch (err) { if (err.response?.data?.message) { alert(err.response.data.message) } } } return (
{/* Like */} {/* Comment toggle */} {/* Share / More menu */}
{menuOpen && (
setMenuOpen(false)} > {isLoggedIn && ( )}
)}
{/* Share feedback toast */} {shareMsg && ( {shareMsg} )}
) }