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 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 | 42x 42x 42x 42x 1x 76x 76x 76x 76x 76x 76x 76x 7x 76x 2x 2x 2x 76x 2x 2x 2x 76x 1x 1x 1x 1x 76x 2x 76x 3x 3x 2x 1x 76x 3x 3x 2x 76x 2x 5x 2x 2x 2x 2x 2x 10x 76x 734x 367x 139x 76x 76x 76x 367x 367x 367x 367x 367x 367x 4x 3x 2x 3x 1x 1x 1x 1x 1x 6x 6x 4x 4x 1x 1x 51x 51x 51x 51x 4x 51x 2x 2x 2x 51x 1x 51x 1x 51x 3x 3x 2x 51x 153x 2x 3x | import { useState, useCallback } from "react";
import { useMutation, useQueryClient } from "@tanstack/react-query";
import { Link, useNavigate } from "@tanstack/react-router";
import { notify } from "../../lib/notify";
import {
ArrowLeft,
Settings,
Plus,
Lock,
Pencil,
Trash2,
ToggleLeft,
ToggleRight,
Check,
X,
ChevronUp,
ChevronDown,
} from "lucide-react";
import { cn } from "../../lib/utils";
import { StickyPageHeader } from "../../components/ui/sticky-page-header";
import { MobileFullPage } from "../../components/ui/mobile-full-page";
import { crmApi } from "../../lib/api";
import type { PipelineStage } from "../../lib/api/pro/crm";
import { queryKeys } from "../../lib/query-keys";
import { usePro } from "../../lib/pro-context";
import {
usePipelineStages,
useLeadSourcesWithInactive,
} from "../../hooks/queries/useCrmQueries";
import { Button } from "../../components/ui/button";
import { ConfirmDialog } from "../../components/ui/confirm-dialog";
// ─── Page ────────────────────────────────────────────────────────────────────
export function CrmSettingsPage() {
const { proId } = usePro();
const navigate = useNavigate();
Iif (!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>
);
}
return (
<MobileFullPage
title="CRM Settings"
onBack={() => navigate({ to: "/crm" })}
>
<div className="space-y-6 max-w-3xl p-4 sm:p-0">
{/* Header — desktop only (MobileFullPage provides the mobile header) */}
<div className="hidden sm:block">
<StickyPageHeader>
<div className="flex items-center gap-3">
<Link
to="/crm"
className="text-foreground-muted hover:text-foreground-default transition-colors"
>
<ArrowLeft className="h-5 w-5" />
</Link>
<h1 className="text-xl font-bold text-foreground-default flex items-center gap-2">
<Settings className="h-5 w-5 text-foreground-subtle" />
CRM Settings
</h1>
</div>
</StickyPageHeader>
</div>
<PipelineConfig proId={proId} />
<SourceConfig proId={proId} />
</div>
</MobileFullPage>
);
}
// ─── Pipeline Configuration ──────────────────────────────────────────────────
function PipelineConfig({ proId }: { proId: string }) {
const queryClient = useQueryClient();
const { data: stages } = usePipelineStages(proId);
const [editingId, setEditingId] = useState<number | null>(null);
const [editName, setEditName] = useState("");
const [newName, setNewName] = useState("");
const [deleteTarget, setDeleteTarget] = useState<PipelineStage | null>(null);
const invalidateStages = useCallback(() => {
queryClient.invalidateQueries({
queryKey: queryKeys.crm.all,
});
}, [queryClient]);
const createMutation = useMutation({
mutationFn: (name: string) => crmApi.createStage(proId, { name }),
onSuccess: () => {
invalidateStages();
setNewName("");
},
onError: (err: Error) => {
notify.error(err.message || "Failed to create stage");
},
});
const renameMutation = useMutation({
mutationFn: ({ stageId, name }: { stageId: number; name: string }) =>
crmApi.updateStage(proId, stageId, { name }),
onSuccess: () => {
invalidateStages();
setEditingId(null);
},
onError: (err: Error) => {
notify.error(err.message || "Failed to rename stage");
},
});
const deleteMutation = useMutation({
mutationFn: (stageId: number) => {
// Reassign to the entry stage
const entry = stages?.find((s) => s.stageType === "system_entry");
return crmApi.deleteStage(proId, stageId, entry?.id);
},
onSuccess: () => {
invalidateStages();
setDeleteTarget(null);
},
onError: (err: Error) => {
notify.error(err.message || "Failed to delete stage");
},
});
const reorderMutation = useMutation({
mutationFn: (stageIds: number[]) => crmApi.reorderStages(proId, stageIds),
onSuccess: invalidateStages,
onError: (err: Error) => {
notify.error(err.message || "Failed to reorder stages");
},
});
const handleRename = useCallback(
(stageId: number) => {
const trimmed = editName.trim();
if (trimmed) {
renameMutation.mutate({ stageId, name: trimmed });
} else {
setEditingId(null);
}
},
[editName, renameMutation],
);
const handleAddStage = useCallback(
(e: React.FormEvent) => {
e.preventDefault();
if (newName.trim()) {
createMutation.mutate(newName.trim());
}
},
[newName, createMutation],
);
const handleMoveStage = useCallback(
(stageId: number, direction: "up" | "down") => {
Iif (!stages) return;
const currentIndex = stages.findIndex((s) => s.id === stageId);
Iif (currentIndex < 0) return;
const newStages = [...stages];
const swapIndex = direction === "up" ? currentIndex - 1 : currentIndex + 1;
Iif (swapIndex < 0 || swapIndex >= newStages.length) return;
// Swap
[newStages[currentIndex], newStages[swapIndex]] = [
newStages[swapIndex],
newStages[currentIndex],
];
reorderMutation.mutate(newStages.map((s) => s.id));
},
[stages, reorderMutation],
);
const isSystemStage = (stage: PipelineStage) =>
stage.stageType === "system_entry" ||
stage.stageType === "system_terminal_won" ||
stage.stageType === "system_terminal_lost";
// Custom (non-system) stages in the current order, used for determining move constraints
const customStages = (stages || []).filter((s) => !isSystemStage(s));
const customStageIds = customStages.map((s) => s.id);
const maxCustomStages = 5;
const atStageLimit = customStages.length >= maxCustomStages;
return (
<div className="rounded-lg border border-border-default bg-background-elevated p-5">
<h2 className="text-sm font-semibold text-foreground-default mb-4">
Pipeline Stages
</h2>
<p className="text-xs text-foreground-subtle mb-4">
Configure the stages leads move through. System stages cannot be deleted.
</p>
{/* Stage list */}
<div className="space-y-1.5 mb-4">
{(stages || []).map((stage) => {
const isSystem = isSystemStage(stage);
const isEditing = editingId === stage.id;
const customIdx = customStageIds.indexOf(stage.id);
const canMoveUp = !isSystem && customIdx > 0;
const canMoveDown =
!isSystem && customIdx < customStageIds.length - 1;
return (
<div
key={stage.id}
className="flex items-center gap-2 p-2 rounded-md border border-border-default bg-background-subtle group"
>
{/* Name or edit input */}
{isEditing ? (
<div className="flex-1 flex items-center gap-1.5">
<input
type="text"
value={editName}
onChange={(e) =>
setEditName(e.target.value)
}
onKeyDown={(e) => {
if (e.key === "Enter")
handleRename(stage.id);
if (e.key === "Escape")
setEditingId(null);
}}
className="flex-1 h-7 px-2 text-sm rounded border border-primary-500 bg-background-elevated text-foreground-default focus:ring-1 focus:ring-primary-500"
/>
<button
type="button"
onClick={() => handleRename(stage.id)}
className="p-1 text-success hover:text-success/80"
>
<Check className="h-3.5 w-3.5" />
</button>
<button
type="button"
onClick={() => setEditingId(null)}
className="p-1 text-foreground-subtle hover:text-foreground-default"
>
<X className="h-3.5 w-3.5" />
</button>
</div>
) : (
<>
<span className="flex-1 text-sm text-foreground-default">
{stage.name}
</span>
{isSystem && (
<Lock className="h-3 w-3 text-foreground-subtle" />
)}
<span className="text-[10px] text-foreground-subtle">
{stage.stageType === "system_entry"
? "Entry"
: stage.stageType ===
"system_terminal_won"
? "Won"
: stage.stageType ===
"system_terminal_lost"
? "Lost"
: ""}
</span>
</>
)}
{/* Actions */}
{!isEditing && (
<div className="flex items-center gap-0.5 opacity-0 group-hover:opacity-100 transition-opacity">
{/* Move up/down for custom stages */}
{!isSystem && (
<>
<button
type="button"
onClick={() => handleMoveStage(stage.id, "up")}
disabled={!canMoveUp || reorderMutation.isPending}
className="p-1 text-foreground-subtle hover:text-foreground-default disabled:opacity-30 disabled:cursor-not-allowed"
title="Move up"
>
<ChevronUp className="h-3.5 w-3.5" />
</button>
<button
type="button"
onClick={() => handleMoveStage(stage.id, "down")}
disabled={!canMoveDown || reorderMutation.isPending}
className="p-1 text-foreground-subtle hover:text-foreground-default disabled:opacity-30 disabled:cursor-not-allowed"
title="Move down"
>
<ChevronDown className="h-3.5 w-3.5" />
</button>
<button
type="button"
onClick={() => {
setEditingId(stage.id);
setEditName(stage.name);
}}
className="p-1 text-foreground-subtle hover:text-primary-600"
title="Rename"
>
<Pencil className="h-3 w-3" />
</button>
<button
type="button"
onClick={() => setDeleteTarget(stage)}
className="p-1 text-foreground-subtle hover:text-error"
title="Delete"
>
<Trash2 className="h-3 w-3" />
</button>
</>
)}
</div>
)}
</div>
);
})}
</div>
{/* Add stage form */}
<form onSubmit={handleAddStage} className="flex items-center gap-2">
<input
type="text"
value={newName}
onChange={(e) => setNewName(e.target.value)}
placeholder="New stage name..."
disabled={atStageLimit}
className="flex-1 h-9 px-3 text-sm rounded-md border border-border-default bg-background-elevated text-foreground-default focus:ring-2 focus:ring-primary-500 disabled:opacity-50 disabled:cursor-not-allowed"
/>
<Button
type="submit"
size="sm"
disabled={!newName.trim() || atStageLimit}
isLoading={createMutation.isPending}
>
<Plus className="h-3.5 w-3.5" />
Add Stage
</Button>
</form>
{atStageLimit && (
<p className="text-xs text-foreground-subtle mt-2">
Maximum {maxCustomStages} custom stages reached. Delete a stage to add a new one.
</p>
)}
{/* Delete confirmation */}
<ConfirmDialog
open={!!deleteTarget}
title="Delete Stage"
description={`Delete "${deleteTarget?.name}"? Leads in this stage will be moved to the entry stage.`}
confirmLabel="Delete"
variant="destructive"
onConfirm={() =>
deleteTarget && deleteMutation.mutate(deleteTarget.id)
}
onCancel={() => setDeleteTarget(null)}
/>
</div>
);
}
// ─── Source Configuration ────────────────────────────────────────────────────
function SourceConfig({ proId }: { proId: string }) {
const queryClient = useQueryClient();
const { data: sources } = useLeadSourcesWithInactive(proId);
const [newName, setNewName] = useState("");
const invalidateSources = useCallback(() => {
queryClient.invalidateQueries({
queryKey: queryKeys.crm.sources(proId),
});
}, [queryClient, proId]);
const createMutation = useMutation({
mutationFn: (name: string) => crmApi.createSource(proId, { name }),
onSuccess: () => {
invalidateSources();
setNewName("");
},
onError: (err: Error) => {
notify.error(err.message || "Failed to create source");
},
});
const deactivateMutation = useMutation({
mutationFn: (sourceId: number) =>
crmApi.deactivateSource(proId, sourceId),
onSuccess: invalidateSources,
onError: (err: Error) => {
notify.error(err.message || "Failed to deactivate source");
},
});
const reactivateMutation = useMutation({
mutationFn: (sourceId: number) =>
crmApi.reactivateSource(proId, sourceId),
onSuccess: invalidateSources,
onError: (err: Error) => {
notify.error(err.message || "Failed to reactivate source");
},
});
const handleAdd = useCallback(
(e: React.FormEvent) => {
e.preventDefault();
if (newName.trim()) {
createMutation.mutate(newName.trim());
}
},
[newName, createMutation],
);
return (
<div className="rounded-lg border border-border-default bg-background-elevated p-5">
<h2 className="text-sm font-semibold text-foreground-default mb-4">
Lead Sources
</h2>
<p className="text-xs text-foreground-subtle mb-4">
Track where your leads come from. System sources cannot be deactivated.
</p>
<div className="space-y-1.5 mb-4">
{(sources || []).map((source) => (
<div
key={source.id}
className="flex items-center gap-3 p-2 rounded-md border border-border-default bg-background-subtle"
>
<span
className={cn(
"flex-1 text-sm",
source.isActive
? "text-foreground-default"
: "text-foreground-subtle line-through",
)}
>
{source.name}
</span>
{source.isSystem ? (
<Lock className="h-3 w-3 text-foreground-subtle" />
) : (
<button
type="button"
onClick={() =>
source.isActive
? deactivateMutation.mutate(source.id)
: reactivateMutation.mutate(source.id)
}
className="flex items-center gap-1 text-xs"
title={
source.isActive ? "Deactivate" : "Reactivate"
}
>
{source.isActive ? (
<ToggleRight className="h-5 w-5 text-success" />
) : (
<ToggleLeft className="h-5 w-5 text-foreground-subtle" />
)}
</button>
)}
</div>
))}
</div>
{/* Add source form */}
<form onSubmit={handleAdd} className="flex items-center gap-2">
<input
type="text"
value={newName}
onChange={(e) => setNewName(e.target.value)}
placeholder="New source name..."
className="flex-1 h-9 px-3 text-sm rounded-md border border-border-default bg-background-elevated text-foreground-default focus:ring-2 focus:ring-primary-500"
/>
<Button
type="submit"
size="sm"
disabled={!newName.trim()}
isLoading={createMutation.isPending}
>
<Plus className="h-3.5 w-3.5" />
Add Source
</Button>
</form>
</div>
);
}
|