'use client' import { useState } from 'react' type Reason = { key: string; label: string; desc: string } export type PostActionLabels = { like: string copyLink: string copied: string report: string bookmark: string bookmarked: string loginRequired: string reportTitle: string reportIntro: string reportDetail: string reportSubmit: string reportSent: string reminderTitle: string savedBookmark: string remind2h: string remindTomorrow: string remind3days: string remindMore: string editTitle: string notePlaceholder: string save: string cancel: string remove: string reminderLabel: string rNone: string rTomorrow: string rNextWeek: string rNextMonth: string rCustom: string } const at8am = (daysAhead: number) => { const d = new Date() d.setDate(d.getDate() + daysAhead) d.setHours(8, 0, 0, 0) return Math.floor(d.getTime() / 1000) } export function PostActions({ postId, permalink, canAct, loginHref, initialLikes, initialLiked, initialBookmarked, initialNote, initialRemindAt, locale, reasons, labels, }: { postId: number permalink: string canAct: boolean loginHref: string initialLikes: number initialLiked: boolean initialBookmarked: boolean initialNote: string initialRemindAt: number | null locale: string reasons: Reason[] labels: PostActionLabels }) { const [likes, setLikes] = useState(initialLikes) const [liked, setLiked] = useState(initialLiked) const [bookmarked, setBookmarked] = useState(initialBookmarked) const [note, setNote] = useState(initialNote) const [remindAt, setRemindAt] = useState(initialRemindAt) const [copied, setCopied] = useState(false) const [busy, setBusy] = useState(false) const [reportOpen, setReportOpen] = useState(false) const [reportReason, setReportReason] = useState('') const [reportDetail, setReportDetail] = useState('') const [reportSent, setReportSent] = useState(false) const [remindPop, setRemindPop] = useState(false) const [editOpen, setEditOpen] = useState(false) const requireLogin = () => { window.location.href = loginHref } const toggleLike = async () => { if (!canAct) return requireLogin() if (busy) return setBusy(true) try { const res = await fetch('/api/forum/like', { method: 'POST', headers: { 'Content-Type': 'application/json' }, body: JSON.stringify({ postId }), }) const d = await res.json() if (d.ok) { setLiked(d.liked) setLikes(d.likes) } } finally { setBusy(false) } } const copyLink = async () => { const url = `${window.location.origin}${permalink}` try { await navigator.clipboard.writeText(url) } catch { /* sin clipboard API */ } setCopied(true) setTimeout(() => setCopied(false), 1500) } const toggleBookmark = async () => { if (!canAct) return requireLogin() if (busy) return setBusy(true) try { const res = await fetch('/api/forum/bookmark', { method: 'POST', headers: { 'Content-Type': 'application/json' }, body: JSON.stringify({ postId }), }) const d = await res.json() if (d.ok) { setBookmarked(d.bookmarked) if (d.bookmarked) setRemindPop(true) else { setNote('') setRemindAt(null) } } } finally { setBusy(false) } } const saveReminder = async (when: number | null, closeAll = true) => { setBusy(true) try { const res = await fetch('/api/forum/bookmark-reminder', { method: 'POST', headers: { 'Content-Type': 'application/json' }, body: JSON.stringify({ postId, note, remindAt: when }), }) const d = await res.json() if (d.ok) { setBookmarked(true) setRemindAt(when) if (closeAll) { setRemindPop(false) setEditOpen(false) } } } finally { setBusy(false) } } const submitReport = async () => { if (!reportReason || busy) return setBusy(true) try { const res = await fetch('/api/forum/report', { method: 'POST', headers: { 'Content-Type': 'application/json' }, body: JSON.stringify({ postId, reason: reportReason, detail: reportDetail }), }) const d = await res.json() if (d.ok) { setReportSent(true) setTimeout(() => { setReportOpen(false) setReportSent(false) setReportReason('') setReportDetail('') }, 1200) } } finally { setBusy(false) } } const fmtRemind = (ts: number) => new Date(ts * 1000).toLocaleString(locale, { day: '2-digit', month: 'short', hour: '2-digit', minute: '2-digit' }) return (
{remindPop && (
{labels.savedBookmark}
{labels.reminderTitle}
)}
{/* Modal denuncia */} {reportOpen && (
setReportOpen(false)}>
e.stopPropagation()}>

{labels.reportTitle}

{reportSent ? (

{labels.reportSent}

) : ( <>

{labels.reportIntro}

{reasons.map((r) => ( ))}