import { IconCheck, IconKey, IconX } from '@tabler/icons-react'; import { FC, KeyboardEvent, useEffect, useRef, useState } from 'react'; import { useTranslation } from 'next-i18next'; import { SidebarButton } from '../Sidebar/SidebarButton'; interface Props { apiKey: string; onApiKeyChange: (apiKey: string) => void; } export const Key: FC = ({ apiKey, onApiKeyChange }) => { const { t } = useTranslation('sidebar'); const [isChanging, setIsChanging] = useState(false); const [newKey, setNewKey] = useState(apiKey); const inputRef = useRef(null); const handleEnterDown = (e: KeyboardEvent) => { if (e.key === 'Enter') { e.preventDefault(); handleUpdateKey(newKey); } }; const handleUpdateKey = (newKey: string) => { onApiKeyChange(newKey.trim()); setIsChanging(false); }; useEffect(() => { if (isChanging) { inputRef.current?.focus(); } }, [isChanging]); return isChanging ? (
setNewKey(e.target.value)} onKeyDown={handleEnterDown} placeholder={t('API Base Url') || 'Base Url'} />
{ e.stopPropagation(); handleUpdateKey(newKey); }} /> { e.stopPropagation(); setIsChanging(false); setNewKey(apiKey); }} />
) : ( } onClick={() => setIsChanging(true)} /> ); };