'use client' import { useState } from 'react' import { Switch } from '@/components/ui/switch' import { Label } from '@/components/ui/label' import { Loader2, Check, X } from 'lucide-react' import { cn } from '@/lib/utils' import { toast } from 'sonner' interface SettingToggleProps { label: string description?: string checked: boolean onChange: (checked: boolean) => Promise disabled?: boolean } export function SettingToggle({ label, description, checked, onChange, disabled }: SettingToggleProps) { const [isLoading, setIsLoading] = useState(false) const [error, setError] = useState(false) const handleChange = async (newChecked: boolean) => { setIsLoading(true) setError(false) try { await onChange(newChecked) toast.success('Setting saved', { description: `${label} has been ${newChecked ? 'enabled' : 'disabled'}` }) } catch (err) { console.error('Error updating setting:', err) setError(true) toast.error('Failed to save setting', { description: 'Please try again' }) } finally { setIsLoading(false) } } return (
{description && (

{description}

)}
{isLoading && } {!isLoading && !error && checked && } {!isLoading && !error && !checked && }
) }