"use client";
import { useState } from "react";
import {
groupDealsByStage,
formatCurrency,
STAGES,
STAGE_STYLES,
} from "@/lib/crm";
import type { CrmState, Stage } from "@/lib/crm";
import { DealCard } from "./DealCard";
import { cn } from "@/lib/utils";
function StageColumn({
stage,
onMoveStage,
children,
}: {
stage: Stage;
onMoveStage?: (dealId: string, stage: Stage) => void;
children: React.ReactNode;
}) {
const [over, setOver] = useState(false);
return (
{
if (onMoveStage) {
e.preventDefault();
setOver(true);
}
}}
onDragLeave={() => setOver(false)}
onDrop={(e) => {
setOver(false);
if (onMoveStage) {
const dealId = e.dataTransfer.getData("text/plain");
if (dealId) onMoveStage(dealId, stage);
}
}}
>
{children}
);
}
export function PipelineBoard({
crm,
selectedDealId,
onSelect,
onMoveStage,
}: {
crm: CrmState;
selectedDealId: string | null;
onSelect: (id: string) => void;
onMoveStage?: (dealId: string, stage: Stage) => void;
}) {
const grouped = groupDealsByStage(crm.deals);
return (
{STAGES.map((stage) => {
const deals = grouped[stage];
const total = deals.reduce((s, d) => s + d.amount, 0);
return (
{stage}
{deals.length} ยท {formatCurrency(total)}
{deals.length === 0 ? (
No deals
) : (
deals.map((d) => {
const account = crm.accounts.find(
(a) => a.id === d.accountId,
);
const contactName = crm.contacts.find(
(c) => c.accountId === d.accountId,
)?.name;
const firstProductId = d.lineItems[0]?.productId;
const product = firstProductId
? crm.products.find((p) => p.id === firstProductId)
: undefined;
const owner = crm.salespeople.find((s) => s.id === d.ownerId);
return (
);
})
)}
);
})}
);
}