"use client"; import { useState, useEffect, useCallback } from 'react'; import { useAuth } from '@/contexts/AuthContext'; import { Card, CardContent, CardDescription, CardHeader, CardTitle } from '@/components/ui/card'; import { Switch } from '@/components/ui/switch'; import { Label } from '@/components/ui/label'; import { Skeleton } from '@/components/ui/skeleton'; export function DndToggle() { const { token } = useAuth(); const [dndStatus, setDndStatus] = useState(false); const [loading, setLoading] = useState(true); const fetchDndStatus = useCallback(async () => { if (!token) return; setLoading(true); try { const res = await fetch(`${process.env.NEXT_PUBLIC_API_URL || 'http://localhost:3001'}/extensions/me/dnd`, { headers: { 'Authorization': `Bearer ${token}` } }); const data = await res.json(); setDndStatus(data.dndStatus); } catch (error) { console.error("Failed to fetch DND status", error); } finally { setLoading(false); } }, [token]); useEffect(() => { fetchDndStatus(); }, [fetchDndStatus]); const handleToggle = async (checked) => { if (!token) return; setDndStatus(checked); // Optimistic update try { await fetch(`${process.env.NEXT_PUBLIC_API_URL || 'http://localhost:3001'}/extensions/me/dnd`, { method: 'POST', headers: { 'Authorization': `Bearer ${token}`, 'Content-Type': 'application/json' }, body: JSON.stringify({ dndStatus: checked }) }); } catch (error) { console.error("Failed to update DND status", error); setDndStatus(!checked); // Revert on failure } }; return ( Do Not Disturb When enabled, calls will not ring your extension. {loading ? (
) : (
)}
); }