"use client"; import { useEffect, useState } from "react"; import { BellIcon, BellOffIcon, AlertCircleIcon } from "lucide-react"; import { Button } from "@/components/ui/button"; import { getPermission, isOptedIn, notificationSupport, requestPermission, setOptedIn, showNotification, type NotificationPermission, } from "@/lib/notifications"; /** * Settings-page card that lets the operator opt into browser * notifications for "reminder fired" and "send-test sent" events. * * Three states the UI surfaces: * * 1. Not supported (server / older browser) — shows a one-line * muted note. No action. * 2. Permission not granted — Enable button asks the browser. * If the user blocks the prompt we fall back to a * "blocked in browser" note pointing them at site settings. * 3. Granted + opted in — toggle to disable, plus a "Test" * button that fires a sample notification so the operator * knows the wiring works end-to-end. */ export function NotificationsToggle() { const [supported, setSupported] = useState(false); const [permission, setPermission] = useState("default"); const [optedIn, setOptedInState] = useState(false); const [busy, setBusy] = useState(false); // Hydrate from the live browser state once on mount; the server // can't know any of these values. useEffect(() => { setSupported(notificationSupport() === "supported"); setPermission(getPermission()); setOptedInState(isOptedIn()); }, []); async function enable() { setBusy(true); const result = await requestPermission(); setPermission(result); if (result === "granted") { setOptedIn(true); setOptedInState(true); } setBusy(false); } function disable() { setOptedIn(false); setOptedInState(false); } function fireTest() { showNotification({ title: "Test notification", body: "Notifications are wired up. Reminder runs and send-tests will surface here.", tag: "settings-test", }); } if (!supported) { return (
Notifications aren't supported by this browser.
); } if (permission === "denied") { return (
Notifications are blocked at the browser level. Enable them in your site settings (lock icon next to the URL) and reload.
); } if (permission === "granted" && optedIn) { return (
On
); } return ( ); }