import { useState, useEffect, useCallback } from 'react'; import { Plus, ExternalLink } from 'lucide-react'; import { Label } from '@/components/ui/label'; import { Input } from '@/components/ui/input'; import { Button } from '@/components/ui/button'; import { Select, SelectContent, SelectItem, SelectTrigger, SelectValue, } from '@/components/ui/select'; import { Alert, AlertDescription } from '@/components/ui/alert'; import { Skeleton } from '@/components/ui/skeleton'; import { proxyUrl } from '@/lib/proxyUrl'; interface NetlifySite { id: string; site_id: string; name: string; url: string; admin_url: string; } interface NetlifyDeployFormProps { apiKey: string; baseURL?: string; projectId: string; projectName?: string; savedSiteId?: string; onSiteChange: (siteId: string, siteName: string) => void; corsProxy?: string; } export function NetlifyDeployForm({ apiKey, baseURL = 'https://api.netlify.com/api/v1', projectId, projectName, savedSiteId, onSiteChange, corsProxy, }: NetlifyDeployFormProps) { const [sites, setSites] = useState([]); const [isLoading, setIsLoading] = useState(true); const [error, setError] = useState(null); const [selectedSiteId, setSelectedSiteId] = useState(''); const [newSiteName, setNewSiteName] = useState(projectName || projectId); const fetchSites = useCallback(async () => { setIsLoading(true); setError(null); try { const url = `${baseURL}/sites`; const targetUrl = corsProxy ? proxyUrl(corsProxy, url) : url; const response = await fetch(targetUrl, { headers: { 'Authorization': `Bearer ${apiKey}`, }, }); if (!response.ok) { throw new Error(`Failed to fetch sites: ${response.status} ${response.statusText}`); } const data: NetlifySite[] = await response.json(); setSites(data); // If there's a saved site ID and it exists in the list, select it // Otherwise leave it empty (showing placeholder) if (savedSiteId && data.some(s => s.id === savedSiteId)) { setSelectedSiteId(savedSiteId); } } catch (err) { setError(err instanceof Error ? err.message : 'Failed to fetch sites'); } finally { setIsLoading(false); } }, [apiKey, baseURL, savedSiteId, corsProxy]); useEffect(() => { fetchSites(); }, [fetchSites]); // Update newSiteName when switching to "new" if it's empty useEffect(() => { if (selectedSiteId === 'new' && !newSiteName) { setNewSiteName(projectName || projectId); } }, [selectedSiteId, newSiteName, projectName, projectId]); useEffect(() => { if (selectedSiteId === '') { // No selection yet - don't call onSiteChange return; } else if (selectedSiteId === 'new') { onSiteChange('', newSiteName); } else { const site = sites.find(s => s.id === selectedSiteId); if (site) { onSiteChange(site.id, site.name); } } }, [selectedSiteId, newSiteName, sites, onSiteChange]); if (isLoading) { return (
); } const selectedSite = sites.find(s => s.id === selectedSiteId); return (
{error && ( {error} )}
{selectedSiteId === 'new' ? (
setNewSiteName(e.target.value)} placeholder={projectName || projectId} />

Domain: {newSiteName || projectId}.netlify.app

A new site will be created when you deploy

) : selectedSite ? (

{selectedSite.name}

{selectedSite.url}

Deployment will update this existing site

) : null}
); }