diff --git a/src/components/AIProviderConfigDialog.tsx b/src/components/AIProviderConfigDialog.tsx index c98d4d89..1809caf7 100644 --- a/src/components/AIProviderConfigDialog.tsx +++ b/src/components/AIProviderConfigDialog.tsx @@ -1,4 +1,4 @@ -import { useState, useEffect, useCallback } from 'react'; +import { useState, useEffect, useCallback, useRef } from 'react'; import { useQuery, useMutation, useQueryClient } from '@tanstack/react-query'; import QRCode from 'qrcode'; import { Trash2, Bot, CreditCard, Zap, ExternalLink, RefreshCw, Check, X, Clock, AlertCircle, Copy, RotateCcw, ArrowLeft, Gift, Plus, History, DollarSign, Printer, Download } from 'lucide-react'; @@ -352,10 +352,13 @@ export function AIProviderConfigDialog({ // Credits state const [amount, setAmount] = useState(10); + const [debouncedAmount, setDebouncedAmount] = useState(10); const [paymentMethod, setPaymentMethod] = useState<'stripe' | 'lightning'>('stripe'); const [lightningInvoice, setLightningInvoice] = useState(null); const [lightningPaymentId, setLightningPaymentId] = useState(null); + const [lightningTotal, setLightningTotal] = useState(10); const [refreshingPayments, setRefreshingPayments] = useState>(new Set()); + const amountDebounceTimer = useRef(null); const [giftcardAmount, setGiftcardAmount] = useState(10); const [giftcardQuantity, setGiftcardQuantity] = useState(1); const [redeemCode, setRedeemCode] = useState(''); @@ -383,13 +386,30 @@ export function AIProviderConfigDialog({ } }, [open, supportsCredits, isLoadingCredits]); + // Debounce amount changes to avoid excessive API calls + useEffect(() => { + if (amountDebounceTimer.current) { + clearTimeout(amountDebounceTimer.current); + } + + amountDebounceTimer.current = setTimeout(() => { + setDebouncedAmount(amount); + }, 500); // Wait 500ms after user stops typing + + return () => { + if (amountDebounceTimer.current) { + clearTimeout(amountDebounceTimer.current); + } + }; + }, [amount]); + // Query for payment preview (to get fee information) const { data: paymentPreview, isLoading: isLoadingPreview } = useQuery({ - queryKey: ['ai-payment-preview', provider.nostr ? user?.pubkey ?? '' : '', provider.id, amount, paymentMethod], + queryKey: ['ai-payment-preview', provider.nostr ? user?.pubkey ?? '' : '', provider.id, debouncedAmount, paymentMethod], queryFn: async (): Promise => { const ai = createAIClient(provider, user, config.corsProxy); const request: AddCreditsRequest = { - amount, + amount: debouncedAmount, method: paymentMethod, }; @@ -401,7 +421,7 @@ export function AIProviderConfigDialog({ body: request, }) as Payment; }, - enabled: open && supportsCredits && amount > 0, + enabled: open && supportsCredits && debouncedAmount > 0, retry: false, refetchOnWindowFocus: false, staleTime: Infinity, @@ -452,6 +472,7 @@ export function AIProviderConfigDialog({ } else if (payment.method === 'lightning') { setLightningInvoice(payment.url); setLightningPaymentId(payment.id); + setLightningTotal(payment.total); } queryClient.invalidateQueries({ @@ -632,6 +653,7 @@ export function AIProviderConfigDialog({ } else if (paymentPreview.method === 'lightning') { setLightningInvoice(paymentPreview.url); setLightningPaymentId(paymentPreview.id); + setLightningTotal(paymentPreview.total); } queryClient.invalidateQueries({ @@ -1094,8 +1116,8 @@ export function AIProviderConfigDialog({ id="amount" type="number" step="0.01" - value={amount} - onChange={(e) => setAmount(Number(e.target.value))} + value={amount || ''} + onChange={(e) => setAmount(Number(e.target.value) || 0)} placeholder={t('enterAmount')} className="text-center text-lg font-medium" /> @@ -1238,6 +1260,7 @@ export function AIProviderConfigDialog({ if (payment.method === 'lightning') { setLightningInvoice(payment.url); setLightningPaymentId(payment.id); + setLightningTotal(payment.total); } else { window.open(payment.url, '_blank'); } @@ -1512,7 +1535,7 @@ export function AIProviderConfigDialog({
{ diff --git a/src/components/AddProviderDialog.tsx b/src/components/AddProviderDialog.tsx index 6838cb98..381e7ed9 100644 --- a/src/components/AddProviderDialog.tsx +++ b/src/components/AddProviderDialog.tsx @@ -22,22 +22,7 @@ import { Link } from 'react-router-dom'; import { useCurrentUser } from '@/hooks/useCurrentUser'; import { useAppContext } from '@/hooks/useAppContext'; 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; -} +import type { PresetDeployProvider } from '@/lib/deploy/types'; interface OAuthHook { isOAuthConfigured: boolean; @@ -49,7 +34,7 @@ interface OAuthHook { interface AddProviderDialogProps { open: boolean; onOpenChange: (open: boolean) => void; - preset: PresetProvider; + preset: PresetDeployProvider; isLoggedIntoNostr: boolean; oauthHook: OAuthHook | null; forceManualEntry: boolean; @@ -57,27 +42,6 @@ interface AddProviderDialogProps { 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, @@ -136,22 +100,16 @@ export function AddProviderDialog({ } }; - const url = getProviderUrl(preset); - return (
- {url ? ( - } - /> - ) : ( - - )} + } + /> {preset.name}
@@ -188,15 +146,11 @@ export function AddProviderDialog({ ) : ( <> - {url ? ( - } - /> - ) : ( - - )} + } + /> Connect to {preset.name} diff --git a/src/components/ExternalFavicon.tsx b/src/components/ExternalFavicon.tsx index 38868387..b58e0456 100644 --- a/src/components/ExternalFavicon.tsx +++ b/src/components/ExternalFavicon.tsx @@ -5,7 +5,7 @@ import { ReactNode, useMemo } from 'react'; interface ExternalFaviconProps { /** The URL to fetch the favicon for */ - url: string | URL; + url: string | URL | undefined; /** Size of the favicon in pixels */ size?: number; /** Fallback element to display if favicon fails to load */ @@ -28,9 +28,15 @@ export function ExternalFavicon({ // Generate the favicon URL using the configured template const faviconSrc = useMemo(() => { + if (!url) return; try { + const parsedUrl = new URL(url); + + // Strip `ai.` and `api.` subdomains for a better chance at finding a favicon + parsedUrl.hostname = parsedUrl.hostname.replace(/^(ai\.|api\.)/, ''); + // Normalize the URL to ensure it has a protocol - return faviconUrl(config.faviconUrl, url); + return faviconUrl(config.faviconUrl, parsedUrl); } catch { return; } diff --git a/src/components/ProviderConfigDialog.tsx b/src/components/ProviderConfigDialog.tsx index 19867745..727ea25e 100644 --- a/src/components/ProviderConfigDialog.tsx +++ b/src/components/ProviderConfigDialog.tsx @@ -15,29 +15,14 @@ import { DialogFooter, } from '@/components/ui/dialog'; import type { DeployProvider } from '@/contexts/DeploySettingsContext'; +import type { PresetDeployProvider } from '@/lib/deploy/types'; import { ExternalFavicon } from '@/components/ExternalFavicon'; -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 ProviderConfigDialogProps { open: boolean; onOpenChange: (open: boolean) => void; provider: DeployProvider; - preset?: PresetProvider; + preset?: PresetDeployProvider; onUpdate: (provider: DeployProvider) => void; onRemove: () => void; } @@ -57,56 +42,6 @@ function normalizeUrl(url: string): string { return `https://${url}`; } -function getProviderUrl(provider: DeployProvider | PresetProvider): string | null { - switch (provider.type) { - case 'shakespeare': - // For Shakespeare providers, check for custom host first, then use default - if ('host' in provider && provider.host) { - return normalizeUrl(provider.host); - } - return 'https://shakespeare.diy'; - - case 'netlify': - // For Netlify providers, check for custom baseURL first, then use default - if ('baseURL' in provider && provider.baseURL) { - return provider.baseURL; - } - return 'https://netlify.com'; - - case 'vercel': - // For Vercel providers, check for custom baseURL first, then use default - if ('baseURL' in provider && provider.baseURL) { - return provider.baseURL; - } - return 'https://vercel.com'; - - case 'cloudflare': - // For Cloudflare providers, check for custom baseURL first, then use default - if ('baseURL' in provider && provider.baseURL) { - return provider.baseURL; - } - return 'https://cloudflare.com'; - - case 'deno': - // For Deno Deploy providers, check for custom baseURL first, then use default - if ('baseURL' in provider && provider.baseURL) { - return provider.baseURL; - } - return 'https://deno.com'; - - case 'nsite': - // nsite uses Rocket icon fallback - return null; - - case 'apkbuilder': - // Use Android's favicon - return 'https://android.com'; - - default: - return null; - } -} - export function ProviderConfigDialog({ open, onOpenChange, @@ -135,22 +70,24 @@ export function ProviderConfigDialog({ onOpenChange(false); }; - const url = getProviderUrl(localProvider); + // Use baseURL from configured provider, falling back to preset baseURL + const baseURL = ('baseURL' in localProvider && localProvider.baseURL) || preset?.baseURL; + + // For Shakespeare, special handling for host field + const shakespeareUrl = localProvider.type === 'shakespeare' && 'host' in localProvider && localProvider.host + ? normalizeUrl(localProvider.host) + : undefined; return (
- {url ? ( - } - /> - ) : ( - - )} + } + /> {localProvider.name}
diff --git a/src/components/deploy/DeploySteps.tsx b/src/components/deploy/DeploySteps.tsx index df9429c7..c7e92ea1 100644 --- a/src/components/deploy/DeploySteps.tsx +++ b/src/components/deploy/DeploySteps.tsx @@ -15,7 +15,8 @@ import { useAppContext } from '@/hooks/useAppContext'; import { useNostr } from '@nostrify/react'; import { Link } from 'react-router-dom'; import type { DeployProvider } from '@/contexts/DeploySettingsContext'; -import type { APKBuildType } from '@/lib/deploy/types'; +import type { APKBuildType, PresetDeployProvider } from '@/lib/deploy/types'; +import { PRESET_DEPLOY_PROVIDERS } from '@/lib/deployProviderPresets'; import { ShakespeareDeployForm } from '@/components/deploy/ShakespeareDeployForm'; import { NetlifyDeployForm } from '@/components/deploy/NetlifyDeployForm'; import { VercelDeployForm } from '@/components/deploy/VercelDeployForm'; @@ -35,57 +36,28 @@ import { DenoDeployAdapter } from '@/lib/deploy/DenoDeployAdapter'; import { APKBuilderAdapter } from '@/lib/deploy/APKBuilderAdapter'; /** - * Helper function to get provider URL for favicon + * Normalize a URL string to ensure it has a protocol */ -function getProviderUrl(provider: DeployProvider): string | null { - switch (provider.type) { - case 'shakespeare': - if (provider.host) { - return `https://${provider.host}`; - } - return 'https://shakespeare.diy'; - - case 'netlify': - if (provider.baseURL) { - return provider.baseURL; - } - return 'https://netlify.com'; - - case 'vercel': - if (provider.baseURL) { - return provider.baseURL; - } - return 'https://vercel.com'; - - case 'cloudflare': - if (provider.baseURL) { - return provider.baseURL; - } - return 'https://cloudflare.com'; - - case 'deno': - if (provider.baseURL) { - return provider.baseURL; - } - return 'https://deno.com'; - - case 'nsite': - return null; - - case 'apkbuilder': - // Use Android's favicon - return 'https://android.com'; - - default: - return null; +function normalizeUrl(url: string): string { + if (/^https?:\/\//i.test(url)) { + return url; } + return `https://${url}`; } /** * Render provider icon using favicon or fallback */ -function renderProviderIcon(provider: DeployProvider, size = 14) { - const url = getProviderUrl(provider); +function renderProviderIcon(provider: DeployProvider, preset: PresetDeployProvider | undefined, size = 14) { + // Use baseURL from configured provider, falling back to preset baseURL + const baseURL = ('baseURL' in provider && provider.baseURL) || preset?.baseURL; + + // For Shakespeare, special handling for host field + const shakespeareUrl = provider.type === 'shakespeare' && 'host' in provider && provider.host + ? normalizeUrl(provider.host) + : undefined; + + const url = shakespeareUrl || baseURL; if (url) { return ( @@ -776,23 +748,26 @@ export function DeploySteps({ projectId, projectName, onClose }: DeployStepsProp
- {settings.providers.map((provider) => ( - - ))} + {settings.providers.map((provider) => { + const preset = PRESET_DEPLOY_PROVIDERS.find(p => p.type === provider.type); + return ( + + ); + })}
diff --git a/src/lib/deploy/types.ts b/src/lib/deploy/types.ts index 1aa27bee..f9d49707 100644 --- a/src/lib/deploy/types.ts +++ b/src/lib/deploy/types.ts @@ -115,3 +115,20 @@ export interface APKBuilderDeployConfig { logs?: string[]; }) => void; } + +export interface PresetDeployProvider { + id: string; + type: 'shakespeare' | 'netlify' | 'vercel' | 'nsite' | 'cloudflare' | 'deno' | 'apkbuilder'; + name: string; + description: string; + baseURL?: string; + requiresNostr?: boolean; + apiKeyLabel?: string; + apiKeyURL?: string; + accountIdLabel?: string; + accountIdURL?: string; + organizationIdLabel?: string; + organizationIdURL?: string; + buildServerUrlLabel?: string; + proxy?: boolean; +} diff --git a/src/lib/deployProviderPresets.ts b/src/lib/deployProviderPresets.ts new file mode 100644 index 00000000..e8290463 --- /dev/null +++ b/src/lib/deployProviderPresets.ts @@ -0,0 +1,73 @@ +import type { PresetDeployProvider } from './deploy/types'; + +export const PRESET_DEPLOY_PROVIDERS: PresetDeployProvider[] = [ + { + id: 'shakespeare', + type: 'shakespeare', + name: 'Shakespeare Deploy', + description: 'Deploy to Shakespeare hosting with Nostr authentication', + baseURL: 'https://shakespeare.diy', + requiresNostr: true, + }, + { + id: 'nsite', + type: 'nsite', + name: 'nsite', + description: 'Deploy to Nostr as a static website', + // No baseURL for nsite - uses Rocket icon fallback + }, + { + id: 'netlify', + type: 'netlify', + name: 'Netlify', + description: 'Deploy to Netlify', + baseURL: 'https://api.netlify.com/api/v1', + apiKeyLabel: 'Personal Access Token', + apiKeyURL: 'https://app.netlify.com/user/applications#personal-access-tokens', + proxy: true, + }, + { + id: 'vercel', + type: 'vercel', + name: 'Vercel', + description: 'Deploy to Vercel', + baseURL: 'https://api.vercel.com', + apiKeyLabel: 'Access Token', + apiKeyURL: 'https://vercel.com/account/tokens', + proxy: true, + }, + { + id: 'cloudflare', + type: 'cloudflare', + name: 'Cloudflare', + description: 'Deploy to Cloudflare Workers', + baseURL: 'https://api.cloudflare.com/client/v4', + apiKeyLabel: 'API Token', + apiKeyURL: 'https://dash.cloudflare.com/profile/api-tokens', + accountIdLabel: 'Account ID', + accountIdURL: 'https://developers.cloudflare.com/fundamentals/account/find-account-and-zone-ids/', + proxy: true, + }, + { + id: 'deno', + type: 'deno', + name: 'Deno Deploy', + description: 'Deploy to Deno Deploy', + baseURL: 'https://api.deno.com/v1', + apiKeyLabel: 'Access Token', + apiKeyURL: 'https://dash.deno.com/account#access-tokens', + organizationIdLabel: 'Organization ID', + organizationIdURL: 'https://dash.deno.com/orgs', + proxy: true, + }, + { + id: 'apkbuilder', + type: 'apkbuilder', + name: 'APK Builder', + description: 'Build Android APKs from your web app', + baseURL: 'https://android.com', + buildServerUrlLabel: 'Build Server URL', + apiKeyLabel: 'API Key', + proxy: true, + }, +]; diff --git a/src/pages/DeploySettings.tsx b/src/pages/DeploySettings.tsx index 7094b36c..67cec1e4 100644 --- a/src/pages/DeploySettings.tsx +++ b/src/pages/DeploySettings.tsx @@ -8,6 +8,8 @@ import { useNetlifyOAuth } from '@/hooks/useNetlifyOAuth'; import { useVercelOAuth } from '@/hooks/useVercelOAuth'; import { useCurrentUser } from '@/hooks/useCurrentUser'; import type { DeployProvider } from '@/contexts/DeploySettingsContext'; +import type { PresetDeployProvider } from '@/lib/deploy/types'; +import { PRESET_DEPLOY_PROVIDERS } from '@/lib/deployProviderPresets'; import { ExternalFavicon } from '@/components/ExternalFavicon'; import { ProviderConfigDialog } from '@/components/ProviderConfigDialog'; import { AddProviderDialog } from '@/components/AddProviderDialog'; @@ -30,31 +32,17 @@ import { } from '@dnd-kit/sortable'; import { CSS } from '@dnd-kit/utilities'; -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 ProviderTileProps { provider: DeployProvider; - preset?: PresetProvider; + preset?: PresetDeployProvider; onClick: () => void; id: string; } -function ProviderTile({ provider, onClick, id }: ProviderTileProps) { - const url = getProviderUrl(provider); +function ProviderTile({ provider, preset, onClick, id }: ProviderTileProps) { + // Use baseURL from configured provider, falling back to preset baseURL + const baseURL = ('baseURL' in provider && provider.baseURL) || preset?.baseURL; + const { attributes, listeners, @@ -76,15 +64,11 @@ function ProviderTile({ provider, onClick, id }: ProviderTileProps) { onClick={onClick} className="flex flex-col items-center justify-center gap-2 p-4 rounded-lg border bg-card hover:bg-muted transition-colors text-center h-[120px] w-full" > - {url ? ( - } - /> - ) : ( - - )} + } + /> {provider.name} @@ -120,27 +104,21 @@ function AddProviderTile({ onClick }: AddProviderTileProps) { } interface PresetProviderTileProps { - preset: PresetProvider; + preset: PresetDeployProvider; onClick: () => void; } function PresetProviderTile({ preset, onClick }: PresetProviderTileProps) { - const url = getProviderUrl(preset); - return (