import { useState } from 'react'; import { useTranslation } from 'react-i18next'; import { Rocket, ChevronDown, Key, Loader2 } from 'lucide-react'; import { Button } from '@/components/ui/button'; 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 { useCurrentUser } from '@/hooks/useCurrentUser'; import { requestBuildServerApiKey } from '@/lib/nip98'; interface PresetProvider { id: string; type: 'shakespeare' | 'netlify' | 'vercel' | 'nsite' | 'cloudflare' | 'deno' | 'apkbuilder'; name: string; description: string; requiresNostr?: boolean; apiKeyLabel?: string; apiKeyURL?: string; accountIdLabel?: string; accountIdURL?: string; organizationIdLabel?: string; organizationIdURL?: string; buildServerUrlLabel?: string; proxy?: boolean; } interface OAuthHook { isOAuthConfigured: boolean; isLoading: boolean; error: string | null; initiateOAuth: () => void; } interface AddProviderDialogProps { open: boolean; onOpenChange: (open: boolean) => void; preset: PresetProvider; isLoggedIntoNostr: boolean; oauthHook: OAuthHook | null; forceManualEntry: boolean; onSetForceManualEntry: (force: boolean) => void; onAdd: (apiKey: string, accountId?: string, organizationId?: string, buildServerUrl?: string) => void; } function getProviderUrl(preset: PresetProvider): string | null { switch (preset.type) { case 'shakespeare': return 'https://shakespeare.diy'; case 'netlify': return 'https://netlify.com'; case 'vercel': return 'https://vercel.com'; case 'cloudflare': return 'https://cloudflare.com'; case 'deno': return 'https://deno.com'; case 'nsite': return null; case 'apkbuilder': return 'https://android.com'; default: return null; } } export function AddProviderDialog({ open, onOpenChange, preset, isLoggedIntoNostr, oauthHook, forceManualEntry, onSetForceManualEntry, onAdd, }: AddProviderDialogProps) { const { t } = useTranslation(); const { user } = useCurrentUser(); const [apiKey, setApiKey] = useState(''); const [accountId, setAccountId] = useState(''); const [organizationId, setOrganizationId] = useState(''); const [buildServerUrl, setBuildServerUrl] = useState(''); const [isGettingApiKey, setIsGettingApiKey] = useState(false); const [apiKeyError, setApiKeyError] = useState(null); const showNostrLoginRequired = preset.requiresNostr && !isLoggedIntoNostr; const isOAuthConfigured = oauthHook?.isOAuthConfigured ?? false; const isOAuthLoading = oauthHook?.isLoading ?? false; const oauthError = oauthHook?.error ?? null; const handleAdd = () => { onAdd(apiKey, accountId, organizationId, buildServerUrl); setApiKey(''); setAccountId(''); setOrganizationId(''); setBuildServerUrl(''); setApiKeyError(null); onOpenChange(false); }; const handleGetApiKey = async () => { if (!buildServerUrl.trim() || !user?.signer) { setApiKeyError('Build server URL and Nostr login required'); return; } setIsGettingApiKey(true); setApiKeyError(null); try { const result = await requestBuildServerApiKey( user.signer, buildServerUrl.trim(), preset.proxy ? 'https://corsproxy.io/?' : undefined ); setApiKey(result.apiKey); } catch (error) { setApiKeyError(error instanceof Error ? error.message : 'Failed to get API key'); } finally { setIsGettingApiKey(false); } }; const url = getProviderUrl(preset); return (
{url ? ( } /> ) : ( )} {preset.name}
{preset.description}
{showNostrLoginRequired ? (

{t('loginToNostrRequired')}

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

{oauthError}

)}
) : ( // Show manual token input
{!preset.requiresNostr && preset.type !== 'nsite' && ( <> {preset.type === 'cloudflare' && ( setAccountId(e.target.value)} onKeyDown={(e) => { if (e.key === 'Enter' && apiKey.trim() && accountId.trim()) { handleAdd(); } }} url={preset.accountIdURL} urlTitle="Find ID" /> )} {preset.type === 'deno' && ( setOrganizationId(e.target.value)} onKeyDown={(e) => { if (e.key === 'Enter' && apiKey.trim() && organizationId.trim()) { handleAdd(); } }} url={preset.organizationIdURL} urlTitle="Find ID" /> )} {preset.type === 'apkbuilder' && ( <> { setBuildServerUrl(e.target.value); setApiKeyError(null); }} onKeyDown={(e) => { if (e.key === 'Enter' && apiKey.trim() && buildServerUrl.trim()) { handleAdd(); } }} />
setApiKey(e.target.value)} onKeyDown={(e) => { if (e.key === 'Enter' && apiKey.trim() && buildServerUrl.trim()) { handleAdd(); } }} className="flex-1" />
{apiKeyError && (

{apiKeyError}

)} {!user?.signer && buildServerUrl.trim() && (

Login to Nostr to get an API key automatically

)} )} {preset.type !== 'apkbuilder' && ( setApiKey(e.target.value)} onKeyDown={(e) => { if (e.key === 'Enter' && apiKey.trim() && (preset.type !== 'cloudflare' || accountId.trim()) && (preset.type !== 'deno' || organizationId.trim())) { handleAdd(); } }} url={preset.apiKeyURL} urlTitle="Get Key" /> )} )}
)}
{!showNostrLoginRequired && !(isOAuthConfigured && !forceManualEntry) && ( )}
); }