import { IconCheck, IconKey, IconX } from '@tabler/icons-react'; import { FC, KeyboardEvent, useState } from 'react'; import { useTranslation } from 'next-i18next'; import { SidebarButton } from './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 handleEnterDown = (e: KeyboardEvent) => { if (e.key === 'Enter') { e.preventDefault(); handleUpdateKey(newKey); } }; const handleUpdateKey = (newKey: string) => { onApiKeyChange(newKey.trim()); setIsChanging(false); }; return isChanging ? (
setNewKey(e.target.value)} onKeyDown={handleEnterDown} placeholder={t('API Key') || 'API Key'} />
{ e.stopPropagation(); handleUpdateKey(newKey); }} /> { e.stopPropagation(); setIsChanging(false); setNewKey(apiKey); }} />
) : ( } onClick={() => setIsChanging(true)} /> ); };