import { useState } from "react"; import { useQuery, useMutation, useQueryClient } from "@tanstack/react-query"; import { useDialog } from "../context/DialogContext"; import { useCompany } from "../context/CompanyContext"; import { projectsApi } from "../api/projects"; import { goalsApi } from "../api/goals"; import { queryKeys } from "../lib/queryKeys"; import { Dialog, DialogContent, } from "@/components/ui/dialog"; import { Button } from "@/components/ui/button"; import { Popover, PopoverContent, PopoverTrigger, } from "@/components/ui/popover"; import { Maximize2, Minimize2, Target, Calendar, } from "lucide-react"; import { cn } from "../lib/utils"; import { StatusBadge } from "./StatusBadge"; import type { Goal } from "@paperclip/shared"; const projectStatuses = [ { value: "backlog", label: "Backlog" }, { value: "planned", label: "Planned" }, { value: "in_progress", label: "In Progress" }, { value: "completed", label: "Completed" }, { value: "cancelled", label: "Cancelled" }, ]; export function NewProjectDialog() { const { newProjectOpen, closeNewProject } = useDialog(); const { selectedCompanyId, selectedCompany } = useCompany(); const queryClient = useQueryClient(); const [name, setName] = useState(""); const [description, setDescription] = useState(""); const [status, setStatus] = useState("planned"); const [goalId, setGoalId] = useState(""); const [targetDate, setTargetDate] = useState(""); const [expanded, setExpanded] = useState(false); const [statusOpen, setStatusOpen] = useState(false); const [goalOpen, setGoalOpen] = useState(false); const { data: goals } = useQuery({ queryKey: queryKeys.goals.list(selectedCompanyId!), queryFn: () => goalsApi.list(selectedCompanyId!), enabled: !!selectedCompanyId && newProjectOpen, }); const createProject = useMutation({ mutationFn: (data: Record) => projectsApi.create(selectedCompanyId!, data), onSuccess: () => { queryClient.invalidateQueries({ queryKey: queryKeys.projects.list(selectedCompanyId!) }); reset(); closeNewProject(); }, }); function reset() { setName(""); setDescription(""); setStatus("planned"); setGoalId(""); setTargetDate(""); setExpanded(false); } function handleSubmit() { if (!selectedCompanyId || !name.trim()) return; createProject.mutate({ name: name.trim(), description: description.trim() || undefined, status, ...(goalId ? { goalId } : {}), ...(targetDate ? { targetDate } : {}), }); } function handleKeyDown(e: React.KeyboardEvent) { if (e.key === "Enter" && (e.metaKey || e.ctrlKey)) { e.preventDefault(); handleSubmit(); } } const currentGoal = (goals ?? []).find((g) => g.id === goalId); return ( { if (!open) { reset(); closeNewProject(); } }} > {/* Header */}
{selectedCompany && ( {selectedCompany.name.slice(0, 3).toUpperCase()} )} New project
{/* Name */}
setName(e.target.value)} autoFocus />
{/* Description */}