Phillip Hamilton • 7 days ago
Matt Schimkowitz • 5 years ago
Phillip Hamilton • 2 years ago
Phillip Hamilton • 11 months ago
Owen Carry • 2 months ago
> const CACHE_EXPIRY_MS = 15 * 60 * 1000; const fetchUserData = async (thumbableType = null) => { let cached = localStorage.getItem('user_data_cache'); let cacheData = null; let timestamp = 0; if (cached) { try { const parsed = JSON.parse(cached); cacheData = parsed.data; timestamp = parsed.timestamp; } catch { console.warn('Corrupt cached data, resetting...'); } } const isFresh = Date.now() - timestamp < CACHE_EXPIRY_MS; if (isFresh && cacheData && (!thumbableType || cacheData.interaction_data?.[thumbableType])) { return cacheData; } try { const query = thumbableType ? `?thumbable_type=${encodeURIComponent(thumbableType)}` : ''; const response = await fetch(`/user_data.json${query}`, { headers: { 'Accept': 'application/json' }, credentials: 'include' }); if (!response.ok) { throw new Error(`HTTP error! Status: ${response.status}`); } const newData = await response.json(); if (cacheData && newData.interaction_data) { cacheData = { ...newData, interaction_data: { ...cacheData.interaction_data, ...newData.interaction_data } }; } else { cacheData = newData; } localStorage.setItem('user_data_cache', JSON.stringify({ timestamp: Date.now(), data: cacheData })); return cacheData; } catch (error) { console.error('Error fetching user interactions:', error); return cacheData; } }; function updateUserInteractions() { const pageType = getPageType(); const contentType = getContentType(); const interactionData = ((window.userData || {}).interaction_data || {})[contentType] || {}; const likes = new Set(interactionData.likes); const dislikes = new Set(interactionData.dislikes); const favorites = new Set(interactionData.favorites); Array.from(document.querySelectorAll('.favorite')).forEach(favoriteElement => { const itemType = favoriteElement.dataset.itemType; const itemId = parseInt(favoriteElement.dataset.itemId); if (!itemType || !itemId) return; const itemFavorites = ((window.userData || {}).interaction_data || {})[itemType] || {}; const itemFavoritesSet = new Set(itemFavorites.favorites || []); if (itemFavoritesSet.has(itemId)) { favoriteElement.classList.add('on'); favoriteElement.classList.remove('off'); } else { favoriteElement.classList.add('off'); favoriteElement.classList.remove('on'); } }); Array.from(document.body.querySelectorAll('.user-interactions:not(.ready)')).forEach(userInteractionsElement => { if (!window.userData) { userInteractionsElement.style.display = 'none'; } userInteractions