import { SelectUIConfigField, StringUIConfigField, TextareaUIConfigField, UIConfigField, } from '@/lib/config/types'; import { useState } from 'react'; import Select from '../ui/Select'; import { toast } from 'sonner'; import { useTheme } from 'next-themes'; import { Loader2 } from 'lucide-react'; const SettingsSelect = ({ field, value, setValue, dataAdd, }: { field: SelectUIConfigField; value?: any; setValue: (value: any) => void; dataAdd: string; }) => { const [loading, setLoading] = useState(false); const { setTheme } = useTheme(); const handleSave = async (newValue: any) => { setLoading(true); setValue(newValue); try { if (field.scope === 'client') { localStorage.setItem(field.key, newValue); if (field.key === 'theme') { setTheme(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); } }; return (

{field.name}

{field.description}

setValue(event.target.value)} onBlur={(event) => handleSave(event.target.value)} className="w-full rounded-lg border border-light-200 dark:border-dark-200 bg-light-primary dark:bg-dark-primary px-3 py-2 lg:px-4 lg:py-3 pr-10 !text-xs lg:!text-sm text-black/80 dark:text-white/80 placeholder:text-black/40 dark:placeholder:text-white/40 focus-visible:outline-none focus-visible:border-light-300 dark:focus-visible:border-dark-300 transition-colors disabled:cursor-not-allowed disabled:opacity-60" placeholder={field.placeholder} type="text" disabled={loading} /> {loading && ( )}
); }; const SettingsTextarea = ({ field, value, setValue, dataAdd, }: { field: TextareaUIConfigField; value?: any; setValue: (value: any) => void; dataAdd: string; }) => { const [loading, setLoading] = useState(false); const handleSave = async (newValue: any) => { setLoading(true); setValue(newValue); try { if (field.scope === 'client') { localStorage.setItem(field.key, 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); } }; return (

{field.name}

{field.description}