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 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 | 82x 82x 82x 82x 82x 82x 82x 82x 41x 82x 5x 5x 5x 1x 4x 5x 82x 82x 82x 82x 82x 82x 41x 41x 160x 41x 36x 36x 41x 82x 41x 41x 36x 41x 82x 82x 82x 8x 8x 8x 8x 3x 3x 8x 1x 1x 8x 82x 41x 41x 160x 41x 82x 13x 13x 11x 11x 11x 8x 8x 3x 82x 5x 5x 5x 2x 2x 5x 2x 2x 5x 5x 5x 5x 5x 82x 1x 1x 1x 1x 82x 324x 82x 3x 82x 2x 82x 1x 81x 6x 24x 24x 1x 75x 300x 4x 1x 1x 1x 1x | import { useMutation, useQueryClient } from "@tanstack/react-query";
import { Contact } from "lucide-react";
import { useCallback, useMemo, useState } from "react";
import { useLeadSources } from "../../hooks/queries/useCrmQueries";
import { crmApi } from "../../lib/api";
import type { KanbanData, KanbanLead } from "../../lib/api/pro/crm";
import { parseCurrencyInput } from "../../lib/currency-utils";
import { queryKeys } from "../../lib/query-keys";
import { EmptyState } from "../ui/empty-state";
import { KanbanColumn } from "./KanbanColumn";
import { MobileLeadList } from "./MobileLeadList";
import { QuickNoteDialog } from "./QuickNoteDialog";
import { SetReminderDialog } from "./SetReminderDialog";
import { StageChangeDialogs, type PendingDrop } from "./StageChangeDialogs";
// ─── Types ───────────────────────────────────────────────────────────────────
type KanbanBoardProps = {
proId: string;
data: KanbanData;
onAddLead: () => void;
};
type QuickAction = {
type: "note" | "reminder";
leadId: number;
};
// ─── Component ───────────────────────────────────────────────────────────────
export function KanbanBoard({ proId, data, onAddLead }: KanbanBoardProps) {
const queryClient = useQueryClient();
const { data: sources } = useLeadSources(proId);
const [pendingDrop, setPendingDrop] = useState<PendingDrop | null>(null);
const [lossReasonCategory, setLossReasonCategory] = useState("");
const [lossReason, setLossReason] = useState("");
const [orderValue, setOrderValue] = useState("");
const [quickAction, setQuickAction] = useState<QuickAction | null>(null);
// Terminal columns default to expanded (users can still collapse manually)
const [collapsedColumns, setCollapsedColumns] = useState<Set<number>>(
() => new Set<number>(),
);
const toggleCollapse = useCallback((stageId: number) => {
setCollapsedColumns((prev) => {
const next = new Set(prev);
if (next.has(stageId)) {
next.delete(stageId);
} else {
next.add(stageId);
}
return next;
});
}, []);
// Build source name lookup
const sourceMap = useMemo(() => {
const map = new Map<number, string>();
for (const s of sources || []) {
map.set(s.id, s.name);
}
return map;
}, [sources]);
// 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]);
// Build lead lookup for O(1) access
const leadMap = useMemo(() => {
const map = new Map<number, KanbanLead>();
for (const lead of data.leads) {
map.set(lead.id, lead);
}
return map;
}, [data.leads]);
// Find the lead object for the quick action dialog
const quickActionLead = quickAction
? leadMap.get(quickAction.leadId) ?? null
: null;
// Change stage mutation
const kanbanQueryKey = queryKeys.crm.kanban(proId);
const changeStageMutation = useMutation({
mutationFn: async ({
leadId,
stageId,
orderValuePaise,
lossReason: reason,
lossReasonCategory: category,
}: {
leadId: number;
stageId: number;
orderValuePaise?: number;
lossReason?: string;
lossReasonCategory?: string;
}) => {
return crmApi.changeStage(proId, leadId, {
stageId,
orderValuePaise,
lossReason: reason,
lossReasonCategory: category,
});
},
onMutate: async ({ leadId, stageId }) => {
await queryClient.cancelQueries({ queryKey: kanbanQueryKey });
const previous = queryClient.getQueryData<KanbanData>(kanbanQueryKey);
if (previous) {
queryClient.setQueryData<KanbanData>(kanbanQueryKey, {
...previous,
leads: previous.leads.map((l) =>
l.id === leadId ? { ...l, currentStageId: stageId } : l,
),
});
}
return { previous };
},
onError: (_err, _vars, context) => {
Eif (context?.previous) {
queryClient.setQueryData(kanbanQueryKey, context.previous);
}
},
onSettled: () => {
queryClient.invalidateQueries({
queryKey: queryKeys.crm.all,
});
},
});
// Build stage lookup for O(1) access in handleDrop
const stageMap = useMemo(() => {
const map = new Map<number, (typeof data.stages)[number]>();
for (const stage of data.stages) {
map.set(stage.id, stage);
}
return map;
}, [data.stages]);
const handleDrop = useCallback(
(leadId: number, stageId: number) => {
const lead = leadMap.get(leadId);
if (!lead || lead.currentStageId === stageId) return;
const targetStage = stageMap.get(stageId);
Iif (!targetStage) return;
// Won/Lost require confirmation
if (
targetStage.stageType === "system_terminal_won" ||
targetStage.stageType === "system_terminal_lost"
) {
setPendingDrop({
leadId,
stageId,
stageName: targetStage.name,
stageType: targetStage.stageType,
});
return;
}
changeStageMutation.mutate({ leadId, stageId });
},
[leadMap, stageMap, changeStageMutation],
);
const confirmDrop = useCallback(() => {
Iif (!pendingDrop) return;
const extras: {
orderValuePaise?: number;
lossReason?: string;
lossReasonCategory?: string;
} = {};
if (pendingDrop.stageType === "system_terminal_won" && orderValue) {
const parsed = parseCurrencyInput(orderValue);
Eif (parsed && parsed > 0) extras.orderValuePaise = parsed;
}
if (pendingDrop.stageType === "system_terminal_lost") {
if (lossReasonCategory) extras.lossReasonCategory = lossReasonCategory;
if (lossReason) extras.lossReason = lossReason;
}
changeStageMutation.mutate({
leadId: pendingDrop.leadId,
stageId: pendingDrop.stageId,
...extras,
});
setPendingDrop(null);
setLossReasonCategory("");
setLossReason("");
setOrderValue("");
}, [pendingDrop, orderValue, lossReasonCategory, lossReason, changeStageMutation]);
const cancelDrop = useCallback(() => {
setPendingDrop(null);
setLossReasonCategory("");
setLossReason("");
setOrderValue("");
}, []);
const isTerminal = (type: string) =>
type === "system_terminal_won" || type === "system_terminal_lost";
const handleQuickNote = useCallback(
(leadId: number) => setQuickAction({ type: "note", leadId }),
[],
);
const handleQuickReminder = useCallback(
(leadId: number) => setQuickAction({ type: "reminder", leadId }),
[],
);
if (data.stages.length === 0) {
return (
<EmptyState
icon={Contact}
title="Setting up your pipeline"
description="Your CRM pipeline is being initialized. Please refresh the page."
/>
);
}
if (data.leads.length === 0) {
return (
<div>
{/* Mobile: empty stage list */}
<div className="md:hidden space-y-2 mb-4">
{data.stages.map((stage) => (
<div
key={stage.id}
className="flex items-center justify-between rounded-md border border-border-default bg-background-elevated px-4 py-3"
>
<span className="text-sm font-medium text-foreground-default">
{stage.name}
</span>
<span className="text-xs text-foreground-subtle">0 leads</span>
</div>
))}
</div>
{/* Desktop: empty kanban columns */}
<div
className="hidden md:flex gap-4 overflow-x-auto pb-4 h-[calc(100vh-200px)]"
>
{data.stages.map((stage) => (
<KanbanColumn
key={stage.id}
stage={stage}
leads={[]}
sourceMap={sourceMap}
onDrop={handleDrop}
onAddLead={
stage.stageType === "system_entry" ? onAddLead : undefined
}
isCollapsed={collapsedColumns.has(stage.id)}
onToggleCollapse={
isTerminal(stage.stageType)
? () => toggleCollapse(stage.id)
: undefined
}
/>
))}
</div>
</div>
);
}
return (
<>
{/* Mobile: list view grouped by stage */}
<div className="md:hidden">
<MobileLeadList
data={data}
sourceMap={sourceMap}
onAddLead={onAddLead}
onQuickNote={handleQuickNote}
onQuickReminder={handleQuickReminder}
/>
</div>
{/* Desktop: kanban columns */}
<div
className="hidden md:flex gap-4 overflow-x-auto pb-4 h-[calc(100vh-200px)]"
>
{data.stages.map((stage) => (
<KanbanColumn
key={stage.id}
stage={stage}
leads={leadsByStage.get(stage.id) || []}
sourceMap={sourceMap}
onDrop={handleDrop}
onAddLead={
stage.stageType === "system_entry" ? onAddLead : undefined
}
isCollapsed={collapsedColumns.has(stage.id)}
onToggleCollapse={
isTerminal(stage.stageType)
? () => toggleCollapse(stage.id)
: undefined
}
onQuickNote={(leadId) => setQuickAction({ type: "note", leadId })}
onQuickReminder={(leadId) =>
setQuickAction({ type: "reminder", leadId })
}
/>
))}
</div>
{/* Won/Lost confirmation dialogs */}
<StageChangeDialogs
pendingDrop={pendingDrop}
orderValue={orderValue}
onOrderValueChange={setOrderValue}
lossReasonCategory={lossReasonCategory}
onLossReasonCategoryChange={setLossReasonCategory}
lossReason={lossReason}
onLossReasonChange={setLossReason}
onConfirm={confirmDrop}
onCancel={cancelDrop}
isPending={changeStageMutation.isPending}
/>
{/* Quick note dialog */}
{quickActionLead && (
<QuickNoteDialog
leadId={quickAction?.leadId ?? 0}
proId={proId}
lead={quickActionLead}
stages={data.stages}
open={quickAction?.type === "note"}
onClose={() => setQuickAction(null)}
/>
)}
{/* Quick reminder dialog */}
<SetReminderDialog
proId={proId}
leadId={quickAction?.leadId ?? 0}
open={quickAction?.type === "reminder"}
onClose={() => setQuickAction(null)}
additionalInvalidations={[kanbanQueryKey]}
/>
</>
);
}
|