@fayz-ai/plugin-crm 0.35.1 → 0.37.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (40) hide show
  1. package/dist/{CrmPage-Z5OYCBKA.js → CrmPage-H3FQDF53.js} +92 -127
  2. package/dist/CrmPage-H3FQDF53.js.map +1 -0
  3. package/dist/SegmentsView-JTFU6XWD.js +514 -0
  4. package/dist/SegmentsView-JTFU6XWD.js.map +1 -0
  5. package/dist/chunk-EHGAPCZF.js +264 -0
  6. package/dist/chunk-EHGAPCZF.js.map +1 -0
  7. package/dist/{chunk-LDBCC67C.js → chunk-J6LYFSFJ.js} +24 -130
  8. package/dist/chunk-J6LYFSFJ.js.map +1 -0
  9. package/dist/chunk-VFLS65NL.js +51 -0
  10. package/dist/chunk-VFLS65NL.js.map +1 -0
  11. package/dist/components/SegmentEditorDialog.d.ts.map +1 -1
  12. package/dist/data/mock.d.ts.map +1 -1
  13. package/dist/data/supabase.d.ts.map +1 -1
  14. package/dist/data/types.d.ts +7 -1
  15. package/dist/data/types.d.ts.map +1 -1
  16. package/dist/index.d.ts +4 -0
  17. package/dist/index.d.ts.map +1 -1
  18. package/dist/index.js +212 -20
  19. package/dist/index.js.map +1 -1
  20. package/dist/lib/segments.d.ts +13 -6
  21. package/dist/lib/segments.d.ts.map +1 -1
  22. package/dist/locales/en.d.ts.map +1 -1
  23. package/dist/locales/pt-BR.d.ts.map +1 -1
  24. package/dist/migrations/index.d.ts +3 -0
  25. package/dist/migrations/index.d.ts.map +1 -1
  26. package/dist/migrations/main.browser.js +4 -1
  27. package/dist/migrations/main.browser.js.map +1 -1
  28. package/dist/migrations/main.js +428 -2
  29. package/dist/migrations/main.js.map +1 -1
  30. package/dist/types.d.ts +29 -9
  31. package/dist/types.d.ts.map +1 -1
  32. package/dist/views/DealListView.d.ts.map +1 -1
  33. package/dist/views/SegmentsView.d.ts.map +1 -1
  34. package/package.json +3 -3
  35. package/dist/CrmPage-Z5OYCBKA.js.map +0 -1
  36. package/dist/SegmentsView-PHLN2MOW.js +0 -385
  37. package/dist/SegmentsView-PHLN2MOW.js.map +0 -1
  38. package/dist/chunk-LDBCC67C.js.map +0 -1
  39. package/dist/chunk-QJOLAGGN.js +0 -129
  40. package/dist/chunk-QJOLAGGN.js.map +0 -1
@@ -0,0 +1,514 @@
1
+ import { useCrmUnitFilter } from './chunk-VFLS65NL.js';
2
+ import { allClauses, audienceOf, describeRule, loadTeamOrNull, VALUELESS_OPS, resolveSourceView, fieldsFor, segmentFieldFor, DAY_OPS, humanizeTag } from './chunk-EHGAPCZF.js';
3
+ import { useCrmProvider, useCrmStore } from './chunk-7V46TYYX.js';
4
+ import React from 'react';
5
+ import { Plus, Filter, Lock, Pencil, Trash2, Briefcase, UserRound, Loader2, Users } from 'lucide-react';
6
+ import { SubpageHeader, Button, TooltipProvider, Tooltip, TooltipTrigger, TooltipContent, Switch, ConfirmDialog, toast, Modal, ModalContent, ModalHeader, ModalTitle, ModalDescription, ModalBody, Input, Select, SelectTrigger, SelectValue, SelectContent, SelectItem, MultiSelect, ModalFooter } from '@fayz-ai/ui';
7
+ import { usePermissionOptional, PageActions } from '@fayz-ai/admin';
8
+ import { useTranslation } from '@fayz-ai/core';
9
+ import { jsxs, jsx, Fragment } from 'react/jsx-runtime';
10
+
11
+ var GROUPS = ["where", "any", "none"];
12
+ function emptyRule(audience) {
13
+ return audience === "deals" ? { column: "stage_name", op: "in", value: [] } : { column: "kind", op: "=", value: "lead" };
14
+ }
15
+ function SegmentEditorDialog({ open, segment, provider, onClose, onSaved }) {
16
+ const t = useTranslation();
17
+ const tagDefs = useCrmStore((s) => s.tagDefs);
18
+ const fetchTagDefs = useCrmStore((s) => s.fetchTagDefs);
19
+ const unitFilter = useCrmUnitFilter();
20
+ const [name, setName] = React.useState("");
21
+ const [description, setDescription] = React.useState("");
22
+ const [isActive, setIsActive] = React.useState(true);
23
+ const [audience, setAudience] = React.useState("people");
24
+ const [rules, setRules] = React.useState({ where: [] });
25
+ const [saving, setSaving] = React.useState(false);
26
+ const [error, setError] = React.useState(null);
27
+ const [preview, setPreview] = React.useState(null);
28
+ const [previewing, setPreviewing] = React.useState(false);
29
+ const [stageNames, setStageNames] = React.useState([]);
30
+ const [team, setTeam] = React.useState([]);
31
+ const [rows, setRows] = React.useState(null);
32
+ const [showRows, setShowRows] = React.useState(false);
33
+ React.useEffect(() => {
34
+ if (!open) return;
35
+ const aud = audienceOf(segment);
36
+ setName(segment?.name ?? "");
37
+ setDescription(segment?.description ?? "");
38
+ setIsActive(segment?.isActive ?? true);
39
+ setAudience(aud);
40
+ setRules(segment?.rules && (segment.rules.where.length || segment.rules.any?.length || segment.rules.none?.length) ? { where: segment.rules.where, any: segment.rules.any ?? [], none: segment.rules.none ?? [] } : { where: [emptyRule(aud)], any: [], none: [] });
41
+ setError(null);
42
+ setPreview(null);
43
+ }, [open, segment]);
44
+ React.useEffect(() => {
45
+ if (open) void fetchTagDefs();
46
+ }, [open, fetchTagDefs]);
47
+ React.useEffect(() => {
48
+ if (!open) return;
49
+ let alive = true;
50
+ void (async () => {
51
+ try {
52
+ const pipelines = await provider.getPipelines();
53
+ const all = await Promise.all(pipelines.map((p) => provider.getPipelineStages(p.id)));
54
+ if (alive) setStageNames([...new Set(all.flat().map((s) => s.name))]);
55
+ } catch {
56
+ }
57
+ const members = await loadTeamOrNull().catch(() => null);
58
+ if (alive) setTeam(members ?? []);
59
+ })();
60
+ return () => {
61
+ alive = false;
62
+ };
63
+ }, [open, provider]);
64
+ React.useEffect(() => {
65
+ if (!open) return;
66
+ const clauses = [...rules.where, ...rules.any ?? [], ...rules.none ?? []];
67
+ if (clauses.some((r) => !VALUELESS_OPS.includes(r.op) && (r.value === "" || Array.isArray(r.value) && r.value.length === 0))) {
68
+ setPreview(null);
69
+ setRows(null);
70
+ return;
71
+ }
72
+ let alive = true;
73
+ setPreviewing(true);
74
+ const timer = window.setTimeout(() => {
75
+ provider.previewSegment(resolveSourceView(rules, audience), rules).then((p) => {
76
+ if (alive) setPreview(p);
77
+ }).then(() => provider.previewSegmentRows(resolveSourceView(rules, audience), rules)).then((r) => {
78
+ if (alive) setRows(r);
79
+ }).catch(() => {
80
+ if (alive) setPreview(null);
81
+ }).finally(() => {
82
+ if (alive) setPreviewing(false);
83
+ });
84
+ }, 400);
85
+ return () => {
86
+ alive = false;
87
+ window.clearTimeout(timer);
88
+ };
89
+ }, [open, rules, audience, provider]);
90
+ function patch(group, i, next) {
91
+ setRules((prev) => ({ ...prev, [group]: (prev[group] ?? []).map((r, j) => j === i ? { ...r, ...next } : r) }));
92
+ }
93
+ function remove(group, i) {
94
+ setRules((prev) => ({ ...prev, [group]: (prev[group] ?? []).filter((_, j) => j !== i) }));
95
+ }
96
+ function add(group) {
97
+ setRules((prev) => ({ ...prev, [group]: [...prev[group] ?? [], emptyRule(audience)] }));
98
+ }
99
+ function changeAudience(next) {
100
+ if (next === audience) return;
101
+ setAudience(next);
102
+ setRules({ where: [emptyRule(next)], any: [], none: [] });
103
+ }
104
+ function changeColumn(group, i, column) {
105
+ const field = segmentFieldFor(column, audience);
106
+ const op = field?.ops[0] ?? "=";
107
+ patch(group, i, { column, op, value: defaultValueFor(op, field) });
108
+ }
109
+ function optionsFor(field) {
110
+ switch (field?.type) {
111
+ case "kind":
112
+ return ["lead", "customer"].map((k) => ({ value: k, label: t(`crm.segments.kind.${k}`) }));
113
+ case "stage":
114
+ return stageNames.map((n) => ({ value: n, label: n }));
115
+ case "dealStatus":
116
+ return ["open", "won", "lost", "archived"].map((s) => ({ value: s, label: t(`crm.segments.dealStatus.${s}`) }));
117
+ case "unit":
118
+ return unitFilter.options.map((u) => ({ value: u.id, label: u.name }));
119
+ case "owner":
120
+ return team.map((m) => ({ value: m.userId, label: m.name }));
121
+ case "tags":
122
+ return [...tagDefs.values()].filter((d) => d.isActive).sort((a, b) => a.name.localeCompare(b.name)).map((d) => ({ value: d.slug, label: d.name || humanizeTag(d.slug) }));
123
+ default:
124
+ return null;
125
+ }
126
+ }
127
+ async function save() {
128
+ if (!name.trim()) {
129
+ setError(t("crm.segments.error.nameRequired"));
130
+ return;
131
+ }
132
+ setSaving(true);
133
+ setError(null);
134
+ try {
135
+ const clean = (list) => (list ?? []).filter((r) => r.column);
136
+ const out = { where: clean(rules.where) };
137
+ if (clean(rules.any).length) out.any = clean(rules.any);
138
+ if (clean(rules.none).length) out.none = clean(rules.none);
139
+ const input = {
140
+ id: segment?.id,
141
+ name: name.trim(),
142
+ description: description.trim() || void 0,
143
+ kind: "rule",
144
+ isActive,
145
+ rules: out,
146
+ sourceView: resolveSourceView(out, audience)
147
+ };
148
+ await provider.saveSegment(input);
149
+ onSaved();
150
+ onClose();
151
+ } catch (e) {
152
+ setError(e?.message ?? t("crm.segments.error.saveFailed"));
153
+ } finally {
154
+ setSaving(false);
155
+ }
156
+ }
157
+ const fields = fieldsFor(audience);
158
+ return /* @__PURE__ */ jsx(Modal, { open, onOpenChange: (v) => {
159
+ if (!v) onClose();
160
+ }, children: /* @__PURE__ */ jsxs(ModalContent, { className: "sm:max-w-3xl", children: [
161
+ /* @__PURE__ */ jsxs(ModalHeader, { children: [
162
+ /* @__PURE__ */ jsx(ModalTitle, { children: segment ? t("crm.segments.editTitle") : t("crm.segments.newTitle") }),
163
+ /* @__PURE__ */ jsx(ModalDescription, { children: t("crm.segments.editorDescriptionGroups") })
164
+ ] }),
165
+ /* @__PURE__ */ jsxs(ModalBody, { className: "space-y-4", children: [
166
+ /* @__PURE__ */ jsxs("div", { className: "grid gap-3 sm:grid-cols-2", children: [
167
+ /* @__PURE__ */ jsxs("div", { children: [
168
+ /* @__PURE__ */ jsx("label", { className: "text-xs font-medium", children: t("crm.segments.name") }),
169
+ /* @__PURE__ */ jsx(
170
+ Input,
171
+ {
172
+ value: name,
173
+ onChange: (e) => setName(e.target.value),
174
+ placeholder: t("crm.segments.namePlaceholder")
175
+ }
176
+ )
177
+ ] }),
178
+ /* @__PURE__ */ jsxs("div", { children: [
179
+ /* @__PURE__ */ jsx("label", { className: "text-xs font-medium", children: t("crm.segments.description") }),
180
+ /* @__PURE__ */ jsx(
181
+ Input,
182
+ {
183
+ value: description,
184
+ onChange: (e) => setDescription(e.target.value),
185
+ placeholder: t("crm.segments.descriptionPlaceholder")
186
+ }
187
+ )
188
+ ] })
189
+ ] }),
190
+ /* @__PURE__ */ jsxs("div", { children: [
191
+ /* @__PURE__ */ jsx("p", { className: "mb-1.5 text-xs font-medium", children: t("crm.segments.audience.label") }),
192
+ /* @__PURE__ */ jsx("div", { className: "inline-flex rounded-full border bg-muted/40 p-0.5", children: ["people", "deals"].map((a) => /* @__PURE__ */ jsxs(
193
+ Button,
194
+ {
195
+ type: "button",
196
+ variant: "bare",
197
+ size: "bare",
198
+ "aria-pressed": audience === a,
199
+ onClick: () => changeAudience(a),
200
+ className: `inline-flex items-center gap-1.5 rounded-full px-3 py-1 text-xs ${audience === a ? "bg-card font-medium text-foreground shadow-sm" : "text-muted-foreground"}`,
201
+ children: [
202
+ a === "deals" ? /* @__PURE__ */ jsx(Briefcase, { className: "h-3.5 w-3.5" }) : /* @__PURE__ */ jsx(UserRound, { className: "h-3.5 w-3.5" }),
203
+ t(`crm.segments.audience.${a}`)
204
+ ]
205
+ },
206
+ a
207
+ )) }),
208
+ /* @__PURE__ */ jsx("p", { className: "mt-1 text-[11px] text-muted-foreground", children: t(`crm.segments.audience.${audience}Hint`) })
209
+ ] }),
210
+ GROUPS.map((group) => {
211
+ const list = rules[group] ?? [];
212
+ return /* @__PURE__ */ jsxs("div", { className: `rounded-card border p-3 ${group === "none" ? "bg-destructive-soft/30" : group === "any" ? "bg-info-soft/30" : "bg-muted/20"}`, children: [
213
+ /* @__PURE__ */ jsxs("div", { className: "mb-2 flex items-center justify-between gap-2", children: [
214
+ /* @__PURE__ */ jsx("p", { className: "text-xs font-medium", children: t(`crm.segments.group.${group}`) }),
215
+ /* @__PURE__ */ jsx("p", { className: "text-[10px] text-muted-foreground", children: t(`crm.segments.group.${group}Hint`) })
216
+ ] }),
217
+ /* @__PURE__ */ jsx("div", { className: "space-y-2", children: list.map((rule, i) => {
218
+ const field = segmentFieldFor(rule.column, audience);
219
+ const needsValue = !VALUELESS_OPS.includes(rule.op);
220
+ const isDays = DAY_OPS.includes(rule.op);
221
+ const isNumber = field?.type === "number";
222
+ const options = optionsFor(field);
223
+ const multi = rule.op === "in" || rule.op === "not_in" || rule.op === "has_any_tag";
224
+ return /* @__PURE__ */ jsxs("div", { className: "flex flex-wrap items-center gap-2", children: [
225
+ /* @__PURE__ */ jsxs(Select, { value: rule.column, onValueChange: (v) => changeColumn(group, i, v), children: [
226
+ /* @__PURE__ */ jsx(SelectTrigger, { className: "h-8 w-[160px] text-xs", children: /* @__PURE__ */ jsx(SelectValue, {}) }),
227
+ /* @__PURE__ */ jsx(SelectContent, { children: fields.map((f) => /* @__PURE__ */ jsx(SelectItem, { value: f.column, children: t(f.labelKey) }, f.column)) })
228
+ ] }),
229
+ /* @__PURE__ */ jsxs(Select, { value: rule.op, onValueChange: (v) => patch(group, i, { op: v, value: defaultValueFor(v, field) }), children: [
230
+ /* @__PURE__ */ jsx(SelectTrigger, { className: "h-8 w-[170px] text-xs", children: /* @__PURE__ */ jsx(SelectValue, {}) }),
231
+ /* @__PURE__ */ jsx(SelectContent, { children: (field?.ops ?? []).map((op) => /* @__PURE__ */ jsx(SelectItem, { value: op, children: t(`crm.segments.op.${op}`) }, op)) })
232
+ ] }),
233
+ needsValue && options && multi && /* @__PURE__ */ jsx(
234
+ MultiSelect,
235
+ {
236
+ className: "h-8 min-w-[200px] text-xs",
237
+ options,
238
+ value: Array.isArray(rule.value) ? rule.value.map(String) : [],
239
+ onChange: (v) => patch(group, i, { value: v }),
240
+ allLabel: t("crm.segments.pick"),
241
+ searchPlaceholder: t("crm.segments.search"),
242
+ emptyLabel: t("crm.segments.noOptions")
243
+ }
244
+ ),
245
+ needsValue && options && !multi && /* @__PURE__ */ jsxs(Select, { value: String(rule.value ?? ""), onValueChange: (v) => patch(group, i, { value: v }), children: [
246
+ /* @__PURE__ */ jsx(SelectTrigger, { className: "h-8 w-[200px] text-xs", children: /* @__PURE__ */ jsx(SelectValue, { placeholder: t("crm.segments.pick") }) }),
247
+ /* @__PURE__ */ jsx(SelectContent, { children: options.map((o) => /* @__PURE__ */ jsx(SelectItem, { value: o.value, children: o.label }, o.value)) })
248
+ ] }),
249
+ needsValue && !options && /* @__PURE__ */ jsx(
250
+ Input,
251
+ {
252
+ className: "h-8 w-[160px] text-xs",
253
+ type: isDays || isNumber ? "number" : "text",
254
+ value: Array.isArray(rule.value) ? rule.value.join(", ") : String(rule.value ?? ""),
255
+ onChange: (e) => patch(group, i, {
256
+ value: multi ? e.target.value.split(",").map((x) => x.trim()).filter(Boolean) : isDays || isNumber ? Number(e.target.value) : e.target.value
257
+ }),
258
+ placeholder: isDays ? t("crm.segments.daysPlaceholder") : t("crm.segments.valuePlaceholder")
259
+ }
260
+ ),
261
+ isDays && /* @__PURE__ */ jsx("span", { className: "text-[10px] text-muted-foreground", children: t("crm.segments.days") }),
262
+ /* @__PURE__ */ jsx(
263
+ Button,
264
+ {
265
+ variant: "ghost",
266
+ size: "icon",
267
+ className: "h-8 w-8",
268
+ disabled: group === "where" && list.length === 1 && !(rules.any?.length || rules.none?.length),
269
+ onClick: () => remove(group, i),
270
+ children: /* @__PURE__ */ jsx(Trash2, { className: "h-3.5 w-3.5" })
271
+ }
272
+ )
273
+ ] }, i);
274
+ }) }),
275
+ /* @__PURE__ */ jsxs(Button, { variant: "outline", size: "sm", className: "mt-2", onClick: () => add(group), children: [
276
+ /* @__PURE__ */ jsx(Plus, { className: "h-3.5 w-3.5" }),
277
+ " ",
278
+ t("crm.segments.addRule")
279
+ ] })
280
+ ] }, group);
281
+ }),
282
+ /* @__PURE__ */ jsxs("div", { className: "rounded-card border bg-card", children: [
283
+ /* @__PURE__ */ jsxs("div", { className: "flex items-center gap-2 px-3 py-2", children: [
284
+ previewing ? /* @__PURE__ */ jsx(Loader2, { className: "h-4 w-4 animate-spin text-muted-foreground" }) : /* @__PURE__ */ jsx(Users, { className: "h-4 w-4 text-muted-foreground" }),
285
+ /* @__PURE__ */ jsx("p", { className: "text-sm", children: preview === null ? /* @__PURE__ */ jsx("span", { className: "text-muted-foreground", children: t("crm.segments.previewIncomplete") }) : audience === "deals" ? t("crm.segments.previewDeals", { deals: String(preview.rows), people: String(preview.people) }) : t("crm.segments.previewPeople", { count: String(preview.people) }) }),
286
+ rows && rows.length > 0 && /* @__PURE__ */ jsx(Button, { variant: "ghost", size: "sm", className: "ml-auto h-7 text-xs", onClick: () => setShowRows((v) => !v), children: showRows ? t("crm.segments.hideList") : t("crm.segments.showList") })
287
+ ] }),
288
+ showRows && rows && /* @__PURE__ */ jsxs("div", { className: "max-h-72 overflow-auto border-t", children: [
289
+ rows.map((r) => /* @__PURE__ */ jsxs("div", { className: "flex items-center gap-2 border-b px-3 py-1.5 text-xs last:border-b-0", children: [
290
+ /* @__PURE__ */ jsxs("span", { className: "min-w-0 flex-1", children: [
291
+ /* @__PURE__ */ jsx("span", { className: "block truncate font-medium", children: r.title }),
292
+ r.subtitle && /* @__PURE__ */ jsx("span", { className: "block truncate text-[11px] text-muted-foreground", children: r.subtitle })
293
+ ] }),
294
+ r.stage && /* @__PURE__ */ jsxs("span", { className: "inline-flex shrink-0 items-center gap-1 text-[11px] text-muted-foreground", children: [
295
+ /* @__PURE__ */ jsx("span", { className: "h-1.5 w-1.5 rounded-full", style: { backgroundColor: r.stageColor ?? "#94A3B8" } }),
296
+ r.stage
297
+ ] })
298
+ ] }, r.id)),
299
+ preview && rows.length < preview.rows && /* @__PURE__ */ jsx("p", { className: "px-3 py-1.5 text-[11px] text-muted-foreground", children: t("crm.segments.listCapped", { shown: String(rows.length) }) })
300
+ ] })
301
+ ] }),
302
+ /* @__PURE__ */ jsxs("div", { className: "flex items-center gap-2", children: [
303
+ /* @__PURE__ */ jsx(Switch, { checked: isActive, onCheckedChange: setIsActive }),
304
+ /* @__PURE__ */ jsx("span", { className: "text-xs", children: t("crm.segments.active") })
305
+ ] }),
306
+ error && /* @__PURE__ */ jsx("p", { className: "text-xs text-destructive", children: error })
307
+ ] }),
308
+ /* @__PURE__ */ jsxs(ModalFooter, { children: [
309
+ /* @__PURE__ */ jsx(Button, { variant: "ghost", onClick: onClose, children: t("common.cancel") }),
310
+ /* @__PURE__ */ jsxs(Button, { onClick: () => void save(), disabled: saving, children: [
311
+ saving && /* @__PURE__ */ jsx(Loader2, { className: "h-3.5 w-3.5 animate-spin" }),
312
+ t("common.save")
313
+ ] })
314
+ ] })
315
+ ] }) });
316
+ }
317
+ function defaultValueFor(op, field) {
318
+ if (VALUELESS_OPS.includes(op)) return void 0;
319
+ if (DAY_OPS.includes(op)) return 30;
320
+ if (op === "in" || op === "not_in" || op === "has_any_tag") return [];
321
+ if (field?.type === "number") return 0;
322
+ return "";
323
+ }
324
+ function SegmentSkeleton() {
325
+ return /* @__PURE__ */ jsx("div", { className: "space-y-3", children: [1, 2, 3].map((i) => /* @__PURE__ */ jsxs("div", { className: "rounded-card border bg-card p-4 shadow-sm", children: [
326
+ /* @__PURE__ */ jsx("div", { className: "h-4 w-40 animate-pulse rounded bg-muted/40" }),
327
+ /* @__PURE__ */ jsx("div", { className: "mt-2 h-3 w-64 animate-pulse rounded bg-muted/20" })
328
+ ] }, i)) });
329
+ }
330
+ function SegmentsView() {
331
+ const provider = useCrmProvider();
332
+ const t = useTranslation();
333
+ const can = usePermissionOptional();
334
+ const canEdit = can("sales", "edit");
335
+ const canDelete = can("sales", "delete");
336
+ const [segments, setSegments] = React.useState(null);
337
+ const [counts, setCounts] = React.useState({});
338
+ const [editing, setEditing] = React.useState(void 0);
339
+ const [removing, setRemoving] = React.useState(null);
340
+ const load = React.useCallback(async () => {
341
+ const list = await provider.getSegments();
342
+ setSegments(list);
343
+ try {
344
+ const cs = await provider.getSegmentCounts();
345
+ setCounts(Object.fromEntries(cs.map((c) => [c.segmentId, c.count])));
346
+ } catch {
347
+ setCounts({});
348
+ }
349
+ }, [provider]);
350
+ React.useEffect(() => {
351
+ void load();
352
+ }, [load]);
353
+ async function toggleActive(segment, next) {
354
+ try {
355
+ await provider.saveSegment({
356
+ id: segment.id,
357
+ name: segment.name,
358
+ description: segment.description,
359
+ kind: segment.kind,
360
+ rules: segment.rules,
361
+ isActive: next
362
+ });
363
+ await load();
364
+ } catch (e) {
365
+ toast.error(e?.message ?? t("crm.segments.error.toggleFailed"));
366
+ }
367
+ }
368
+ async function remove(segment) {
369
+ try {
370
+ await provider.deleteSegment(segment.id);
371
+ setRemoving(null);
372
+ await load();
373
+ } catch (e) {
374
+ toast.error(e?.message ?? t("crm.segments.error.deleteFailed"));
375
+ }
376
+ }
377
+ return /* @__PURE__ */ jsxs("div", { className: "space-y-4", children: [
378
+ /* @__PURE__ */ jsx(
379
+ SubpageHeader,
380
+ {
381
+ title: t("crm.segments.title"),
382
+ subtitle: t("crm.segments.subtitle"),
383
+ actions: /* @__PURE__ */ jsx(
384
+ PageActions,
385
+ {
386
+ actions: [{
387
+ id: "new-segment",
388
+ label: t("crm.segments.new"),
389
+ icon: Plus,
390
+ intent: "primary",
391
+ permission: { feature: "sales", action: "create" },
392
+ onSelect: () => setEditing(null)
393
+ }]
394
+ }
395
+ )
396
+ }
397
+ ),
398
+ segments === null ? /* @__PURE__ */ jsx(SegmentSkeleton, {}) : segments.length === 0 ? /* @__PURE__ */ jsxs("div", { className: "rounded-card border border-dashed bg-card px-6 py-10 text-center", children: [
399
+ /* @__PURE__ */ jsx(Filter, { className: "mx-auto mb-3 h-8 w-8 text-muted-foreground/50" }),
400
+ /* @__PURE__ */ jsx("p", { className: "text-sm font-medium", children: t("crm.segments.emptyTitle") }),
401
+ /* @__PURE__ */ jsx("p", { className: "mx-auto mt-1 max-w-md text-xs text-muted-foreground", children: t("crm.segments.emptyHint") }),
402
+ /* @__PURE__ */ jsxs(Button, { className: "mt-4", size: "sm", onClick: () => setEditing(null), children: [
403
+ /* @__PURE__ */ jsx(Plus, { className: "h-3.5 w-3.5" }),
404
+ " ",
405
+ t("crm.segments.new")
406
+ ] })
407
+ ] }) : /* @__PURE__ */ jsx("div", { className: "space-y-3", "data-testid": "segment-list", children: segments.map((segment) => {
408
+ const count = counts[segment.id];
409
+ return /* @__PURE__ */ jsx(
410
+ "div",
411
+ {
412
+ "data-testid": "segment-row",
413
+ className: `rounded-card border bg-card p-4 shadow-sm ${segment.isActive ? "" : "opacity-60"}`,
414
+ children: /* @__PURE__ */ jsxs("div", { className: "flex flex-wrap items-start justify-between gap-3", children: [
415
+ /* @__PURE__ */ jsxs("div", { className: "min-w-0 flex-1", children: [
416
+ /* @__PURE__ */ jsxs("div", { className: "flex flex-wrap items-center gap-2", children: [
417
+ segment.color && /* @__PURE__ */ jsx("span", { className: "h-2.5 w-2.5 shrink-0 rounded-full", style: { backgroundColor: segment.color } }),
418
+ /* @__PURE__ */ jsx("h3", { className: "text-sm font-semibold", children: segment.name }),
419
+ segment.isSystem && /* @__PURE__ */ jsx(TooltipProvider, { children: /* @__PURE__ */ jsxs(Tooltip, { children: [
420
+ /* @__PURE__ */ jsx(TooltipTrigger, { asChild: true, children: /* @__PURE__ */ jsxs("span", { className: "inline-flex items-center gap-1 rounded-full bg-muted px-2 py-0.5 text-[10px] text-muted-foreground", children: [
421
+ /* @__PURE__ */ jsx(Lock, { className: "h-2.5 w-2.5" }),
422
+ " ",
423
+ t("crm.segments.systemBadge")
424
+ ] }) }),
425
+ /* @__PURE__ */ jsx(TooltipContent, { children: t("crm.segments.systemHint") })
426
+ ] }) })
427
+ ] }),
428
+ segment.description && /* @__PURE__ */ jsx("p", { className: "mt-0.5 text-xs text-muted-foreground", children: segment.description }),
429
+ /* @__PURE__ */ jsx("div", { className: "mt-2 flex flex-wrap gap-1.5", children: allClauses(segment.rules).length === 0 ? /* @__PURE__ */ jsx("span", { className: "rounded bg-warning/10 px-2 py-0.5 text-[10px] text-foreground", children: t("crm.segments.noRulesWarning") }) : /* @__PURE__ */ jsxs(Fragment, { children: [
430
+ audienceOf(segment) === "deals" && /* @__PURE__ */ jsx("span", { className: "rounded bg-primary/10 px-2 py-0.5 text-[10px] font-medium text-primary", children: t("crm.segments.audience.deals") }),
431
+ segment.rules.where.map((rule, i) => /* @__PURE__ */ jsx("span", { className: "rounded bg-muted px-2 py-0.5 text-[10px] text-muted-foreground", children: describeRule(rule, t, audienceOf(segment)) }, `w${i}`)),
432
+ (segment.rules.any ?? []).length > 0 && /* @__PURE__ */ jsxs("span", { className: "rounded bg-info-soft px-2 py-0.5 text-[10px] text-info-soft-foreground", children: [
433
+ t("crm.segments.group.anyShort"),
434
+ ": ",
435
+ (segment.rules.any ?? []).map((r) => describeRule(r, t, audienceOf(segment))).join(" \xB7 ")
436
+ ] }),
437
+ (segment.rules.none ?? []).length > 0 && /* @__PURE__ */ jsxs("span", { className: "rounded bg-destructive-soft px-2 py-0.5 text-[10px] text-destructive-soft-foreground", children: [
438
+ t("crm.segments.group.noneShort"),
439
+ ": ",
440
+ (segment.rules.none ?? []).map((r) => describeRule(r, t, audienceOf(segment))).join(" \xB7 ")
441
+ ] })
442
+ ] }) })
443
+ ] }),
444
+ /* @__PURE__ */ jsxs("div", { className: "flex items-center gap-3", children: [
445
+ /* @__PURE__ */ jsxs("div", { className: "text-right", children: [
446
+ /* @__PURE__ */ jsx("p", { className: "text-lg font-semibold leading-none", "data-testid": "segment-count", children: count === void 0 ? "\xB7" : count === null ? "\u2014" : count }),
447
+ /* @__PURE__ */ jsx("p", { className: "mt-1 text-[10px] text-muted-foreground", children: t("crm.segments.people") })
448
+ ] }),
449
+ /* @__PURE__ */ jsx(
450
+ Switch,
451
+ {
452
+ checked: segment.isActive,
453
+ disabled: !canEdit,
454
+ onCheckedChange: (v) => void toggleActive(segment, v)
455
+ }
456
+ ),
457
+ /* @__PURE__ */ jsx(
458
+ Button,
459
+ {
460
+ variant: "ghost",
461
+ size: "icon",
462
+ className: "h-8 w-8",
463
+ disabled: !canEdit,
464
+ onClick: () => setEditing(segment),
465
+ children: /* @__PURE__ */ jsx(Pencil, { className: "h-3.5 w-3.5" })
466
+ }
467
+ ),
468
+ !segment.isSystem && /* @__PURE__ */ jsx(
469
+ Button,
470
+ {
471
+ variant: "ghost",
472
+ size: "icon",
473
+ className: "h-8 w-8",
474
+ disabled: !canDelete,
475
+ onClick: () => setRemoving(segment),
476
+ children: /* @__PURE__ */ jsx(Trash2, { className: "h-3.5 w-3.5" })
477
+ }
478
+ )
479
+ ] })
480
+ ] })
481
+ },
482
+ segment.id
483
+ );
484
+ }) }),
485
+ /* @__PURE__ */ jsx(
486
+ SegmentEditorDialog,
487
+ {
488
+ open: editing !== void 0,
489
+ segment: editing ?? null,
490
+ provider,
491
+ onClose: () => setEditing(void 0),
492
+ onSaved: () => void load()
493
+ }
494
+ ),
495
+ /* @__PURE__ */ jsx(
496
+ ConfirmDialog,
497
+ {
498
+ open: removing !== null,
499
+ onCancel: () => setRemoving(null),
500
+ title: t("crm.segments.deleteTitle"),
501
+ description: t("crm.segments.deleteDescription", { name: removing?.name ?? "" }),
502
+ confirmLabel: t("common.delete"),
503
+ variant: "destructive",
504
+ onConfirm: () => {
505
+ if (removing) void remove(removing);
506
+ }
507
+ }
508
+ )
509
+ ] });
510
+ }
511
+
512
+ export { SegmentsView };
513
+ //# sourceMappingURL=SegmentsView-JTFU6XWD.js.map
514
+ //# sourceMappingURL=SegmentsView-JTFU6XWD.js.map