From 56f6d16cd19d7d7dc4a6d6b7151c30d801dcdb5d Mon Sep 17 00:00:00 2001 From: 230419 <230419@epvc.pt> Date: Wed, 27 May 2026 12:39:32 +0100 Subject: [PATCH] Update daily notification logic: reset lastNotifiedDate on time change and add debug logs --- src/App.jsx | 48 ++++++++++++++++++++++++++++++++++++++++-------- 1 file changed, 40 insertions(+), 8 deletions(-) diff --git a/src/App.jsx b/src/App.jsx index aceef3a..aa52d5e 100644 --- a/src/App.jsx +++ b/src/App.jsx @@ -673,37 +673,67 @@ export default function App() { useEffect(() => { if (!user || !dailyOutfitNotifEnabled || !dailyOutfitTime) return; + console.log('[Daily Outfit] Notification system initialized. Scheduled for:', dailyOutfitTime); + const intervalId = setInterval(() => { const now = new Date(); const todayStr = `${now.getFullYear()}-${String(now.getMonth()+1).padStart(2,'0')}-${String(now.getDate()).padStart(2,'0')}`; + console.log(`[Daily Outfit] Checking... Time now: ${now.getHours()}:${now.getMinutes()}, Scheduled: ${dailyOutfitTime}. Last notified: ${lastNotifiedDate}`); + if (lastNotifiedDate !== todayStr) { const currentMins = now.getHours() * 60 + now.getMinutes(); const [targetH, targetM] = dailyOutfitTime.split(':').map(Number); const targetMins = targetH * 60 + targetM; if (currentMins >= targetMins) { + console.log('[Daily Outfit] Time matched! Checking for outfits today...'); const todaysLooks = getLooksForDayGlobal(todayStr); + if (todaysLooks.length > 0) { const mainLook = todaysLooks[0]; - if ('Notification' in window && Notification.permission === 'granted') { - navigator.serviceWorker?.getRegistration().then(reg => { + console.log(`[Daily Outfit] Found outfit: ${mainLook.name}. Attempting to send notification...`); + + if ('Notification' in window) { + if (Notification.permission === 'granted') { const title = 'MyCloset - Outfit Diário'; const options = { body: `O seu outfit planeado "${mainLook.name}" está pronto para hoje!`, icon: '/favicon.ico' }; - if (reg) { - reg.showNotification(title, options); - } else { + + try { + navigator.serviceWorker?.getRegistration().then(reg => { + if (reg && reg.showNotification) { + console.log('[Daily Outfit] Using Service Worker to show notification.'); + reg.showNotification(title, options); + } else { + console.log('[Daily Outfit] Using standard Notification API.'); + new Notification(title, options); + } + }).catch(err => { + console.error('[Daily Outfit] Service Worker registration failed, using standard API.', err); + new Notification(title, options); + }); + } catch (e) { + console.error('[Daily Outfit] Fallback notification error:', e); new Notification(title, options); } - }); - setLastNotifiedDate(todayStr); - saveUserSetting('lastNotifiedDate', todayStr); + + setLastNotifiedDate(todayStr); + saveUserSetting('lastNotifiedDate', todayStr); + } else { + console.log('[Daily Outfit] Permission not granted. Current state:', Notification.permission); + } + } else { + console.log('[Daily Outfit] Notifications API not supported in this browser.'); } + } else { + console.log('[Daily Outfit] No outfits planned for today.'); } } + } else { + console.log('[Daily Outfit] Already notified today.'); } }, 30000); // Verifica a cada 30 segundos @@ -2659,6 +2689,8 @@ export default function App() { onChange={(e) => { setDailyOutfitTime(e.target.value); saveUserSetting('dailyOutfitTime', e.target.value); + setLastNotifiedDate(''); + saveUserSetting('lastNotifiedDate', ''); }} className={`px-3 py-1.5 rounded-xl text-sm outline-none border font-bold ${darkMode ? 'bg-gray-800 border-gray-700 text-white' : 'bg-white border-gray-200 text-gray-900'}`} />