"use client"; import { useState, useEffect } from "react"; import { createClient } from "@/lib/supabase/client"; import { Pipeline, PipelineStage } from "@/types"; import { PipelineBoard } from "@/components/pipelines/pipeline-board"; import { PipelineSettings } from "@/components/pipelines/pipeline-settings"; import { Button } from "@/components/ui/button"; import { Dialog, DialogContent, DialogHeader, DialogTitle, DialogFooter, } from "@/components/ui/dialog"; import { Input } from "@/components/ui/input"; import { Label } from "@/components/ui/label"; import { GitBranch, Plus, Settings, ChevronDown, } from "lucide-react"; const DEFAULT_STAGES = [ { name: "Lead", color: "#6366f1", position: 0 }, { name: "Qualified", color: "#8b5cf6", position: 1 }, { name: "Proposal", color: "#f97316", position: 2 }, { name: "Negotiation", color: "#eab308", position: 3 }, { name: "Won", color: "#22c55e", position: 4 }, ]; export default function PipelinesPage() { const [pipelines, setPipelines] = useState([]); const [stages, setStages] = useState([]); const [selectedPipelineId, setSelectedPipelineId] = useState(""); const [loading, setLoading] = useState(true); const [newPipelineOpen, setNewPipelineOpen] = useState(false); const [newPipelineName, setNewPipelineName] = useState(""); const [creating, setCreating] = useState(false); const [settingsOpen, setSettingsOpen] = useState(false); const [dropdownOpen, setDropdownOpen] = useState(false); const supabase = createClient(); useEffect(() => { loadPipelines(); }, []); useEffect(() => { if (selectedPipelineId) { loadStages(selectedPipelineId); } }, [selectedPipelineId]); async function loadPipelines() { setLoading(true); const { data } = await supabase .from("pipelines") .select("*") .order("created_at"); if (data && data.length > 0) { setPipelines(data); if (!selectedPipelineId || !data.find((p) => p.id === selectedPipelineId)) { setSelectedPipelineId(data[0].id); } } else { setPipelines([]); setSelectedPipelineId(""); } setLoading(false); } async function loadStages(pipelineId: string) { const { data } = await supabase .from("pipeline_stages") .select("*") .eq("pipeline_id", pipelineId) .order("position"); if (data) setStages(data); } async function handleCreatePipeline() { if (!newPipelineName.trim()) return; setCreating(true); const { data: { session }, } = await supabase.auth.getSession(); const user = session?.user; if (!user) { setCreating(false); return; } const { data: pipeline } = await supabase .from("pipelines") .insert({ user_id: user.id, name: newPipelineName.trim() }) .select() .single(); if (pipeline) { const stageInserts = DEFAULT_STAGES.map((s) => ({ pipeline_id: pipeline.id, name: s.name, color: s.color, position: s.position, })); await supabase.from("pipeline_stages").insert(stageInserts); setNewPipelineName(""); setNewPipelineOpen(false); setSelectedPipelineId(pipeline.id); await loadPipelines(); } setCreating(false); } function handleRefresh() { loadPipelines(); if (selectedPipelineId) { loadStages(selectedPipelineId); } } const selectedPipeline = pipelines.find((p) => p.id === selectedPipelineId); if (loading) { return (
{[1, 2, 3, 4].map((i) => (
))}
); } return (
{/* Header */}

Pipelines

Manage your deals and sales pipeline

{selectedPipeline && ( )}
{/* Pipeline selector */} {pipelines.length > 1 && (
{dropdownOpen && ( <>
setDropdownOpen(false)} />
{pipelines.map((p) => ( ))}
)}
)} {/* Board or empty state */} {pipelines.length === 0 ? (

No pipelines yet

Create your first pipeline to start tracking deals

) : ( loadStages(selectedPipelineId)} /> )} {/* New Pipeline Dialog */} New Pipeline
setNewPipelineName(e.target.value)} placeholder="e.g., Sales Pipeline" className="mt-2 bg-slate-800 border-slate-700 text-white" onKeyDown={(e) => { if (e.key === "Enter") handleCreatePipeline(); }} />

Default stages (Lead, Qualified, Proposal, Negotiation, Won) will be created automatically.

{/* Pipeline Settings */} {selectedPipeline && ( )}
); }