"use client"; /** * LikeEmailToggle — settings control for aggregated "someone liked your post" * emails. Reads the current preference and lets the user opt out (push + * in-app like notifications are unaffected). Matches the other settings rows. */ import { useEffect, useState } from "react"; import { toast } from "sonner"; import { notifications } from "@/lib/social"; export function LikeEmailToggle() { const [enabled, setEnabled] = useState(null); const [busy, setBusy] = useState(false); useEffect(() => { let cancelled = false; notifications .emailPrefs() .then((r) => { if (!cancelled) setEnabled(r.like_emails); }) .catch(() => { if (!cancelled) setEnabled(true); // default-on; toggle still works }); return () => { cancelled = true; }; }, []); async function toggle() { if (enabled === null) return; const next = !enabled; setBusy(true); try { await notifications.setEmailPrefs(next); setEnabled(next); toast.success(next ? "Like emails turned on" : "Like emails turned off"); } catch { toast.error("Could not update preference"); } finally { setBusy(false); } } return ( ); }