'use client'; import { useState, useEffect } from 'react'; import { useRouter } from 'next/navigation'; import { Dialog, DialogContent, DialogDescription, DialogHeader, DialogTitle, } from '@/components/ui/dialog'; import { Button } from '@/components/ui/button'; import { Input } from '@/components/ui/input'; import { Label } from '@/components/ui/label'; import { Github, Loader2, CheckCircle2, Copy, ExternalLink, ChevronLeft } from 'lucide-react'; import { toast } from 'sonner'; import { Card, CardContent } from '@/components/ui/card'; import { Separator } from '@/components/ui/separator'; interface ProjectCreationModalProps { open: boolean; onOpenChange: (open: boolean) => void; initialWorkspacePath?: string; workspace: string; } export function ProjectCreationModal({ open, onOpenChange, initialWorkspacePath, workspace, }: ProjectCreationModalProps) { const router = useRouter(); const [step, setStep] = useState(1); const [productName, setProductName] = useState(''); const [selectedRepo, setSelectedRepo] = useState(null); const [createdProjectId, setCreatedProjectId] = useState(null); const [loading, setLoading] = useState(false); const [githubConnected, setGithubConnected] = useState(false); const [githubRepos, setGithubRepos] = useState([]); const [loadingGithub, setLoadingGithub] = useState(false); useEffect(() => { async function checkGitHub() { if (!open || step !== 2) return; setLoadingGithub(true); try { const statusResponse = await fetch('/api/github/connect'); if (statusResponse.ok) { const statusData = await statusResponse.json(); const isConnected = statusData.connected || false; setGithubConnected(isConnected); if (isConnected) { const reposResponse = await fetch('/api/github/repos'); if (reposResponse.ok) { const repos = await reposResponse.json(); setGithubRepos(Array.isArray(repos) ? repos : []); } } } else { setGithubConnected(false); } } catch (error) { console.error('GitHub check error:', error); setGithubConnected(false); } finally { setLoadingGithub(false); } } checkGitHub(); }, [open, step]); useEffect(() => { if (!open) setTimeout(resetModal, 200); }, [open]); const resetModal = () => { setStep(1); setProductName(''); setSelectedRepo(null); setCreatedProjectId(null); setLoading(false); }; const handleCreateProject = async () => { if (!productName.trim()) { toast.error('Product name is required'); return; } setLoading(true); try { const projectData = { projectName: productName.trim(), projectType: selectedRepo ? 'existing' : 'scratch', slug: productName.toLowerCase().replace(/[^a-z0-9]+/g, '-'), product: { name: productName }, ...(selectedRepo && { githubRepo: selectedRepo.full_name, githubRepoId: selectedRepo.id, githubRepoUrl: selectedRepo.html_url, githubDefaultBranch: selectedRepo.default_branch, }), }; const response = await fetch('/api/projects/create', { method: 'POST', headers: { 'Content-Type': 'application/json' }, body: JSON.stringify(projectData), }); if (response.ok) { const data = await response.json(); setCreatedProjectId(data.projectId); toast.success('Project created!'); setStep(3); } else { const error = await response.json(); toast.error(error.error || 'Failed to create project'); } } catch (error) { console.error('Error creating project:', error); toast.error('An error occurred'); } finally { setLoading(false); } }; const copyToClipboard = (text: string) => { navigator.clipboard.writeText(text); toast.success('Copied to clipboard!'); }; const handleFinish = () => { onOpenChange(false); if (createdProjectId) { router.push(`/${workspace}/project/${createdProjectId}/v_ai_chat`); } }; return ( {step === 1 && 'Create New Project'} {step === 2 && 'Connect GitHub Repository'} {step === 3 && 'Setup Complete!'} {step === 1 && 'Give your project a name to get started.'} {step === 2 && 'Select a GitHub repository to connect (optional).'} {step === 3 && 'Add the .vibn file to your project to enable tracking.'}
{step === 1 && (
setProductName(e.target.value)} onKeyDown={(e) => { if (e.key === 'Enter' && productName.trim()) { setStep(2); } }} autoFocus />

Connect GitHub or skip to continue

)} {step === 2 && (
{loadingGithub ? (

Checking GitHub connection...

) : ( <>
{githubRepos.length > 0 ? ( githubRepos.map((repo) => ( )) ) : !githubConnected ? (

GitHub not connected yet

) : (

No repositories found

)}
)}
)} {step === 3 && createdProjectId && (

Project created successfully!

Project ID: {createdProjectId}

Create a .vibn file in your project root:

{`{
  "projectId": "${createdProjectId}",
  "version": "1.0.0"
}`}
                    
{selectedRepo && ( )}
)}
); }