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 | 2x 2x 38x 38x 38x 38x 38x 38x 38x 38x 38x 11x 38x 38x 38x 5x 5x 5x 5x 5x 5x 5x 38x 5x 5x 38x 4x 3x 3x 3x 3x 1x 38x 6x 6x 6x 1x 1x 5x 6x 1x 1x 4x 4x 4x 1x 1x 4x 1x 4x 38x 37x 3x 3x 3x 139x 102x | 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";
import { Checkbox } from "../ui/checkbox";
import { Field, Label } from "../ui/field";
import { Input } from "../ui/input";
import { Select } from "../ui/select";
import { Textarea } from "../ui/textarea";
// ─── 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[];
/**
* Whether this member may move the lead. Staff may POST a note — it is the
* one lead route on the server's write allowlist — but the note endpoint
* also accepts `newStageId`, so the one allowed route can reach the one
* capability staff do not have. Defaults to true; the Leads page passes
* `leadWriteScope(proRole).canChangeStage`.
*/
canChangeStage?: boolean;
open: boolean;
onClose: () => void;
};
export function QuickNoteDialog({
leadId,
proId,
lead,
stages,
canChangeStage = true,
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 =
canChangeStage && 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;
}
const wantsStageChange = canChangeStage && !!newStageId;
if (!content.trim() && !isContacted && !wantsStageChange) {
setError(
canChangeStage
? "Please enter a note, mark as contacted, or change the stage"
: "Please enter a note or mark as contacted",
);
return;
}
const data: AddNoteInput = {};
if (content.trim()) data.content = content.trim();
if (isContacted) {
data.isContacted = true;
data.contactMethod = contactMethod;
}
// Never sent when the member may not move the lead: the API would
// accept it through the one allowlisted route, and the UI must not
// offer what the write scope denies.
if (wantsStageChange && Number(newStageId) !== lead.currentStageId) {
data.newStageId = Number(newStageId);
}
addNoteMutation.mutate(data);
},
[
content,
isContacted,
contactMethod,
newStageId,
canChangeStage,
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 cursor-default bg-overlay-dark"
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}
resize="none"
aria-label="Note"
placeholder="Add a note about this lead..."
/>
<p className="mt-1 text-right text-xs tabular-nums text-foreground-subtle">
{content.length}/{MAX_NOTE_LENGTH}
</p>
</div>
{/* Options row */}
<div className="flex flex-wrap items-center gap-3">
<Checkbox
checked={isContacted}
onChange={(e) => setIsContacted(e.target.checked)}
>
Mark as contacted
</Checkbox>
{isContacted && (
<Select
tier="dense"
aria-label="Contact method"
value={contactMethod}
onChange={(e) => setContactMethod(e.target.value)}
options={CONTACT_METHODS}
className="w-auto"
/>
)}
{/* Stage change — hidden entirely outside the write scope. */}
{canChangeStage && (
<Select
tier="dense"
aria-label="Move to stage"
value={newStageId}
onChange={(e) =>
setNewStageId(e.target.value ? Number(e.target.value) : "")
}
placeholder="No stage change"
options={stages
.filter((s) => s.id !== lead.currentStageId)
.map((s) => ({
value: String(s.id),
label: `Move to ${s.name}`,
}))}
className="w-auto"
/>
)}
</div>
{/* Won prompt */}
{showOrderPrompt && (
<Field controlId="quick-note-order-value" className="rounded-md bg-success-light p-3">
<Label optional>Order value</Label>
{/*
* `id` is passed explicitly: `Input` predates
* `useFieldControl`, so it does not read the `Field`
* context the way `Textarea` and `Select` do, and the
* Label's `htmlFor` would otherwise point at nothing.
*/}
<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"
/>
</Field>
)}
{/* Lost prompt */}
{showLossPrompt && (
<Field controlId="quick-note-loss-reason" className="rounded-md bg-error-light p-3">
<Label optional>Loss reason</Label>
<Textarea
value={lossReason}
onChange={(e) => setLossReason(e.target.value)}
rows={2}
resize="none"
placeholder="Why was this lead lost?"
/>
</Field>
)}
{/* 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>
);
}
|