import React, { useState } from 'react' export default function ArtworkActions({ artwork, canonicalUrl, mobilePriority = false }) { const [liked, setLiked] = useState(Boolean(artwork?.viewer?.is_liked)) const [favorited, setFavorited] = useState(Boolean(artwork?.viewer?.is_favorited)) const [reporting, setReporting] = useState(false) const downloadUrl = artwork?.thumbs?.xl?.url || artwork?.thumbs?.lg?.url || artwork?.file?.url || '#' const shareUrl = canonicalUrl || artwork?.canonical_url || (typeof window !== 'undefined' ? window.location.href : '#') const csrfToken = typeof document !== 'undefined' ? document.querySelector('meta[name="csrf-token"]')?.getAttribute('content') : null const postInteraction = async (url, body) => { const response = await fetch(url, { method: 'POST', headers: { 'Content-Type': 'application/json', 'X-CSRF-TOKEN': csrfToken || '', }, credentials: 'same-origin', body: JSON.stringify(body), }) if (!response.ok) throw new Error('Request failed') return response.json() } const onToggleLike = async () => { const nextState = !liked setLiked(nextState) try { await postInteraction(`/api/artworks/${artwork.id}/like`, { state: nextState }) } catch { setLiked(!nextState) } } const onToggleFavorite = async () => { const nextState = !favorited setFavorited(nextState) try { await postInteraction(`/api/artworks/${artwork.id}/favorite`, { state: nextState }) } catch { setFavorited(!nextState) } } const onShare = async () => { try { if (navigator.share) { await navigator.share({ title: artwork?.title || 'Artwork', url: shareUrl, }) return } await navigator.clipboard.writeText(shareUrl) } catch { // noop } } const onReport = async () => { if (reporting) return setReporting(true) try { await postInteraction(`/api/artworks/${artwork.id}/report`, { reason: 'Reported from artwork page', }) } catch { // noop } finally { setReporting(false) } } return (
) }