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 | 6x 6x 41x 41x 41x 41x 41x 41x 41x 41x 41x 11x 41x 41x 41x 5x 5x 5x 5x 5x 5x 5x 41x 5x 5x 41x 4x 3x 3x 3x 3x 1x 41x 6x 6x 6x 1x 1x 5x 1x 1x 4x 4x 4x 1x 1x 4x 1x 4x 41x 39x 3x 3x 36x 3x 147x 108x | import { useMutation, useQueryClient } from "@tanstack/react-query";
import { AlertCircle, X } from "lucide-react";
import { useCallback, useState } from "react";
import { crmApi } from "../../lib/api";
import type { AddNoteInput, PipelineStage } from "../../lib/api/pro/crm";
import { queryKeys } from "../../lib/query-keys";
import { Button } from "../ui/button";
// ─── Constants ───────────────────────────────────────────────────────────────
const CONTACT_METHODS = [
{ value: "call", label: "Call" },
{ value: "whatsapp", label: "WhatsApp" },
{ value: "site_visit", label: "Site Visit" },
{ value: "showroom_visit", label: "Showroom Visit" },
{ value: "video_call", label: "Video Call" },
{ value: "other", label: "Other" },
];
const MAX_NOTE_LENGTH = 2000;
// ─── Component ───────────────────────────────────────────────────────────────
type QuickNoteDialogProps = {
leadId: number;
proId: string;
lead: { currentStageId: number; id: number };
stages: PipelineStage[];
open: boolean;
onClose: () => void;
};
export function QuickNoteDialog({
leadId,
proId,
lead,
stages,
open,
onClose,
}: QuickNoteDialogProps) {
const queryClient = useQueryClient();
const [content, setContent] = useState("");
const [isContacted, setIsContacted] = useState(false);
const [contactMethod, setContactMethod] = useState("call");
const [newStageId, setNewStageId] = useState<number | "">("");
const [orderValue, setOrderValue] = useState("");
const [lossReason, setLossReason] = useState("");
const [error, setError] = useState<string | null>(null);
const selectedStage = newStageId
? stages.find((s) => s.id === Number(newStageId))
: null;
const showOrderPrompt =
selectedStage?.stageType === "system_terminal_won";
const showLossPrompt =
selectedStage?.stageType === "system_terminal_lost";
const reset = useCallback(() => {
setContent("");
setIsContacted(false);
setContactMethod("call");
setNewStageId("");
setOrderValue("");
setLossReason("");
setError(null);
}, []);
const handleClose = useCallback(() => {
reset();
onClose();
}, [reset, onClose]);
const addNoteMutation = useMutation({
mutationFn: async (data: AddNoteInput) =>
crmApi.addNote(proId, leadId, data),
onSuccess: () => {
queryClient.invalidateQueries({
queryKey: queryKeys.crm.activities(proId, String(leadId)),
});
queryClient.invalidateQueries({
queryKey: queryKeys.crm.lead(proId, String(leadId)),
});
queryClient.invalidateQueries({
queryKey: queryKeys.crm.kanban(proId),
});
handleClose();
},
onError: (err: Error) => {
setError(err.message || "Failed to add note");
},
});
const handleSubmit = useCallback(
(e: React.FormEvent) => {
e.preventDefault();
setError(null);
if (isContacted && !content.trim()) {
setError("Note is required when marking as contacted");
return;
}
if (!content.trim() && !isContacted && !newStageId) {
setError(
"Please enter a note, mark as contacted, or change the stage",
);
return;
}
const data: AddNoteInput = {};
if (content.trim()) data.content = content.trim();
if (isContacted) {
data.isContacted = true;
data.contactMethod = contactMethod;
}
if (newStageId && Number(newStageId) !== lead.currentStageId) {
data.newStageId = Number(newStageId);
}
addNoteMutation.mutate(data);
},
[
content,
isContacted,
contactMethod,
newStageId,
lead.currentStageId,
addNoteMutation,
],
);
if (!open) return null;
return (
<div
role="dialog"
className="fixed inset-0 z-[60] flex items-center justify-center"
>
<button
type="button"
aria-label="Close"
className="fixed inset-0 bg-black/50 cursor-default"
onClick={handleClose}
tabIndex={-1}
/>
<div className="relative z-10 bg-background-elevated rounded-lg shadow-xl w-full max-w-md mx-4 p-6">
<div className="flex items-center justify-between mb-4">
<h2 className="text-lg font-semibold text-foreground-default">
Add Note
</h2>
<button
type="button"
onClick={handleClose}
className="p-1 text-foreground-subtle hover:text-foreground-default"
>
<X className="h-5 w-5" />
</button>
</div>
<form onSubmit={handleSubmit} className="space-y-4">
{/* Content */}
<div>
<textarea
value={content}
onChange={(e) => setContent(e.target.value)}
maxLength={MAX_NOTE_LENGTH}
rows={3}
className="w-full px-3 py-2 text-sm rounded-md border border-border-default bg-background-elevated text-foreground-default focus:ring-2 focus:ring-primary-500 focus:border-transparent resize-none"
placeholder="Add a note about this lead..."
/>
<div className="text-right text-xs text-foreground-subtle mt-1">
{content.length}/{MAX_NOTE_LENGTH}
</div>
</div>
{/* Options row */}
<div className="flex flex-wrap items-center gap-4">
{/* Contacted checkbox */}
<label className="flex items-center gap-2 text-sm cursor-pointer">
<input
type="checkbox"
checked={isContacted}
onChange={(e) => setIsContacted(e.target.checked)}
className="rounded border-border-default text-primary-500 focus:ring-primary-500"
/>
<span className="text-foreground-default">
Mark as Contacted
</span>
</label>
{/* Contact method */}
{isContacted && (
<select
value={contactMethod}
onChange={(e) => setContactMethod(e.target.value)}
className="h-8 px-2 text-sm rounded border border-border-default bg-background-elevated text-foreground-default focus:ring-2 focus:ring-primary-500"
>
{CONTACT_METHODS.map((m) => (
<option key={m.value} value={m.value}>
{m.label}
</option>
))}
</select>
)}
{/* Stage change */}
<select
value={newStageId}
onChange={(e) =>
setNewStageId(
e.target.value ? Number(e.target.value) : "",
)
}
className="h-8 px-2 text-sm rounded border border-border-default bg-background-elevated text-foreground-default focus:ring-2 focus:ring-primary-500"
>
<option value="">No stage change</option>
{stages
.filter((s) => s.id !== lead.currentStageId)
.map((s) => (
<option key={s.id} value={s.id}>
Move to {s.name}
</option>
))}
</select>
</div>
{/* Won prompt */}
{showOrderPrompt && (
<div className="p-3 rounded-md bg-success/5 border border-success/20">
<label
htmlFor="quick-note-order-value"
className="block text-sm font-medium text-foreground-default mb-1"
>
Order Value (optional)
</label>
<input
id="quick-note-order-value"
type="text"
value={orderValue}
onChange={(e) => setOrderValue(e.target.value)}
placeholder="e.g., 4.2L or 420000"
className="h-8 w-full px-2 text-sm rounded border border-border-default bg-background-elevated text-foreground-default focus:ring-2 focus:ring-primary-500"
/>
</div>
)}
{/* Lost prompt */}
{showLossPrompt && (
<div className="p-3 rounded-md bg-error/5 border border-error/20">
<label
htmlFor="quick-note-loss-reason"
className="block text-sm font-medium text-foreground-default mb-1"
>
Loss Reason (optional)
</label>
<textarea
id="quick-note-loss-reason"
value={lossReason}
onChange={(e) => setLossReason(e.target.value)}
rows={2}
placeholder="Why was this lead lost?"
className="w-full px-2 py-1.5 text-sm rounded border border-border-default bg-background-elevated text-foreground-default focus:ring-2 focus:ring-primary-500 resize-none"
/>
</div>
)}
{/* Error */}
{error && (
<div className="flex items-center gap-2 text-sm text-error">
<AlertCircle className="h-4 w-4 flex-shrink-0" />
{error}
</div>
)}
{/* Actions */}
<div className="flex justify-end gap-3 pt-2">
<Button type="button" variant="outline" onClick={handleClose}>
Cancel
</Button>
<Button type="submit" isLoading={addNoteMutation.isPending}>
Add Note
</Button>
</div>
</form>
</div>
</div>
);
}
|