import { useState, useEffect } from 'react'; import { Dialog, DialogContent, DialogHeader, DialogTitle, DialogFooter, } from '@/components/ui/dialog'; import { Button } from '@/components/ui/button'; import { Input } from '@/components/ui/input'; import { Label } from '@/components/ui/label'; import { useToast } from '@/hooks/useToast'; import { useAppContext } from '@/hooks/useAppContext'; import { useCurrentUser } from '@/hooks/useCurrentUser'; import { useMutation } from '@tanstack/react-query'; import { deployProject } from '@/lib/deploy'; import { useFS } from '@/hooks/useFS'; import { CloudUpload, Loader2, } from 'lucide-react'; interface DeployDialogProps { projectId: string; projectName: string; open: boolean; onOpenChange: (open: boolean) => void; onFirstInteraction?: () => void; } export function DeployDialog({ projectId, projectName, open, onOpenChange, onFirstInteraction }: DeployDialogProps) { const { config } = useAppContext(); const { user } = useCurrentUser(); const { fs } = useFS(); const { toast } = useToast(); // State for the deploy URL input const [deployUrl, setDeployUrl] = useState(''); // Initialize deploy URL when dialog opens useEffect(() => { if (open) { setDeployUrl(`${projectId}.${config.deployServer}`); } }, [open, projectId, config.deployServer]); const deployMutation = useMutation({ mutationFn: async () => { if (!user) { throw new Error("You must be logged into Nostr to deploy a project"); } // Parse the deploy URL to extract hostname and server const hostname = deployUrl.trim(); const parts = hostname.split('.'); if (parts.length < 2) { throw new Error("Invalid deploy URL format. Please use format: subdomain.domain.com"); } // Extract the deploy server (everything after the first dot) const deployServer = parts.slice(1).join('.'); return await deployProject({ fs, projectId, // Keep the original projectId for file path deployServer, projectPath: `/projects/${projectId}`, signer: user.signer, customHostname: hostname, // Pass the custom hostname }); }, onSuccess: (result) => { toast({ title: "Deployment successful!", description: `Your project is now live at ${result.hostname}`, }); // Open the deployed site in a new tab window.open(result.url, '_blank'); // Close the dialog onOpenChange(false); }, onError: (error) => { toast({ title: "Deployment failed", description: error instanceof Error ? error.message : "An unexpected error occurred", variant: "destructive", }); }, }); const handleDeploy = () => { if (onFirstInteraction) { onFirstInteraction(); } deployMutation.mutate(); }; const handleUrlChange = (value: string) => { // Remove protocol if user enters it const cleanValue = value.replace(/^https?:\/\//, ''); setDeployUrl(cleanValue); }; const isValidUrl = () => { const trimmed = deployUrl.trim(); return trimmed.length > 0 && trimmed.includes('.') && !trimmed.startsWith('.') && !trimmed.endsWith('.'); }; return ( Deploy "{projectName}"
https:// handleUrlChange(e.target.value)} placeholder={`${projectId}.${config.deployServer}`} className="pl-16" disabled={deployMutation.isPending} />

Your project will be deployed to this URL. You can customize the subdomain.

{!user && (

You must be logged into Nostr to deploy projects.

)}
); }