import { useState } from 'react'; import { useTranslation } from 'react-i18next'; import { Bot, ChevronDown } from 'lucide-react'; import { Button } from '@/components/ui/button'; import { Checkbox } from '@/components/ui/checkbox'; import { ExternalInput } from '@/components/ui/external-input'; import { Dialog, DialogContent, DialogDescription, DialogHeader, DialogTitle, DialogFooter, } from '@/components/ui/dialog'; import { DropdownMenu, DropdownMenuContent, DropdownMenuItem, DropdownMenuTrigger, } from '@/components/ui/dropdown-menu'; import { ExternalFavicon } from '@/components/ExternalFavicon'; import { Link } from 'react-router-dom'; import type { PresetProvider } from '@/lib/aiProviderPresets'; interface OAuthHook { isOAuthConfigured: boolean; isLoading: boolean; error: string | null; initiateOAuth: () => void; } interface AddAIProviderDialogProps { open: boolean; onOpenChange: (open: boolean) => void; preset: PresetProvider; isLoggedIntoNostr: boolean; oauthHook?: OAuthHook | null; forceManualEntry?: boolean; onSetForceManualEntry?: (force: boolean) => void; onAdd: (apiKey: string) => void; } export function AddAIProviderDialog({ open, onOpenChange, preset, isLoggedIntoNostr, oauthHook, forceManualEntry = false, onSetForceManualEntry, onAdd, }: AddAIProviderDialogProps) { const { t } = useTranslation(); const [apiKey, setApiKey] = useState(''); const [termsAgreed, setTermsAgreed] = useState(false); const showNostrLoginRequired = preset.nostr && !isLoggedIntoNostr; const isOAuthConfigured = oauthHook?.isOAuthConfigured ?? false; const isOAuthLoading = oauthHook?.isLoading ?? false; const oauthError = oauthHook?.error ?? null; const handleAdd = () => { onAdd(apiKey); setApiKey(''); onOpenChange(false); }; // Reset state when dialog closes const handleOpenChange = (open: boolean) => { if (!open) { setApiKey(''); setTermsAgreed(false); } onOpenChange(open); }; return (
} /> {preset.name}
Add {preset.name} as your AI provider
{showNostrLoginRequired ? (

{t('loginToNostrRequired')}

) : isOAuthConfigured && !forceManualEntry ? ( // Show OAuth button if OAuth is configured and not forced to manual
onSetForceManualEntry?.(true)}> Enter API key
{oauthError && (

{oauthError}

)}
) : (
{!preset.nostr && preset.apiKeysURL && ( setApiKey(e.target.value)} onKeyDown={(e) => { if (e.key === 'Enter' && apiKey.trim() && termsAgreed) { handleAdd(); } }} url={preset.apiKeysURL} urlTitle="Get Key" /> )} {/* Terms of Service Agreement */}
setTermsAgreed(checked === true)} className="size-3 [&_svg]:size-3" />
)}
{!showNostrLoginRequired && !(isOAuthConfigured && !forceManualEntry) && ( )}
); }