import { useState, useRef } from 'react' import { useTranslation } from '@/i18n/react-i18next-compat' import { Dialog, DialogTrigger, DialogContent, DialogTitle, DialogClose, DialogFooter, DialogHeader, } from '@/components/ui/dialog' import { Button } from '@/components/ui/button' import { Input } from '@/components/ui/input' interface AddProviderDialogProps { onCreateProvider: (name: string) => void children: React.ReactNode } export function AddProviderDialog({ onCreateProvider, children, }: AddProviderDialogProps) { const { t } = useTranslation() const [name, setName] = useState('') const [isOpen, setIsOpen] = useState(false) const createButtonRef = useRef(null) const handleCreate = () => { if (name.trim()) { onCreateProvider(name.trim()) setName('') setIsOpen(false) } } const handleCancel = () => { setName('') setIsOpen(false) } const handleKeyDown = (e: React.KeyboardEvent) => { if (e.key === 'Enter' && name.trim()) { e.preventDefault() handleCreate() } // Prevent key from being captured by parent components e.stopPropagation() } const handleOpenChange = (open: boolean) => { setIsOpen(open) if (!open) { setName('') } } return ( {children} { e.preventDefault() createButtonRef.current?.focus() }} > {t('provider:addOpenAIProvider')} setName(e.target.value)} className="mt-2" placeholder={t('provider:enterNameForProvider')} onKeyDown={handleKeyDown} /> ) }