import { useRef, 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 { assetsApi } from "../api/assets"; 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, Plus, X, } from "lucide-react"; import { cn } from "../lib/utils"; import { MarkdownEditor, type MarkdownEditorRef } from "./MarkdownEditor"; import { StatusBadge } from "./StatusBadge"; 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 [goalIds, setGoalIds] = useState([]); const [targetDate, setTargetDate] = useState(""); const [expanded, setExpanded] = useState(false); const [statusOpen, setStatusOpen] = useState(false); const [goalOpen, setGoalOpen] = useState(false); const descriptionEditorRef = useRef(null); 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(); }, }); const uploadDescriptionImage = useMutation({ mutationFn: async (file: File) => { if (!selectedCompanyId) throw new Error("No company selected"); return assetsApi.uploadImage(selectedCompanyId, file, "projects/drafts"); }, }); function reset() { setName(""); setDescription(""); setStatus("planned"); setGoalIds([]); setTargetDate(""); setExpanded(false); } function handleSubmit() { if (!selectedCompanyId || !name.trim()) return; createProject.mutate({ name: name.trim(), description: description.trim() || undefined, status, ...(goalIds.length > 0 ? { goalIds } : {}), ...(targetDate ? { targetDate } : {}), }); } function handleKeyDown(e: React.KeyboardEvent) { if (e.key === "Enter" && (e.metaKey || e.ctrlKey)) { e.preventDefault(); handleSubmit(); } } const selectedGoals = (goals ?? []).filter((g) => goalIds.includes(g.id)); const availableGoals = (goals ?? []).filter((g) => !goalIds.includes(g.id)); return ( { if (!open) { reset(); closeNewProject(); } }} > {/* Header */}
{selectedCompany && ( {selectedCompany.name.slice(0, 3).toUpperCase()} )} New project
{/* Name */}
setName(e.target.value)} onKeyDown={(e) => { if (e.key === "Tab" && !e.shiftKey) { e.preventDefault(); descriptionEditorRef.current?.focus(); } }} autoFocus />
{/* Description */}
{ const asset = await uploadDescriptionImage.mutateAsync(file); return asset.contentPath; }} />
{/* Property chips */}
{/* Status */} {projectStatuses.map((s) => ( ))} {selectedGoals.map((goal) => ( {goal.title} ))} {selectedGoals.length === 0 && ( )} {availableGoals.map((g) => ( ))} {selectedGoals.length > 0 && availableGoals.length === 0 && (
All goals already selected.
)}
{/* Target date */}
setTargetDate(e.target.value)} placeholder="Target date" />
{/* Footer */}
); }