'use client'; import React, { useEffect, useState } from 'react'; import { Deployment } from '@/lib/vfs/types'; import { Server, Loader2 } from 'lucide-react'; import { Select, SelectContent, SelectItem, SelectTrigger, SelectValue, } from '@/components/ui/select'; import { cn } from '@/lib/utils'; interface DeploymentSelectorProps { projectId: string; selectedDeploymentId: string | null; onDeploymentChange: (deploymentId: string | null, deploymentName: string | null) => void; className?: string; workspaceId?: string; } export function DeploymentSelector({ projectId, selectedDeploymentId, onDeploymentChange, className, workspaceId, }: DeploymentSelectorProps) { const apiBase = workspaceId ? `/api/w/${workspaceId}` : '/api'; const [deployments, setDeployments] = useState([]); const [loading, setLoading] = useState(true); const [error, setError] = useState(null); // Fetch deployments for this project useEffect(() => { const fetchDeployments = async () => { // Only fetch in server mode if (process.env.NEXT_PUBLIC_SERVER_MODE !== 'true') { setLoading(false); return; } try { setLoading(true); setError(null); const response = await fetch(`${apiBase}/projects/${projectId}/deployments`); if (!response.ok) { throw new Error('Failed to fetch deployments'); } const data = await response.json(); setDeployments(data.deployments || []); // Auto-select first deployment if only one exists and nothing is selected if (data.deployments?.length === 1 && !selectedDeploymentId) { const deployment = data.deployments[0]; if (deployment.databaseEnabled) { onDeploymentChange(deployment.id, deployment.name); } } } catch { // Expected in browser mode or when project hasn't been synced to server yet // Don't show error — deployments are optional } finally { setLoading(false); } }; fetchDeployments(); }, [projectId]); // Only refetch when project changes // Don't render in browser mode if (process.env.NEXT_PUBLIC_SERVER_MODE !== 'true') { return null; } // Don't render if no deployments with database enabled const databaseEnabledDeployments = deployments.filter(s => s.databaseEnabled); if (!loading && databaseEnabledDeployments.length === 0) { return null; } if (loading) { return (
Loading deployments...
); } if (error) { return (
{error}
); } return (
); }