"use client"; import { useState, useEffect, useRef } from "react"; import { Switch } from "@/app/_components/ui/switch"; import { Separator } from "@/app/_components/ui/separator"; import { ScrollArea } from "@/app/_components/ui/scroll-area"; import { type NotificationPreferences, defaultNotificationPreferences, getNotificationPreferences, saveNotificationPreferences, applyNotificationPreferences, } from "@/utils/notification-cookies-manager"; import { toast } from "sonner"; export default function NotificationsSetting() { const contentRef = useRef(null); const [isLoading, setIsLoading] = useState(true); const [lastToastTime, setLastToastTime] = useState(0); const [notificationPreferences, setNotificationPreferences] = useState(null); const [isScrollable, setIsScrollable] = useState(false); const scrollAreaHeight = "calc(100vh - 140px)"; useEffect(() => { const checkScrollable = () => { if (contentRef.current) { const contentHeight = contentRef.current.scrollHeight; const viewportHeight = window.innerHeight - 140; setIsScrollable(contentHeight > viewportHeight); } }; // Check initially checkScrollable(); // Check on window resize window.addEventListener("resize", checkScrollable); // Clean up event listener return () => window.removeEventListener("resize", checkScrollable); }, []); // Show toast with debounce to prevent too many notifications const showSavedToast = () => { // const now = Date.now(); // if (now - lastToastTime > 2000) { // toast("Preferences saved"); // setLastToastTime(now); // } }; // Load saved preferences on component mount useEffect(() => { try { const savedPreferences = getNotificationPreferences(); setNotificationPreferences(savedPreferences); } catch (error) { console.error("Error loading notification preferences:", error); setNotificationPreferences(defaultNotificationPreferences); } finally { setIsLoading(false); } }, []); // Jika masih loading atau notificationPreferences masih null, tampilkan loading if (isLoading || !notificationPreferences) { return (
Loading...
); } // Gunakan nilai default jika notificationPreferences belum ada const { mobilePushNotifications, emailNotifications, announcementNotifications, } = notificationPreferences || defaultNotificationPreferences; // Generic handler untuk update state dan menyimpan preferensi const updatePreference = ( key: keyof NotificationPreferences, value: boolean ) => { const newPreferences = { ...notificationPreferences, [key]: value }; setNotificationPreferences(newPreferences); saveNotificationPreferences(newPreferences); applyNotificationPreferences(newPreferences); showSavedToast(); }; return (

Notifications

{/* Mobile push notification Section */}

Mobile push notifications

Receive push notifications on your mobile device when you're offline.

updatePreference("mobilePushNotifications", checked) } />
{/* Email notifications Section */}

Email Notifications

Always send email notifications

Receive emails about activity in your workspace, even when you're active on the app.

updatePreference("emailNotifications", checked) } />

Announcements and Update Emails

Receive occasional emails about product launches and new features from Notion.

updatePreference("announcementNotifications", checked) } />
); }