import React, { useState, useEffect, useRef } from "react"; import { Lock, Coffee, Users, Mail, PanelRight, X } from "lucide-react"; import { toast } from "sonner"; import { Button } from "@/components/ui/button"; import { Switch } from "@/components/ui/switch"; interface ScheduleItem { time: string; activity: string; type: "focused" | "collaborative" | "rest" | "light"; icon: React.ReactNode; } export default function GlobalWorkNotifications() { const [currentTime, setCurrentTime] = useState(new Date()); const [lastNotifiedIndex, setLastNotifiedIndex] = useState(-1); const [showNotificationModal, setShowNotificationModal] = useState(false); const [notificationsEnabled, setNotificationsEnabled] = useState(() => { if (typeof window !== 'undefined') { const saved = localStorage.getItem('globalWorkNotificationsEnabled'); return saved !== null ? saved === 'true' : true; } return true; }); const [currentNotification, setCurrentNotification] = useState<{ title: string; description: string; type: "focused" | "collaborative" | "rest" | "light"; } | null>(null); const notificationAudioRef = useRef(null); // Initialize audio element useEffect(() => { const audio = new Audio('/notification.mp3'); audio.preload = 'auto'; audio.volume = 1.0; // Full volume notificationAudioRef.current = audio; return () => { if (notificationAudioRef.current) { notificationAudioRef.current.pause(); notificationAudioRef.current = null; } }; }, []); // Save notification preferences useEffect(() => { localStorage.setItem('globalWorkNotificationsEnabled', notificationsEnabled.toString()); }, [notificationsEnabled]); // Update current time every minute useEffect(() => { const timer = setInterval(() => { setCurrentTime(new Date()); }, 60000); return () => clearInterval(timer); }, []); // Daily schedule data const scheduleItems: ScheduleItem[] = [ { time: "9:30 AM – 10:30 AM", activity: "Team Collaboration / Planning", type: "light", icon: }, { time: "10:30 AM – 11:30 AM", activity: "Deep Work Session 1", type: "focused", icon: }, { time: "11:30 AM – 12:00 PM", activity: "Team Discussion / Quick Sync", type: "collaborative", icon: }, { time: "12:00 PM – 1:00 PM", activity: "Deep Work Session 2", type: "focused", icon: }, { time: "1:00 PM – 1:45 PM", activity: "Lunch Break", type: "rest", icon: }, { time: "1:45 PM – 2:30 PM", activity: "Team Collaboration / Emails", type: "light", icon: }, { time: "2:30 PM – 3:30 PM", activity: "Deep Work Session 3", type: "focused", icon: }, { time: "3:30 PM – 4:00 PM", activity: "Team Discussion / Admin Tasks", type: "collaborative", icon: }, { time: "4:00 PM – 4:15 PM", activity: "Tea Break", type: "rest", icon: }, { time: "4:15 PM – 4:30 PM", activity: "Flex / Light Catch-Up", type: "light", icon: }, { time: "4:30 PM – 5:30 PM", activity: "Deep Work Session 4", type: "focused", icon: }, { time: "5:30 PM – 6:30 PM", activity: "Team Support / Wrap-Up", type: "light", icon: }, ]; const playNotificationSound = () => { if (notificationAudioRef.current && notificationsEnabled) { try { notificationAudioRef.current.currentTime = 0; notificationAudioRef.current.play().catch(err => { console.log("Could not play notification sound:", err); }); } catch (error) { console.error("Error playing notification sound:", error); } } }; const showNotification = (item: ScheduleItem) => { if (!notificationsEnabled) return; playNotificationSound(); const title = `Time for ${item.activity}`; const description = `${item.time} - ${ item.type === "focused" ? "Focus on deep work" : item.type === "collaborative" ? "Connect with your team" : item.type === "rest" ? "Take a break to recharge" : "Handle lighter tasks" }`; toast.info(title, { description }); setCurrentNotification({ title, description, type: item.type }); setShowNotificationModal(true); document.body.classList.add('overflow-hidden'); }; // Check current activity and show notifications only when time changes useEffect(() => { const now = currentTime; const hour = now.getHours(); const minute = now.getMinutes(); const currentTimeInMinutes = hour * 60 + minute; const index = scheduleItems.findIndex((item) => { const [startTime, endTime] = item.time.split(" – "); const [startHour, startMinutePart] = startTime.split(":"); const startMinute = parseInt(startMinutePart); const startHourNum = parseInt(startHour) + (startTime.includes("PM") && parseInt(startHour) !== 12 ? 12 : 0); const startTimeInMinutes = startHourNum * 60 + startMinute; const [endHour, endMinutePart] = endTime.split(":"); const endMinute = parseInt(endMinutePart); const endHourNum = parseInt(endHour) + (endTime.includes("PM") && parseInt(endHour) !== 12 ? 12 : 0); const endTimeInMinutes = endHourNum * 60 + endMinute; return currentTimeInMinutes >= startTimeInMinutes && currentTimeInMinutes < endTimeInMinutes; }); // Only show notification if this is not the initial load and the activity has changed const isInitialLoad = lastNotifiedIndex === -1; if (index !== -1) { if (!isInitialLoad && index !== lastNotifiedIndex && notificationsEnabled) { console.log('Time-based notification triggered for:', scheduleItems[index].activity); showNotification(scheduleItems[index]); } setLastNotifiedIndex(index); } else if (isInitialLoad) { // If no current activity on initial load, set lastNotifiedIndex to avoid future false triggers setLastNotifiedIndex(-2); } }, [currentTime, scheduleItems, notificationsEnabled]); const getModalStyles = (type: "focused" | "collaborative" | "rest" | "light") => { switch (type) { case "focused": return "bg-purple-50 border-purple-200 text-purple-800"; case "collaborative": return "bg-blue-50 border-blue-200 text-blue-800"; case "rest": return "bg-red-50 border-red-200 text-red-800"; case "light": return "bg-green-50 border-green-200 text-green-800"; default: return ""; } }; const getModalIcon = (type: "focused" | "collaborative" | "rest" | "light") => { switch (type) { case "focused": return ; case "collaborative": return ; case "rest": return ; case "light": return ; default: return null; } }; const closeModal = () => { setShowNotificationModal(false); document.body.classList.remove('overflow-hidden'); }; useEffect(() => { const handleEscapeKey = (event: KeyboardEvent) => { if (event.key === 'Escape' && showNotificationModal) { closeModal(); } }; document.addEventListener('keydown', handleEscapeKey); return () => document.removeEventListener('keydown', handleEscapeKey); }, [showNotificationModal]); return ( <> {showNotificationModal && currentNotification && (
{getModalIcon(currentNotification.type)}

{currentNotification.title}

{currentNotification.description}

{currentNotification.type === "focused" ? (

🧠 Time to focus! Close distractions, silence notifications, and dedicate this time to deep, meaningful work.

) : currentNotification.type === "collaborative" ? (

👥 Connect with your team! This is a good time for meetings, discussions, and collaborative problem-solving.

) : currentNotification.type === "rest" ? (

🔋 Take a break! Step away from your desk, stretch, or grab a refreshment to recharge your energy.

) : (

📋 Handle lighter tasks! This is a good time for emails, planning, and administrative tasks.

)}
)} ); }