import { useEffect, useRef, useState } from 'react'; interface Option { value: string; label: string; } interface InlineEditSelectProps { value: string; options: Option[]; onSave: (value: string) => Promise; displayLabel?: string; className?: string; disabled?: boolean; } type Status = 'idle' | 'editing' | 'saving' | 'success' | 'error'; export default function InlineEditSelect({ value, options, onSave, displayLabel, className = '', disabled = false, }: InlineEditSelectProps) { const [status, setStatus] = useState('idle'); const selectRef = useRef(null); useEffect(() => { if (status === 'editing' && selectRef.current) { selectRef.current.focus(); } }, [status]); const startEditing = () => { if (disabled) return; setStatus('editing'); }; const handleChange = async (e: React.ChangeEvent) => { const newValue = e.target.value; if (newValue === value) { setStatus('idle'); return; } setStatus('saving'); try { await onSave(newValue); setStatus('success'); setTimeout(() => setStatus('idle'), 1500); } catch { setStatus('error'); setTimeout(() => setStatus('idle'), 1500); } }; const handleBlur = () => { if (status === 'editing') { setStatus('idle'); } }; if (status === 'editing' || status === 'saving') { return ( ); } return ( {displayLabel || value} {status === 'success' && OK} {status === 'error' && ERR} ); }