'use client' import { useState, useEffect } from 'react' import { Card, CardContent, CardDescription, CardHeader, CardTitle } from "@/components/ui/card" import { Badge } from "@/components/ui/badge" import { Button } from "@/components/ui/button" import { Input } from "@/components/ui/input" import { Textarea } from "@/components/ui/textarea" import { Label } from "@/components/ui/label" import { Loader2, Plus, ArrowLeft, Check } from "lucide-react" import { useToast } from '@/hooks/use-toast' import type { TemplateRegister } from '../project/[...id]/types' interface ExploreTemplatesProps { onProjectCreated?: (project: any) => void } type Step = 'select-template' | 'enter-details' | 'creating' export default function ExploreTemplates({ onProjectCreated }: ExploreTemplatesProps) { const [templateData, setTemplateData] = useState([]) const [isLoadingTemplates, setIsLoadingTemplates] = useState(false) const [templatesLoaded, setTemplatesLoaded] = useState(false) const [currentStep, setCurrentStep] = useState('select-template') const [selectedTemplate, setSelectedTemplate] = useState(null) const [projectName, setProjectName] = useState('') const [projectDescription, setProjectDescription] = useState('') const [isCreatingProject, setIsCreatingProject] = useState(false) const { toast } = useToast() // Fetch template data useEffect(() => { if (templatesLoaded) return const fetchTemplateData = async () => { setIsLoadingTemplates(true) try { const response = await fetch('/api/template/list') if (response.ok) { const data = await response.json() setTemplateData(data) setTemplatesLoaded(true) } else { console.error('Failed to fetch template data:', response.statusText) } } catch (error) { console.error('Error fetching template data:', error) } finally { setIsLoadingTemplates(false) } } fetchTemplateData() }, [templatesLoaded]) const handleTemplateSelect = (template: TemplateRegister) => { setSelectedTemplate(template) // Pre-fill with template-based suggestions setProjectName(`${template.name} Project`) setProjectDescription(template.description) setCurrentStep('enter-details') } const handleBackToTemplates = () => { setCurrentStep('select-template') setSelectedTemplate(null) setProjectName('') setProjectDescription('') } const handleCreateProject = async () => { if (!selectedTemplate || !projectName.trim() || !projectDescription.trim()) { toast({ title: "Error", description: "Please fill in both project name and description.", variant: "destructive", duration: 3000, }) return } setIsCreatingProject(true) setCurrentStep('creating') try { const projectData = { project_name: projectName.trim(), project_description: projectDescription.trim(), template_name: selectedTemplate.name, template_description: selectedTemplate.description } const response = await fetch('/api/projects/create', { method: 'POST', headers: { 'Content-Type': 'application/json', }, body: JSON.stringify(projectData) }) if (!response.ok) { const errorData = await response.json() throw new Error(errorData.error || 'Failed to create project') } const newProject = await response.json() toast({ title: "Project created successfully!", description: `Project "${newProject.name}" has been created using the ${selectedTemplate.name} template.`, duration: 5000, }) // Reset to initial state setCurrentStep('select-template') setSelectedTemplate(null) setProjectName('') setProjectDescription('') // Notify parent component about the new project if (onProjectCreated) { onProjectCreated(newProject) } } catch (error) { const message = error instanceof Error ? error.message : 'Unknown error occurred' toast({ title: "Error", description: `There was an error creating your project: ${message}`, variant: "destructive", duration: 5000, }) // Go back to details step on error setCurrentStep('enter-details') } finally { setIsCreatingProject(false) } } const renderStepIndicator = () => (
{/* Step 1: Select Template */}
{selectedTemplate && currentStep !== 'select-template' ? : '1'}
Select Template
{/* Arrow */}
{/* Step 2: Enter Details */}
{currentStep === 'creating' && projectName && projectDescription ? : '2'}
Enter Details
{/* Arrow */}
{/* Step 3: Create Project */}
3
Create Project
) const renderTemplateSelection = () => (

Choose a Template

Select a template to get started with your project

{isLoadingTemplates && (
Loading templates...
)}
{!isLoadingTemplates && templateData.length === 0 ? (

No templates available

) : (
{templateData.map((template, index) => ( handleTemplateSelect(template)} >
{template.name} v{template.starfish_version}
by {template.author}

{template.description}

{template.dependencies.length > 0 && (

Dependencies:

{template.dependencies.slice(0, 3).map((dep, depIndex) => ( {dep} ))} {template.dependencies.length > 3 && ( +{template.dependencies.length - 3} more )}
)} {template.input_example && (

Example Input:

{typeof template.input_example === 'object' ? JSON.stringify(template.input_example) : template.input_example}
)}
))}
)}
) const renderProjectDetails = () => (

Project Details

Configure your project name and description

{/* Selected Template Info */} {selectedTemplate && (
Selected Template: {selectedTemplate.name}
{selectedTemplate.description}
by {selectedTemplate.author} v{selectedTemplate.starfish_version}
)} {/* Project Details Form */} Project Information Enter the name and description for your new project
setProjectName(e.target.value)} placeholder="Enter your project name" className="w-full" />