// Reaction and share functionality (function() { // Generate or retrieve visitor ID function getVisitorId() { let visitorId = localStorage.getItem('visitorId'); if (!visitorId) { visitorId = 'visitor_' + Date.now() + '_' + Math.random().toString(36).substr(2, 9); localStorage.setItem('visitorId', visitorId); } return visitorId; } // Load reactions for a post async function loadReactions(postId) { try { const response = await fetch(`/api/reaction.php?postId=${postId}`); const data = await response.json(); if (data.success) { updateReactionButtons(postId, data.counts); } } catch (error) { console.error('Error loading reactions:', error); } } // Send reaction async function sendReaction(postId, reaction) { const visitorId = getVisitorId(); try { const response = await fetch('/api/reaction.php', { method: 'POST', headers: { 'Content-Type': 'application/json', }, body: JSON.stringify({ postId: postId, reaction: reaction, visitorId: visitorId }) }); if (!response.ok) { console.error('API error:', response.status, response.statusText); return; } const data = await response.json(); console.log('Reaction response:', data); // Debug log if (data.success && data.counts) { updateReactionButtons(postId, data.counts); } else { console.error('Invalid response:', data); } } catch (error) { console.error('Error sending reaction:', error); } } // Update reaction button displays function updateReactionButtons(postId, counts) { const container = document.querySelector(`#post-${postId} .reactions-buttons`); if (!container) return; ['like', 'love', 'helpful'].forEach(reaction => { const button = container.querySelector(`.reaction-btn[data-reaction="${reaction}"]`); if (button) { const count = counts[reaction] || 0; const countSpan = button.querySelector('.reaction-count'); if (countSpan) { countSpan.textContent = count; } } }); } // Track share event async function trackShare(postId, platform) { const visitorId = getVisitorId(); try { await fetch('/api/track.php', { method: 'POST', headers: { 'Content-Type': 'application/json', }, body: JSON.stringify({ type: 'share', page: `/blog/post.html?id=${postId}`, visitorId: visitorId, platform: platform }) }); } catch (error) { console.error('Error tracking share:', error); } } // Inline SVG icons - using currentColor for theme support const iconSVGs = { like: '', love: '', helpful: '', mastodon: '', bluesky: '', copy: '' }; // Create reaction buttons HTML function createReactionButtons(postId) { return `