import { useRef, useState } from "react"; import { useMutation, useQueryClient } from "@tanstack/react-query"; import { PROJECT_COLORS, type ProjectRepository } from "@paperclipai/shared"; import { X } from "lucide-react"; import { useDialog } from "../context/DialogContext"; import { useCompany } from "../context/CompanyContext"; import { projectsApi } from "../api/projects"; import { queryKeys } from "../lib/queryKeys"; import { trackRecentProject } from "../lib/recent-projects"; import { ProjectTile } from "./ProjectTile"; import { Dialog, DialogContent, DialogTitle } from "./ui/dialog"; import { Button } from "./ui/button"; import { ProjectRepositoryInput, repositoryOptionsKey } from "./ProjectRepositoryInput"; import { ConnectionSetupFlow } from "@/features/connections/ConnectionSetupFlow"; export function NewProjectDialog() { const { newProjectOpen, closeNewProject } = useDialog(); const { selectedCompanyId } = useCompany(); return selectedCompanyId && newProjectOpen ? : null; } export function NewProjectForm({ companyId, onClose }: { companyId: string; onClose: () => void }) { const client = useQueryClient(); const [name, setName] = useState(""); const [color] = useState(() => PROJECT_COLORS[Math.floor(Math.random() * PROJECT_COLORS.length)]!); const [repos, setRepos] = useState([]); const [connecting, setConnecting] = useState(false); const input = useRef(null); const create = useMutation({ mutationFn: () => projectsApi.create(companyId, { name: name.trim(), color, status: "planned", repositoryIds: repos.map((repo) => repo.id) }), onSuccess: (project) => { trackRecentProject(project.id, companyId); void client.invalidateQueries({ queryKey: queryKeys.projects.all(companyId) }); onClose(); }, }); return { if (!open && !create.isPending) onClose(); }}> { event.preventDefault(); input.current?.focus(); }}> {connecting ?
Connect GitHub setConnecting(false)} onComplete={() => { void client.invalidateQueries({ queryKey: repositoryOptionsKey(companyId) }); setConnecting(false); }} />
:
{ event.preventDefault(); if (name.trim() && !create.isPending) create.mutate(); }}>
Create project
setName(event.target.value)} placeholder="Project name" required className="h-10 w-full min-w-0 border-0 bg-transparent text-base outline-none placeholder:text-muted-foreground md:text-sm" />
setConnecting(true)} disabled={create.isPending} />
{create.isError &&

{create.error.message}

}
}
; }