"use client"; import { useState, useEffect } from "react"; import { createClient } from "@/lib/supabase/client"; import { Pipeline, PipelineStage } from "@/types"; import { Dialog, DialogContent, DialogHeader, DialogTitle, DialogFooter, } from "@/components/ui/dialog"; import { Button } from "@/components/ui/button"; import { Input } from "@/components/ui/input"; import { Label } from "@/components/ui/label"; import { Trash2, Plus, GripVertical, ArrowUp, ArrowDown, AlertTriangle, } from "lucide-react"; const STAGE_COLORS = [ "#6366f1", "#8b5cf6", "#ec4899", "#f43f5e", "#f97316", "#eab308", "#22c55e", "#14b8a6", "#06b6d4", "#3b82f6", ]; interface PipelineSettingsProps { open: boolean; onOpenChange: (open: boolean) => void; pipeline: Pipeline; stages: PipelineStage[]; onUpdated: () => void; } export function PipelineSettings({ open, onOpenChange, pipeline, stages, onUpdated, }: PipelineSettingsProps) { const [name, setName] = useState(pipeline.name); const [localStages, setLocalStages] = useState(stages); const [newStageName, setNewStageName] = useState(""); const [newStageColor, setNewStageColor] = useState(STAGE_COLORS[0]); const [saving, setSaving] = useState(false); const [showDeleteConfirm, setShowDeleteConfirm] = useState(false); const [deleting, setDeleting] = useState(false); const supabase = createClient(); useEffect(() => { if (open) { setName(pipeline.name); setLocalStages([...stages].sort((a, b) => a.position - b.position)); setShowDeleteConfirm(false); } }, [open, pipeline, stages]); async function handleSave() { setSaving(true); await supabase .from("pipelines") .update({ name: name.trim() }) .eq("id", pipeline.id); for (let i = 0; i < localStages.length; i++) { const stage = localStages[i]; await supabase .from("pipeline_stages") .update({ name: stage.name, color: stage.color, position: i }) .eq("id", stage.id); } setSaving(false); onOpenChange(false); onUpdated(); } async function handleAddStage() { if (!newStageName.trim()) return; const { data: { session }, } = await supabase.auth.getSession(); const user = session?.user; if (!user) return; const { data } = await supabase .from("pipeline_stages") .insert({ pipeline_id: pipeline.id, name: newStageName.trim(), color: newStageColor, position: localStages.length, }) .select() .single(); if (data) { setLocalStages([...localStages, data]); setNewStageName(""); setNewStageColor( STAGE_COLORS[(localStages.length + 1) % STAGE_COLORS.length] ); } } async function handleRemoveStage(stageId: string) { await supabase.from("pipeline_stages").delete().eq("id", stageId); setLocalStages(localStages.filter((s) => s.id !== stageId)); } function moveStage(index: number, direction: "up" | "down") { const newStages = [...localStages]; const targetIndex = direction === "up" ? index - 1 : index + 1; if (targetIndex < 0 || targetIndex >= newStages.length) return; [newStages[index], newStages[targetIndex]] = [ newStages[targetIndex], newStages[index], ]; setLocalStages(newStages); } async function handleDeletePipeline() { setDeleting(true); await supabase.from("deals").delete().eq("pipeline_id", pipeline.id); await supabase .from("pipeline_stages") .delete() .eq("pipeline_id", pipeline.id); await supabase.from("pipelines").delete().eq("id", pipeline.id); setDeleting(false); onOpenChange(false); onUpdated(); } return ( Pipeline Settings {showDeleteConfirm ? (

Delete Pipeline

This will permanently delete the pipeline, all stages, and all deals within it. This cannot be undone.

) : ( <>
setName(e.target.value)} className="bg-slate-800 border-slate-700 text-white" />
{localStages.map((stage, index) => (
{ const updated = [...localStages]; updated[index] = { ...updated[index], name: e.target.value, }; setLocalStages(updated); }} className="h-7 bg-transparent border-transparent text-white text-sm focus:border-slate-600" />
))}
{STAGE_COLORS.map((color) => (
setNewStageName(e.target.value)} placeholder="New stage name" className="bg-slate-800 border-slate-700 text-white text-sm" onKeyDown={(e) => { if (e.key === "Enter") handleAddStage(); }} />
)}
); }