import { useState, useRef, useEffect } from 'react'; import { useTranslation } from 'react-i18next'; import { SettingDefinition } from '../../api/adminSettings'; import { CheckIcon, CloseIcon, EditIcon } from './icons'; interface SettingInputProps { setting: SettingDefinition; onUpdate: (value: string) => void; disabled?: boolean; } // Check if value is likely JSON or multi-line function isLongValue(value: string | null | undefined): boolean { if (!value) return false; const str = String(value); return str.length > 50 || str.includes('\n') || str.startsWith('[') || str.startsWith('{'); } // Check if key suggests it's a list or JSON config function isListOrJsonKey(key: string): boolean { const lowerKey = key.toLowerCase(); return ( lowerKey.includes('_items') || lowerKey.includes('_config') || lowerKey.includes('_keywords') || lowerKey.includes('_list') || lowerKey.includes('_json') || lowerKey.includes('_template') || lowerKey.includes('_periods') || lowerKey.includes('_discounts') || lowerKey.includes('_packages') ); } export function SettingInput({ setting, onUpdate, disabled }: SettingInputProps) { const { t } = useTranslation(); const [isEditing, setIsEditing] = useState(false); const [value, setValue] = useState(''); const textareaRef = useRef(null); const inputRef = useRef(null); const currentValue = String(setting.current ?? ''); const needsTextarea = isLongValue(currentValue) || isListOrJsonKey(setting.key); // Auto-resize textarea useEffect(() => { if (textareaRef.current && isEditing) { textareaRef.current.style.height = 'auto'; textareaRef.current.style.height = Math.min(textareaRef.current.scrollHeight, 300) + 'px'; } }, [value, isEditing]); const handleStart = () => { setValue(currentValue); setIsEditing(true); }; const handleSave = () => { onUpdate(value); setIsEditing(false); }; const handleCancel = () => { setIsEditing(false); setValue(''); }; // Dropdown for choices if (setting.choices && setting.choices.length > 0) { return ( ); } // Editing mode - Textarea for long values if (isEditing && needsTextarea) { return (