import { useState, useCallback } from 'react' interface SettingChoice { value: string label: string description?: string } interface SettingDefinition { key: string name: string description?: string category: string type: string is_optional: boolean current: string | number | boolean | null original: string | number | boolean | null has_override: boolean read_only: boolean choices?: SettingChoice[] hint?: string } interface SettingCardProps { setting: SettingDefinition translatedName?: string translatedDescription?: string isFavorite: boolean onToggleFavorite: () => void onUpdate: (value: string) => void onReset: () => void disabled?: boolean } // Icons const StarIcon = ({ filled }: { filled: boolean }) => ( ) const RefreshIcon = () => ( ) const LockIcon = () => ( ) const CheckIcon = () => ( ) const CloseIcon = () => ( ) export function SettingCard({ setting, translatedName, translatedDescription, isFavorite, onToggleFavorite, onUpdate, onReset, disabled }: SettingCardProps) { const [isEditing, setIsEditing] = useState(false) const [editValue, setEditValue] = useState('') const displayName = translatedName || setting.name || setting.key const displayDescription = translatedDescription || setting.description const handleStartEdit = useCallback(() => { if (setting.read_only) return setEditValue(String(setting.current ?? '')) setIsEditing(true) }, [setting.current, setting.read_only]) const handleSave = useCallback(() => { onUpdate(editValue) setIsEditing(false) }, [editValue, onUpdate]) const handleCancel = useCallback(() => { setIsEditing(false) setEditValue('') }, []) const handleKeyDown = useCallback((e: React.KeyboardEvent) => { if (e.key === 'Enter') { handleSave() } else if (e.key === 'Escape') { handleCancel() } }, [handleSave, handleCancel]) const handleToggle = useCallback(() => { if (setting.read_only) return const newValue = setting.current === true || setting.current === 'true' ? 'false' : 'true' onUpdate(newValue) }, [setting.current, setting.read_only, onUpdate]) const handleSelectChange = useCallback((e: React.ChangeEvent) => { onUpdate(e.target.value) }, [onUpdate]) // Render input based on type const renderInput = () => { // Read-only display if (setting.read_only) { return (
{String(setting.current ?? '-')}
) } // Boolean toggle if (setting.type === 'bool') { const isChecked = setting.current === true || setting.current === 'true' return ( ) } // Select dropdown if (setting.choices && setting.choices.length > 0) { return ( ) } // Text/number input if (isEditing) { return (
setEditValue(e.target.value)} onKeyDown={handleKeyDown} autoFocus className="bg-dark-700 border border-accent-500 rounded-lg px-3 py-1.5 text-sm text-dark-100 focus:outline-none w-40" />
) } return ( ) } return (
{/* Left side - info */}
{displayName} {setting.has_override && ( Изменено )}
{displayDescription && (

{displayDescription}

)} {setting.hint && (

{setting.hint}

)}
{/* Right side - controls */}
{/* Favorite button */} {/* Input control */} {renderInput()} {/* Reset button */} {setting.has_override && !setting.read_only && ( )}
) }