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 | 66x 66x 66x 66x 66x 66x 66x 66x 66x 66x 66x 66x 66x 3x 3x 3x 66x 2x 2x 2x 66x 1x 1x 1x 66x 66x 66x 66x 66x 6x 6x 6x 6x 66x 6x 6x 6x 6x 6x 6x 6x 66x 10x 29x 10x 9x 8x 1x 66x 5x 5x 5x 2x 2x 5x 1x 1x 5x 66x 2x 64x 2x 62x 10x 232x 66x 2x 1x 1x 1x 1x 1x | import { useState, useCallback } from "react";
import { useParams, useNavigate } from "@tanstack/react-router";
import { useMutation, useQueryClient } from "@tanstack/react-query";
import { AlertCircle, MessageSquarePlus, Bell, Upload, X } from "lucide-react";
import { usePro } from "../../lib/pro-context";
import {
useLead,
usePipelineStages,
useLeadSources,
useLeadActivities,
} from "../../hooks/queries/useCrmQueries";
import { crmApi } from "../../lib/api";
import type { ChangeStageInput } from "../../lib/api/pro/crm";
import { queryKeys } from "../../lib/query-keys";
import { parseCurrencyInput } from "../../lib/currency-utils";
import { LeadHeader } from "../../components/crm/LeadHeader";
import { StickyPageHeader } from "../../components/ui/sticky-page-header";
import { MobileFullPage } from "../../components/ui/mobile-full-page";
import { LeadOverview } from "../../components/crm/LeadOverview";
import { LeadDocuments } from "../../components/crm/LeadDocuments";
import { LeadReminders } from "../../components/crm/LeadReminders";
import { ActivityTimeline } from "../../components/crm/ActivityTimeline";
import { NoteForm } from "../../components/crm/NoteForm";
import { StageChangeDialogs, type PendingDrop } from "../../components/crm/StageChangeDialogs";
import { EmptyState } from "../../components/ui/empty-state";
import { Button } from "../../components/ui/button";
import { useIsMobile } from "../../hooks/useIsMobile";
export function CrmLeadDetailPage() {
const { leadId } = useParams({ strict: false }) as { leadId: string };
const { proId } = usePro();
const navigate = useNavigate();
const isMobile = useIsMobile();
const queryClient = useQueryClient();
const { data: lead, isLoading } = useLead(proId, leadId);
const { data: stages } = usePipelineStages(proId);
const { data: sources } = useLeadSources(proId);
const { data: activityData } = useLeadActivities(proId, leadId);
// Mobile action states — only one sheet open at a time
const [showNoteModal, setShowNoteModal] = useState(false);
const [showReminderDialog, setShowReminderDialog] = useState(false);
const [showUploadDialog, setShowUploadDialog] = useState(false);
const openNote = useCallback(() => {
setShowReminderDialog(false);
setShowUploadDialog(false);
setShowNoteModal(true);
}, []);
const openReminder = useCallback(() => {
setShowNoteModal(false);
setShowUploadDialog(false);
setShowReminderDialog(true);
}, []);
const openUpload = useCallback(() => {
setShowNoteModal(false);
setShowReminderDialog(false);
setShowUploadDialog(true);
}, []);
// Stage change from mobile header
const [pendingDrop, setPendingDrop] = useState<PendingDrop | null>(null);
const [dialogOrderValue, setDialogOrderValue] = useState("");
const [dialogLossReasonCategory, setDialogLossReasonCategory] = useState("");
const [dialogLossReason, setDialogLossReason] = useState("");
const invalidateLeadQueries = useCallback(() => {
Iif (!proId || !leadId) return;
queryClient.invalidateQueries({ queryKey: queryKeys.crm.lead(proId, leadId) });
queryClient.invalidateQueries({ queryKey: queryKeys.crm.kanban(proId) });
queryClient.invalidateQueries({ queryKey: queryKeys.crm.activities(proId, leadId) });
}, [queryClient, proId, leadId]);
const changeStageMutation = useMutation({
mutationFn: async (data: ChangeStageInput) => {
Iif (!proId || !lead) throw new Error("Missing proId or lead");
return crmApi.changeStage(proId, lead.id, data);
},
onSuccess: () => {
invalidateLeadQueries();
setPendingDrop(null);
setDialogOrderValue("");
setDialogLossReasonCategory("");
setDialogLossReason("");
},
});
const handleStageChange = useCallback(
(stageId: number) => {
Iif (!lead || !stages) return;
const stage = stages.find((s) => s.id === stageId);
if (!stage || stageId === lead.currentStageId) return;
if (stage.stageType === "system_terminal_won" || stage.stageType === "system_terminal_lost") {
setPendingDrop({ leadId: lead.id, stageId: stage.id, stageName: stage.name, stageType: stage.stageType });
} else {
changeStageMutation.mutate({ stageId });
}
},
[stages, lead, changeStageMutation],
);
const handleDialogConfirm = useCallback(() => {
Iif (!pendingDrop) return;
const data: ChangeStageInput = { stageId: pendingDrop.stageId };
if (pendingDrop.stageType === "system_terminal_won" && dialogOrderValue.trim()) {
const paise = parseCurrencyInput(dialogOrderValue);
if (paise !== null && paise >= 0) data.orderValuePaise = paise;
}
if (pendingDrop.stageType === "system_terminal_lost") {
Eif (dialogLossReasonCategory) data.lossReasonCategory = dialogLossReasonCategory;
Eif (dialogLossReason.trim()) data.lossReason = dialogLossReason.trim();
}
changeStageMutation.mutate(data);
}, [pendingDrop, dialogOrderValue, dialogLossReasonCategory, dialogLossReason, changeStageMutation]);
if (isLoading || !proId) {
return (
<div className="flex items-center justify-center h-64">
<div className="animate-spin rounded-full h-8 w-8 border-b-2 border-primary-600" />
</div>
);
}
if (!lead) {
return (
<EmptyState
icon={AlertCircle}
title="Lead not found"
description="This lead may have been deleted or you don't have access."
/>
);
}
// Stage selector for mobile header
const stageSelect = (
<select
value={lead.currentStageId}
onChange={(e) => handleStageChange(Number(e.target.value))}
className="h-9 px-3 rounded-md border border-border-default bg-background-elevated text-sm text-foreground-default focus:ring-2 focus:ring-primary-500"
>
{(stages || []).map((s) => (
<option key={s.id} value={s.id}>{s.name}</option>
))}
</select>
);
return (
<MobileFullPage
title={lead.customerName}
onBack={() => navigate({ to: "/crm" })}
headerActions={stageSelect}
>
<div className="space-y-6 max-w-7xl p-4 sm:p-0 pb-24 sm:pb-0">
{/* Header with name, contact info, stage selector */}
<div className="hidden sm:block">
<StickyPageHeader>
<LeadHeader
lead={lead}
proId={proId}
stages={stages || []}
sources={sources || []}
/>
</StickyPageHeader>
</div>
{/* Mobile: compact header (contact info only, no back/stage) */}
<div className="sm:hidden">
<LeadHeader
lead={lead}
proId={proId}
stages={stages || []}
sources={sources || []}
/>
</div>
{/* Two-column layout: desktop = left (note+reminders+docs) + right (overview+timeline) */}
{/* Mobile: reordered — overview+timeline first, reminders+docs last, NoteForm hidden (via action bar) */}
<div className="grid grid-cols-1 lg:grid-cols-3 gap-6">
{/* Left column (narrower) — desktop inline NoteForm + reminders + docs */}
<div className="space-y-6 order-last lg:order-none">
{/* NoteForm: inline on desktop, modal on mobile */}
<div className="hidden sm:block">
<NoteForm lead={lead} proId={proId} stages={stages || []} />
</div>
<LeadReminders
leadId={lead.id}
proId={proId}
externalOpen={showReminderDialog}
onExternalClose={() => setShowReminderDialog(false)}
/>
<LeadDocuments
leadId={lead.id}
proId={proId}
externalOpen={showUploadDialog}
onExternalClose={() => setShowUploadDialog(false)}
/>
</div>
{/* Right column (wider) — overview + timeline come first on mobile */}
<div className="lg:col-span-2 space-y-6 order-first lg:order-none">
<LeadOverview lead={lead} proId={proId} />
<div className="rounded-lg border border-border-default bg-background-elevated p-5">
<h3 className="text-sm font-semibold text-foreground-default mb-4">
Activity Timeline
</h3>
<ActivityTimeline
activities={activityData?.activities || []}
total={activityData?.total || 0}
/>
</div>
</div>
</div>
</div>
{/* Floating action bar — mobile only, hidden when any sheet is open */}
{isMobile && !showNoteModal && !showReminderDialog && !showUploadDialog && (
<div className="fixed bottom-0 inset-x-0 z-[61] border-t border-border-default bg-background-elevated px-4 py-3 flex items-center gap-2">
<Button
size="sm"
variant="outline"
className="flex-1"
onClick={openNote}
>
<MessageSquarePlus className="h-4 w-4" />
Add Note
</Button>
<Button
size="sm"
variant="outline"
className="flex-1"
onClick={openReminder}
>
<Bell className="h-4 w-4" />
Reminder
</Button>
<Button
size="sm"
variant="outline"
className="flex-1"
onClick={openUpload}
>
<Upload className="h-4 w-4" />
Upload
</Button>
</div>
)}
{/* NoteForm bottom sheet — mobile only */}
{showNoteModal && (
<div className="fixed inset-0 z-[62] flex flex-col justify-end">
<button
type="button"
aria-label="Close"
className="fixed inset-0 bg-black/50"
onClick={() => setShowNoteModal(false)}
tabIndex={-1}
/>
<div className="relative z-10 bg-background-base rounded-t-2xl animate-slide-up max-h-[85vh] flex flex-col mx-2">
<div className="flex items-center justify-between border-b border-border-default bg-background-elevated px-4 py-3 rounded-t-2xl flex-shrink-0">
<h2 className="text-lg font-semibold text-foreground-default">Add Note</h2>
<button
type="button"
onClick={() => setShowNoteModal(false)}
className="p-1.5 text-foreground-subtle hover:text-foreground-default rounded-md"
aria-label="Close"
>
<X className="h-5 w-5" />
</button>
</div>
<div className="flex-1 overflow-y-auto p-4">
<NoteForm
lead={lead}
proId={proId}
stages={stages || []}
onClose={() => setShowNoteModal(false)}
bare
/>
</div>
</div>
</div>
)}
{/* Stage change dialogs (for mobile header stage selector) */}
<StageChangeDialogs
pendingDrop={pendingDrop}
orderValue={dialogOrderValue}
onOrderValueChange={setDialogOrderValue}
lossReasonCategory={dialogLossReasonCategory}
onLossReasonCategoryChange={setDialogLossReasonCategory}
lossReason={dialogLossReason}
onLossReasonChange={setDialogLossReason}
onConfirm={handleDialogConfirm}
onCancel={() => {
setPendingDrop(null);
setDialogOrderValue("");
setDialogLossReasonCategory("");
setDialogLossReason("");
}}
isPending={changeStageMutation.isPending}
/>
</MobileFullPage>
);
}
|