import { useAppStore } from '@/store/app-store'; import { useSetupStore } from '@/store/setup-store'; import { Button } from '@/components/ui/button'; import { Key, CheckCircle2, Trash2 } from 'lucide-react'; import { Spinner } from '@/components/ui/spinner'; import { ApiKeyField } from './api-key-field'; import { buildProviderConfigs } from '@/config/api-providers'; import { SecurityNotice } from './security-notice'; import { useApiKeyManagement } from './hooks/use-api-key-management'; import { cn } from '@/lib/utils'; import { useState, useCallback } from 'react'; import { getElectronAPI } from '@/lib/electron'; import { toast } from 'sonner'; export function ApiKeysSection() { const { apiKeys, setApiKeys } = useAppStore(); const { claudeAuthStatus, setClaudeAuthStatus, codexAuthStatus, setCodexAuthStatus } = useSetupStore(); const [isDeletingAnthropicKey, setIsDeletingAnthropicKey] = useState(false); const [isDeletingOpenaiKey, setIsDeletingOpenaiKey] = useState(false); const { providerConfigParams, handleSave, saved } = useApiKeyManagement(); const providerConfigs = buildProviderConfigs(providerConfigParams); // Delete Anthropic API key const deleteAnthropicKey = useCallback(async () => { setIsDeletingAnthropicKey(true); try { const api = getElectronAPI(); if (!api.setup?.deleteApiKey) { toast.error('Delete API not available'); return; } const result = await api.setup.deleteApiKey('anthropic'); if (result.success) { setApiKeys({ ...apiKeys, anthropic: '' }); setClaudeAuthStatus({ authenticated: false, method: 'none', hasCredentialsFile: claudeAuthStatus?.hasCredentialsFile || false, }); toast.success('Anthropic API key deleted'); } else { toast.error(result.error || 'Failed to delete API key'); } } catch (error) { toast.error('Failed to delete API key'); } finally { setIsDeletingAnthropicKey(false); } }, [apiKeys, setApiKeys, claudeAuthStatus, setClaudeAuthStatus]); // Delete OpenAI API key const deleteOpenaiKey = useCallback(async () => { setIsDeletingOpenaiKey(true); try { const api = getElectronAPI(); if (!api.setup?.deleteApiKey) { toast.error('Delete API not available'); return; } const result = await api.setup.deleteApiKey('openai'); if (result.success) { setApiKeys({ ...apiKeys, openai: '' }); setCodexAuthStatus({ authenticated: false, method: 'none', }); toast.success('OpenAI API key deleted'); } else { toast.error(result.error || 'Failed to delete API key'); } } catch (error) { toast.error('Failed to delete API key'); } finally { setIsDeletingOpenaiKey(false); } }, [apiKeys, setApiKeys, setCodexAuthStatus]); return (
Configure your AI provider API keys. Keys are stored locally in your browser.
API Keys saved here can be used by API Profiles with "credentials" as the API key source. This lets you share a single key across multiple profile configurations without re-entering it.