"use client"; import { v4 as uuidv4 } from "uuid"; import { Button } from "@/components/ui/button"; import { Dialog, DialogContent, DialogDescription, DialogHeader, DialogTrigger, DialogTitle, DialogFooter, } from "@/components/ui/dialog"; import { Card, CardContent, CardDescription, CardHeader, CardTitle, } from "@/components/ui/card"; import { Loader2, PencilRuler, Plus, Trash2 } from "lucide-react"; import Link from "next/link"; import { useEffect, useState } from "react"; import { Feature, Task } from "@/types"; import { ScrollArea } from "./ui/scroll-area"; import FeatureCard from "./feature-card"; import { useRouter } from "next/navigation"; export function GenerateProject({ workspaceID, projectID, project_name, project_description, tech_stack, }: { workspaceID: string; projectID: string; project_name: string; project_description: string; tech_stack: string[]; }) { const [features, setFeatures] = useState([]); const [tasks, setTasks] = useState([]); const [isDialogOpen, setDialogOpen] = useState(false); const [loading, setLoading] = useState(false); const [isAddingFeature, setIsAddingFeature] = useState(false); const [addName, setAddName] = useState(""); const [addDescription, setAddDescription] = useState(""); const [page, setPage] = useState(0); const router = useRouter(); async function generateFeatures() { setLoading(true); const res = await fetch(`/w/${workspaceID}/p/${projectID}/features/gen`, { method: "POST", headers: { "Content-Type": "application/json", }, body: JSON.stringify({ project_name, project_description, tech_stack, }), }); setLoading(false); setPage(1); const data = await res.json(); console.log("CLIENT RAW RES ===>", data.features); setFeatures(data.features); } async function generateTasks(features: Feature[]) { console.log("GENERATING TASKS ===>", features); let _tasks: Task[] = []; features.forEach(async (feature, i) => { const res = await fetch(`/w/${workspaceID}/p/${projectID}/tasks/gen`, { method: "POST", headers: { "Content-Type": "application/json", }, body: JSON.stringify({ project_name, project_description, tech_stack, related_features: [], feature, }), }); const data = await res.json(); console.log("TASK RAW RES ===>", data.tasks); _tasks.push(...data.tasks); console.log("CURTASKS ===>", _tasks); if (i === features.length - 1) { console.log("TASKS ===>", _tasks); setTasks(_tasks); addTasks(_tasks); setPage(3); } }); } async function addTasks(tasks: Task[] = []) { console.log("ADDING TASKS ===>", tasks); const res = await fetch(`/w/${workspaceID}/p/${projectID}/tasks/add`, { method: "POST", headers: { "Content-Type": "application/json", }, body: JSON.stringify(tasks), }); const data = await res.json(); console.log("ADDTASKS RAW RES ===>", data.tasks); } async function addFeatures() { const res = await fetch(`/w/${workspaceID}/p/${projectID}/features/add`, { method: "POST", headers: { "Content-Type": "application/json", }, body: JSON.stringify(features), }); const data = await res.json(); console.log("addFeatures RAW RES ===>", data.features); setFeatures(data.features); setPage(2); return data.features; } useEffect(() => { async function getFeatures() { const res = await fetch(`/w/${workspaceID}/p/${projectID}/features`, { method: "POST", headers: { "Content-Type": "application/json", }, }); const data = await res.json(); if (data.features.length === 0) setDialogOpen(true); console.log("CLIENT RAW RES ===>", data.features); return data.features; } getFeatures().then((features) => setFeatures(features)); }, [projectID, workspaceID]); return ( <> {page == 0 && ( Welcome to {project_name}. Now it's time to generate some features for your new project. {!loading ? ( ) : ( )} )} {page == 1 && ( Here's what we've generated for you. Feel free to edit, delete, or add new features.
{features.map((feature) => ( ))} {!isAddingFeature ? ( setIsAddingFeature(true)} >

Create a new feature

) : ( { setAddName(e.target.value); }} />