All files / src/components/crm MobileLeadList.tsx

91.89% Statements 34/37
78.57% Branches 22/28
83.33% Functions 10/12
91.66% Lines 33/36

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                                        170x                               85x     85x 44x 44x 170x 85x     44x     85x 1x 1x 1x     1x   1x         85x 44x 44x 170x   44x 41x 41x   44x       85x       85x 1x     84x                         334x               334x 334x 334x   334x               1x                                                               80x                                                              
import { ChevronDown, ChevronRight, Plus } from "lucide-react";
import { useMemo, useState } from "react";
import type { KanbanData, KanbanLead } from "../../lib/api/pro/crm";
import { abbreviatePaise } from "../../lib/currency-utils";
import { Button } from "../ui/button";
import { LeadCard } from "./LeadCard";
 
// ─── Types ───────────────────────────────────────────────────────────────────
 
type MobileLeadListProps = {
	data: KanbanData;
	sourceMap: Map<number, string>;
	onAddLead: () => void;
	onQuickNote?: (leadId: number) => void;
	onQuickReminder?: (leadId: number) => void;
};
 
// ─── Helpers ─────────────────────────────────────────────────────────────────
 
function isTerminalStage(stageType: string): boolean {
	return (
		stageType === "system_terminal_won" ||
		stageType === "system_terminal_lost"
	);
}
 
// ─── Component ───────────────────────────────────────────────────────────────
 
export function MobileLeadList({
	data,
	sourceMap,
	onAddLead,
	onQuickNote,
	onQuickReminder,
}: MobileLeadListProps) {
	// Filter: null = show all, number = filter to one stage
	const [stageFilter, setStageFilter] = useState<number | null>(null);
 
	// Terminal stages start collapsed, non-terminal start expanded
	const [collapsed, setCollapsed] = useState<Set<number>>(() => {
		const ids = new Set<number>();
		for (const stage of data.stages) {
			if (isTerminalStage(stage.stageType)) {
				ids.add(stage.id);
			}
		}
		return ids;
	});
 
	const toggleSection = (stageId: number) => {
		setCollapsed((prev) => {
			const next = new Set(prev);
			Iif (next.has(stageId)) {
				next.delete(stageId);
			} else {
				next.add(stageId);
			}
			return next;
		});
	};
 
	// Group leads by stage
	const leadsByStage = useMemo(() => {
		const map = new Map<number, KanbanLead[]>();
		for (const stage of data.stages) {
			map.set(stage.id, []);
		}
		for (const lead of data.leads) {
			const arr = map.get(lead.currentStageId);
			Eif (arr) arr.push(lead);
		}
		return map;
	}, [data]);
 
	// Determine which stages to show
	const visibleStages = stageFilter
		? data.stages.filter((s) => s.id === stageFilter)
		: data.stages;
 
	if (data.stages.length === 0) {
		return null; // KanbanBoard handles the empty-pipeline state
	}
 
	return (
		<div className="space-y-3">
			{/* Stage filter dropdown */}
			<select
				aria-label="Filter by stage"
				value={stageFilter ?? ""}
				onChange={(e) =>
					setStageFilter(e.target.value ? Number(e.target.value) : null)
				}
				className="w-full h-10 px-3 rounded-md border border-border-default bg-background-elevated text-foreground-default text-sm focus:ring-2 focus:ring-primary-500"
			>
				<option value="">All Stages</option>
				{data.stages.map((stage) => (
					<option key={stage.id} value={stage.id}>
						{stage.name} ({stage.leadCount})
					</option>
				))}
			</select>
 
			{/* Stage sections */}
			{visibleStages.map((stage) => {
				const leads = leadsByStage.get(stage.id) || [];
				const isOpen = !collapsed.has(stage.id);
				const isEntry = stage.stageType === "system_entry";
 
				return (
					<div
						key={stage.id}
						className="rounded-lg border border-border-default bg-background-elevated overflow-hidden"
					>
						{/* Section header */}
						<button
							type="button"
							onClick={() => toggleSection(stage.id)}
							className="w-full flex items-center justify-between px-4 py-3 bg-background-subtle hover:bg-background-muted transition-colors"
						>
							<div className="flex items-center gap-2">
								{isOpen ? (
									<ChevronDown className="h-4 w-4 text-foreground-subtle" />
								) : (
									<ChevronRight className="h-4 w-4 text-foreground-subtle" />
								)}
								<span className="text-sm font-semibold text-foreground-default">
									{stage.name}
								</span>
								<span className="text-xs font-medium text-foreground-muted bg-background-muted rounded-full px-2 py-0.5">
									{stage.leadCount}
								</span>
							</div>
							{stage.totalValue > 0 && (
								<span className="text-xs font-medium text-foreground-muted">
									₹{abbreviatePaise(stage.totalValue)}
								</span>
							)}
						</button>
 
						{/* Lead cards */}
						{isOpen && (
							<div className="p-3 space-y-2">
								{leads.length === 0 ? (
									<p className="text-xs text-foreground-subtle text-center py-3">
										No leads in this stage
									</p>
								) : (
									leads.map((lead) => (
										<LeadCard
											key={lead.id}
											lead={lead}
											sourceName={sourceMap.get(lead.leadSourceId)}
											documentCount={lead.documentCount}
											reminderCount={lead.reminderCount}
											nextReminderDueAt={lead.nextReminderDueAt}
											onQuickNote={onQuickNote}
											onQuickReminder={onQuickReminder}
										/>
									))
								)}
								{isEntry && (
									<Button
										variant="outline"
										size="sm"
										className="w-full mt-1"
										onClick={onAddLead}
									>
										<Plus className="h-3.5 w-3.5 mr-1" />
										Add Lead
									</Button>
								)}
							</div>
						)}
					</div>
				);
			})}
		</div>
	);
}