All files / src/components/crm KanbanColumn.tsx

90.9% Statements 30/33
93.1% Branches 27/29
87.5% Functions 7/8
93.75% Lines 30/32

Press n or j to go to the next uncovered block, b, p or k for the previous block.

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225                                                        341x   85x   83x   81x   92x         341x 83x 258x 258x 177x                               341x   341x 3x 3x 3x     341x   1x 1x     341x   15x 15x 15x 15x 14x           341x               341x     341x 6x                                                                       335x                                                                                                                                               78x                                  
import { useState, useCallback } from "react";
import { Plus, Trophy, XCircle, ChevronLeft } from "lucide-react";
import { cn } from "../../lib/utils";
import { LeadCard } from "./LeadCard";
import type { KanbanLead, PipelineStage } from "../../lib/api/pro/crm";
 
// ─── Types ───────────────────────────────────────────────────────────────────
 
type StageWithCounts = PipelineStage & {
	leadCount: number;
	totalValue: number;
};
 
type KanbanColumnProps = {
	stage: StageWithCounts;
	leads: KanbanLead[];
	sourceMap: Map<number, string>;
	onDrop: (leadId: number, stageId: number) => void;
	onAddLead?: () => void;
	isCollapsed?: boolean;
	onToggleCollapse?: () => void;
	onQuickNote?: (leadId: number) => void;
	onQuickReminder?: (leadId: number) => void;
};
 
// ─── Stage header color mapping ──────────────────────────────────────────────
 
function getStageHeaderStyle(stageType: string) {
	switch (stageType) {
		case "system_entry":
			return "border-t-primary-500";
		case "system_terminal_won":
			return "border-t-success";
		case "system_terminal_lost":
			return "border-t-error";
		default:
			return "border-t-secondary-500";
	}
}
 
function getStageIcon(stageType: string) {
	if (stageType === "system_terminal_won")
		return <Trophy className="h-4 w-4 text-success" />;
	if (stageType === "system_terminal_lost")
		return <XCircle className="h-4 w-4 text-error" />;
	return null;
}
 
// ─── Component ───────────────────────────────────────────────────────────────
 
export function KanbanColumn({
	stage,
	leads,
	sourceMap,
	onDrop,
	onAddLead,
	isCollapsed,
	onToggleCollapse,
	onQuickNote,
	onQuickReminder,
}: KanbanColumnProps) {
	const [isDragOver, setIsDragOver] = useState(false);
 
	const handleDragOver = useCallback((e: React.DragEvent) => {
		e.preventDefault();
		e.dataTransfer.dropEffect = "move";
		setIsDragOver(true);
	}, []);
 
	const handleDragLeave = useCallback((e: React.DragEvent) => {
		// Only count leaving the column itself, not child elements
		Iif (e.currentTarget.contains(e.relatedTarget as Node)) return;
		setIsDragOver(false);
	}, []);
 
	const handleDrop = useCallback(
		(e: React.DragEvent) => {
			e.preventDefault();
			setIsDragOver(false);
			const leadId = Number(e.dataTransfer.getData("text/plain"));
			if (leadId) {
				onDrop(leadId, stage.id);
			}
		},
		[onDrop, stage.id],
	);
 
	const handleDragStart = useCallback(
		(e: React.DragEvent, leadId: number) => {
			e.dataTransfer.setData("text/plain", String(leadId));
			e.dataTransfer.effectAllowed = "move";
		},
		[],
	);
 
	const headerValue = `${stage.leadCount}`;
 
	// Collapsed view for terminal stages
	if (isCollapsed) {
		return (
			<section
				aria-label={`${stage.name} stage (collapsed)`}
				className={cn(
					"flex flex-col rounded-lg border-t-4 bg-background-subtle w-[56px] min-w-[56px] max-h-full",
					getStageHeaderStyle(stage.stageType),
					isDragOver && "ring-2 ring-primary-500/50 bg-primary-50/30",
				)}
				onDragOver={handleDragOver}
				onDragLeave={handleDragLeave}
				onDrop={handleDrop}
			>
				<button
					type="button"
					aria-label={`Expand ${stage.name} column`}
					className="flex flex-col items-center flex-1 w-full cursor-pointer hover:bg-background-muted transition-colors rounded-lg"
					onClick={onToggleCollapse}
				>
					<div className="py-3">
						<span className="text-xs font-medium text-foreground-muted bg-background-muted rounded-full px-2 py-0.5">
							{stage.leadCount}
						</span>
					</div>
					<div className="flex-1 flex items-center justify-center">
						<span
							className="text-sm font-semibold text-foreground-default [writing-mode:vertical-rl] [text-orientation:mixed]"
						>
							{getStageIcon(stage.stageType)}
							{stage.name}
						</span>
					</div>
				</button>
			</section>
		);
	}
 
	return (
		<section
			aria-label={`${stage.name} stage`}
			className={cn(
				"flex flex-col rounded-lg border-t-4 bg-background-subtle min-w-[280px] w-[280px] max-h-full",
				getStageHeaderStyle(stage.stageType),
				isDragOver && "ring-2 ring-primary-500/50 bg-primary-50/30",
			)}
			onDragOver={handleDragOver}
			onDragLeave={handleDragLeave}
			onDrop={handleDrop}
		>
			{/* Column header */}
			<div className="flex items-center justify-between px-3 py-2.5 border-b border-border-default">
				<div className="flex items-center gap-1.5 min-w-0">
					{getStageIcon(stage.stageType)}
					<h3 className="text-sm font-semibold text-foreground-default truncate">
						{stage.name}
					</h3>
				</div>
				<div className="flex items-center gap-1.5 flex-shrink-0">
					<span className="text-xs font-medium text-foreground-muted bg-background-muted rounded-full px-2 py-0.5">
						{headerValue}
					</span>
					{onAddLead &&
						stage.stageType === "system_entry" && (
							<button
								type="button"
								onClick={onAddLead}
								className="p-0.5 rounded text-foreground-subtle hover:text-primary-600 hover:bg-primary-50 transition-colors"
								title="Add lead"
							>
								<Plus className="h-4 w-4" />
							</button>
						)}
					{onToggleCollapse && (
						<button
							type="button"
							onClick={onToggleCollapse}
							className="p-0.5 rounded text-foreground-subtle hover:text-primary-600 hover:bg-primary-50 transition-colors"
							title="Collapse column"
						>
							<ChevronLeft className="h-4 w-4" />
						</button>
					)}
				</div>
			</div>
 
			{/* Cards container */}
			<div className="flex-1 overflow-y-auto p-2 space-y-2">
				{leads.length === 0 ? (
					<div className="text-center py-8 text-xs text-foreground-subtle">
						{isDragOver
							? "Drop here"
							: onAddLead
								? (
									<div className="space-y-2">
										<p className="text-sm text-foreground-muted">No leads in this stage</p>
										<button
											type="button"
											onClick={onAddLead}
											className="mt-2 inline-flex items-center gap-1.5 px-3 py-1.5 text-sm font-medium text-foreground-inverse bg-primary-600 hover:bg-primary-700 rounded-md transition-colors"
										>
											<Plus className="h-3.5 w-3.5" />
											Add Lead
										</button>
									</div>
								)
								: "No leads"}
					</div>
				) : (
					leads.map((lead) => (
						<LeadCard
							key={lead.id}
							lead={lead}
							sourceName={sourceMap.get(lead.leadSourceId)}
							documentCount={lead.documentCount}
							reminderCount={lead.reminderCount}
							nextReminderDueAt={lead.nextReminderDueAt}
							onDragStart={handleDragStart}
							onQuickNote={onQuickNote}
							onQuickReminder={onQuickReminder}
						/>
					))
				)}
			</div>
		</section>
	);
}