From ce6a37aaffd459f3da5e9332328a6271bea59e9e Mon Sep 17 00:00:00 2001 From: ItzCrazyKns <95534749+ItzCrazyKns@users.noreply.github.com> Date: Thu, 30 Oct 2025 11:28:15 +0530 Subject: [PATCH] feat(settingsFields): add switch field --- src/components/Settings/SettingsField.tsx | 84 +++++++++++++++++++++++ 1 file changed, 84 insertions(+) diff --git a/src/components/Settings/SettingsField.tsx b/src/components/Settings/SettingsField.tsx index 8b2fc41..2d8099b 100644 --- a/src/components/Settings/SettingsField.tsx +++ b/src/components/Settings/SettingsField.tsx @@ -1,6 +1,7 @@ import { SelectUIConfigField, StringUIConfigField, + SwitchUIConfigField, TextareaUIConfigField, UIConfigField, } from '@/lib/config/types'; @@ -9,6 +10,7 @@ import Select from '../ui/Select'; import { toast } from 'sonner'; import { useTheme } from 'next-themes'; import { Loader2 } from 'lucide-react'; +import { Switch } from '@headlessui/react'; const SettingsSelect = ({ field, @@ -237,6 +239,79 @@ const SettingsTextarea = ({ ); }; +const SettingsSwitch = ({ + field, + value, + setValue, + dataAdd, +}: { + field: SwitchUIConfigField; + value?: any; + setValue: (value: any) => void; + dataAdd: string; +}) => { + const [loading, setLoading] = useState(false); + + const handleSave = async (newValue: boolean) => { + setLoading(true); + setValue(newValue); + try { + if (field.scope === 'client') { + localStorage.setItem(field.key, String(newValue)); + } else { + const res = await fetch('/api/config', { + method: 'POST', + headers: { + 'Content-Type': 'application/json', + }, + body: JSON.stringify({ + key: `${dataAdd}.${field.key}`, + value: newValue, + }), + }); + + if (!res.ok) { + console.error('Failed to save config:', await res.text()); + throw new Error('Failed to save configuration'); + } + } + } catch (error) { + console.error('Error saving config:', error); + toast.error('Failed to save configuration.'); + } finally { + setTimeout(() => setLoading(false), 150); + } + }; + + const isChecked = value === true || value === 'true'; + + return ( +
+
+
+

+ {field.name} +

+

+ {field.description} +

+
+ + +
+
+ ); +}; + const SettingsField = ({ field, value, @@ -276,6 +351,15 @@ const SettingsField = ({ dataAdd={dataAdd} /> ); + case 'switch': + return ( + + ); default: return
Unsupported field type: {field.type}
; }