@odla-ai/crm 0.7.1 → 0.9.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.
package/dist/ui/index.js CHANGED
@@ -1,1053 +1,23 @@
1
- // src/ui/hooks.ts
2
- import { useCallback, useEffect, useMemo, useRef, useState } from "preact/hooks";
3
- function useCrmQuery(client, type, initial) {
4
- const first = useRef(initial);
5
- const [params, setParams] = useState({ type, offset: 0, ...first.current });
6
- const [page, setPage] = useState(null);
7
- const [loading, setLoading] = useState(true);
8
- const [error, setError] = useState(null);
9
- const [tick, setTick] = useState(0);
10
- const seq = useRef(0);
11
- useEffect(() => {
12
- setParams({ type, offset: 0, ...first.current });
13
- }, [type]);
14
- useEffect(() => {
15
- const mine = ++seq.current;
16
- setLoading(true);
17
- client.listRecords(params).then(
18
- (result) => {
19
- if (seq.current !== mine) return;
20
- setPage(result);
21
- setError(null);
22
- setLoading(false);
23
- },
24
- (failure) => {
25
- if (seq.current !== mine) return;
26
- setError(failure instanceof Error ? failure.message : String(failure));
27
- setLoading(false);
28
- }
29
- );
30
- }, [client, params, tick]);
31
- const update = useCallback((patch) => {
32
- setParams((prev) => ({ ...prev, ...patch, offset: 0 }));
33
- }, []);
34
- const setOffset = useCallback((offset) => {
35
- setParams((prev) => ({ ...prev, offset }));
36
- }, []);
37
- const setSort = useCallback((sort) => update({ sort }), [update]);
38
- const setSearch = useCallback((search) => update({ search }), [update]);
39
- const refresh = useCallback(() => setTick((t) => t + 1), []);
40
- return useMemo(
41
- () => ({ params, page, loading, error, update, setOffset, setSort, setSearch, refresh }),
42
- [params, page, loading, error, update, setOffset, setSort, setSearch, refresh]
43
- );
44
- }
45
- function useCrmRecord(client, id) {
46
- const [detail, setDetail] = useState(null);
47
- const [loading, setLoading] = useState(Boolean(id));
48
- const [error, setError] = useState(null);
49
- const [tick, setTick] = useState(0);
50
- const seq = useRef(0);
51
- useEffect(() => {
52
- const mine = ++seq.current;
53
- if (!id) {
54
- setDetail(null);
55
- setLoading(false);
56
- setError(null);
57
- return;
58
- }
59
- setLoading(true);
60
- client.getRecord(id).then(
61
- (result) => {
62
- if (seq.current !== mine) return;
63
- setDetail(result);
64
- setError(null);
65
- setLoading(false);
66
- },
67
- (failure) => {
68
- if (seq.current !== mine) return;
69
- setError(failure instanceof Error ? failure.message : String(failure));
70
- setLoading(false);
71
- }
72
- );
73
- }, [client, id, tick]);
74
- const refresh = useCallback(() => setTick((t) => t + 1), []);
75
- return useMemo(() => ({ detail, loading, error, refresh }), [detail, loading, error, refresh]);
76
- }
77
-
78
- // src/constants.ts
79
- var CRM_SLOTS = ["s1", "s2", "s3", "s4", "n1", "n2", "n3", "d1", "d2"];
80
- var SORTABLE_COLUMNS = [
81
- "name",
82
- "primaryEmail",
83
- "stageIndex",
84
- "stageChangedAt",
85
- "rank",
86
- "score",
87
- "emailStatus",
88
- "billingStatus",
89
- "renewalAt",
90
- "createdAt",
91
- "updatedAt"
92
- ];
93
- var RESERVED_FIELDS = /* @__PURE__ */ new Set([
94
- "id",
95
- "type",
96
- "primaryEmail",
97
- "stage",
98
- "stageIndex",
99
- "stageChangedAt",
100
- "rank",
101
- "score",
102
- "ownerId",
103
- "emailStatus",
104
- "clerkUserId",
105
- "stripeCustomerId",
106
- "subscriptionId",
107
- "billingStatus",
108
- "renewalAt",
109
- "mrr",
110
- "fields",
111
- "searchText",
112
- "archived",
113
- "createdAt",
114
- "updatedAt",
115
- "archivedAt",
116
- ...CRM_SLOTS
117
- ]);
118
-
119
- // src/time.ts
120
- function epochOf(value) {
121
- if (typeof value === "number") return Number.isFinite(value) ? value : void 0;
122
- if (typeof value !== "string") return void 0;
123
- const parsed = Date.parse(value);
124
- return Number.isNaN(parsed) ? void 0 : parsed;
125
- }
126
-
127
- // src/ui/columns.tsx
128
- var dateText = (value) => {
129
- const ms = epochOf(value);
130
- return ms === void 0 ? "\u2014" : new Date(ms).toISOString().slice(0, 10);
131
- };
132
- var PROMOTED = {
133
- name: { header: "Name", cell: (r) => r.name },
134
- primaryEmail: { header: "Email", cell: (r) => r.primaryEmail ?? "\u2014" },
135
- stage: { header: "Stage", cell: (r) => r.stage ?? "\u2014" },
136
- emailStatus: { header: "Contactable", cell: (r) => r.emailStatus ?? "\u2014" },
137
- billingStatus: { header: "Billing", cell: (r) => r.billingStatus ?? "\u2014" },
138
- renewalAt: { header: "Renews", cell: (r) => dateText(r.renewalAt) },
139
- score: { header: "Score", cell: (r) => r.score != null ? String(r.score) : "\u2014" },
140
- rank: { header: "Rank", cell: (r) => r.rank != null ? String(r.rank) : "\u2014" },
141
- createdAt: { header: "Created", cell: (r) => dateText(r.createdAt) },
142
- updatedAt: { header: "Updated", cell: (r) => dateText(r.updatedAt) }
143
- };
144
- var SORT_ALIAS = { stage: "stageIndex" };
145
- function fieldCell(field, name) {
146
- return (record) => {
147
- const value = record.fields[name];
148
- if (value == null || value === "") return "\u2014";
149
- if (field.type === "date") return dateText(value);
150
- if (field.type === "boolean") return value ? "yes" : "no";
151
- if (field.type === "json") return JSON.stringify(value);
152
- return String(value);
153
- };
154
- }
155
- var fieldSortAs = (field) => {
156
- if (!field.slot) return void 0;
157
- if (field.type === "number") return "number";
158
- if (field.type === "date") return "date";
159
- return "string";
160
- };
161
- function sortFieldFor(crm, type, key) {
162
- if (SORT_ALIAS[key]) return SORT_ALIAS[key];
163
- if (SORTABLE_COLUMNS.includes(key)) return key;
164
- return crm.type(type).fields[key]?.slot ? key : null;
165
- }
166
- function columnsFor(crm, type, keys) {
167
- const def = crm.type(type);
168
- const columns = [];
169
- for (const key of keys) {
170
- const promoted = PROMOTED[key];
171
- if (promoted) {
172
- const sortable = sortFieldFor(crm, type, key) != null;
173
- const cell = key === "stage" ? (record) => {
174
- if (!record.stage) return "\u2014";
175
- const stage = def.pipeline?.stages.find((candidate) => candidate.id === record.stage);
176
- return stage?.label ?? stage?.id ?? record.stage;
177
- } : promoted.cell;
178
- columns.push({
179
- key,
180
- header: promoted.header,
181
- cell,
182
- ...sortable ? { sortAs: key === "name" || key === "primaryEmail" ? "string" : "number" } : {}
183
- });
184
- continue;
185
- }
186
- const field = def.fields[key];
187
- if (!field) continue;
188
- const sortAs = fieldSortAs(field);
189
- columns.push({
190
- key,
191
- header: field.label ?? key,
192
- cell: fieldCell(field, key),
193
- ...sortAs ? { sortAs } : {}
194
- });
195
- }
196
- return columns;
197
- }
198
- function defaultColumns(crm, type) {
199
- const def = crm.type(type);
200
- const keys = ["name"];
201
- if (def.pipeline) keys.push("stage");
202
- if (def.emailField) keys.push("primaryEmail", "emailStatus");
203
- for (const [name, field] of Object.entries(def.fields)) {
204
- if (field.slot && name !== def.emailField && name !== (def.nameField ?? "name"))
205
- keys.push(name);
206
- }
207
- keys.push("updatedAt");
208
- return keys;
209
- }
210
-
211
- // src/ui/crm-list.tsx
212
- import { DataTable, FilterChips, Pagination } from "@odla-ai/ui/components";
213
- import { jsx, jsxs } from "preact/jsx-runtime";
214
- function CrmList({
215
- crm,
216
- type,
217
- query,
218
- columns,
219
- onOpenRecord,
220
- hrefForRecord,
221
- selectable = false,
222
- selected,
223
- onSelectionChange,
224
- toolbar
225
- }) {
226
- const def = crm.type(type);
227
- const keys = columns ?? defaultColumns(crm, type);
228
- const tableColumns = columnsFor(crm, type, keys).map(
229
- (column) => (onOpenRecord || hrefForRecord) && column.key === "name" ? {
230
- ...column,
231
- cell: (record) => {
232
- const href = hrefForRecord?.(record);
233
- return href ? /* @__PURE__ */ jsx("a", { className: "crm-record-open", href, onClick: () => onOpenRecord?.(record), children: record.name }) : /* @__PURE__ */ jsx("button", { type: "button", className: "crm-record-open", onClick: () => onOpenRecord?.(record), children: record.name });
234
- }
235
- } : column
236
- );
237
- const limit = query.page?.limit ?? query.params.limit ?? 50;
238
- const sort = query.params.sort ? { key: query.params.sort.field, dir: query.params.sort.dir } : null;
239
- return /* @__PURE__ */ jsxs("div", { className: "crm-list", children: [
240
- (def.pipeline || toolbar) && /* @__PURE__ */ jsxs("div", { className: "crm-list-toolbar", children: [
241
- def.pipeline && /* @__PURE__ */ jsx(
242
- FilterChips,
243
- {
244
- ariaLabel: "Stage",
245
- allLabel: "All stages",
246
- chips: def.pipeline.stages.map((stage) => ({
247
- value: stage.id,
248
- label: stage.label ?? stage.id
249
- })),
250
- value: query.params.stage ?? [],
251
- onValueChange: (stage) => query.update({ stage })
252
- }
253
- ),
254
- toolbar
255
- ] }),
256
- /* @__PURE__ */ jsx(
257
- DataTable,
258
- {
259
- manual: true,
260
- filterable: true,
261
- rows: query.page?.records ?? [],
262
- rowKey: (record) => record.id,
263
- columns: tableColumns,
264
- loading: query.loading && !query.page,
265
- sort,
266
- onSortChange: (next) => {
267
- const field = sortFieldFor(crm, type, next.key);
268
- if (field) query.setSort({ field, dir: next.dir });
269
- },
270
- filter: query.params.search ?? "",
271
- onFilterChange: (search) => query.setSearch(search),
272
- filterPlaceholder: "Search records\u2026",
273
- selectable,
274
- selected,
275
- onSelectionChange,
276
- emptyState: query.error ?? "No records yet",
277
- ariaLabel: `${def.labelPlural ?? def.label} list`
278
- }
279
- ),
280
- /* @__PURE__ */ jsx(
281
- Pagination,
282
- {
283
- offset: query.params.offset ?? 0,
284
- limit,
285
- total: query.page?.total,
286
- count: query.page?.records.length,
287
- onOffsetChange: (offset) => query.setOffset(offset),
288
- disabled: query.loading
289
- }
290
- )
291
- ] });
292
- }
293
-
294
- // src/ui/crm-shell.tsx
295
- import { AppShell, Sidebar, SidebarItem, SidebarSection } from "@odla-ai/ui/components";
296
- import { jsx as jsx2, jsxs as jsxs2 } from "preact/jsx-runtime";
297
- function CrmShell({ crm, active, views = [], onNavigate, onOpenSettings, header, children }) {
298
- return /* @__PURE__ */ jsx2(
299
- AppShell,
300
- {
301
- header,
302
- sidebar: /* @__PURE__ */ jsxs2(Sidebar, { "aria-label": "CRM navigation", children: [
303
- /* @__PURE__ */ jsx2(SidebarSection, { label: "Records", children: Object.entries(crm.config.types).map(([type, def]) => /* @__PURE__ */ jsx2(
304
- SidebarItem,
305
- {
306
- active: active.type === type && !active.viewId,
307
- onClick: () => onNavigate({ type }),
308
- children: def.labelPlural ?? def.label
309
- },
310
- type
311
- )) }),
312
- views.length > 0 && /* @__PURE__ */ jsx2(SidebarSection, { label: "Views", children: views.map((view) => /* @__PURE__ */ jsxs2(
313
- SidebarItem,
314
- {
315
- active: active.viewId === view.id,
316
- onClick: () => onNavigate({ type: view.type, viewId: view.id }),
317
- children: [
318
- view.name,
319
- view.shared ? " \xB7shared" : ""
320
- ]
321
- },
322
- view.id
323
- )) }),
324
- onOpenSettings && /* @__PURE__ */ jsx2(SidebarSection, { label: "Manage", children: /* @__PURE__ */ jsx2(SidebarItem, { onClick: onOpenSettings, children: "Settings" }) })
325
- ] }),
326
- children
327
- }
328
- );
329
- }
330
-
331
- // src/ui/record-panel.tsx
332
- import { Tabs } from "@odla-ai/ui/components";
333
-
334
- // src/ui/cards.tsx
335
- import { Button } from "@odla-ai/ui/components";
336
- import { jsx as jsx3, jsxs as jsxs3 } from "preact/jsx-runtime";
337
- var dateText2 = (value) => value != null ? new Date(value).toISOString().slice(0, 10) : "\u2014";
338
- function IdentityCard({ record, onLink, linking = false }) {
339
- return /* @__PURE__ */ jsxs3("section", { className: "crm-card", "aria-label": "Identity", children: [
340
- /* @__PURE__ */ jsx3("h3", { className: "crm-card-title", children: "Identity" }),
341
- record.clerkUserId ? /* @__PURE__ */ jsxs3("p", { children: [
342
- "Linked account: ",
343
- /* @__PURE__ */ jsx3("code", { children: record.clerkUserId })
344
- ] }) : /* @__PURE__ */ jsxs3("p", { className: "crm-card-empty", children: [
345
- "No linked account.",
346
- onLink && /* @__PURE__ */ jsx3(Button, { mini: true, onClick: onLink, disabled: linking, children: "Find by email" })
347
- ] })
348
- ] });
349
- }
350
- function BillingCard({ record }) {
351
- return /* @__PURE__ */ jsxs3("section", { className: "crm-card", "aria-label": "Billing", children: [
352
- /* @__PURE__ */ jsx3("h3", { className: "crm-card-title", children: "Billing" }),
353
- record.stripeCustomerId ? /* @__PURE__ */ jsxs3("dl", { className: "crm-card-facts", children: [
354
- /* @__PURE__ */ jsx3("dt", { children: "Status" }),
355
- /* @__PURE__ */ jsx3("dd", { children: record.billingStatus ?? "\u2014" }),
356
- /* @__PURE__ */ jsx3("dt", { children: "Renews" }),
357
- /* @__PURE__ */ jsx3("dd", { children: dateText2(record.renewalAt) }),
358
- /* @__PURE__ */ jsx3("dt", { children: "Customer" }),
359
- /* @__PURE__ */ jsx3("dd", { children: /* @__PURE__ */ jsx3("code", { children: record.stripeCustomerId }) })
360
- ] }) : /* @__PURE__ */ jsx3("p", { className: "crm-card-empty", children: "No billing on file." })
361
- ] });
362
- }
363
- function ChannelsCard({ channels }) {
364
- return /* @__PURE__ */ jsxs3("section", { className: "crm-card", "aria-label": "Contact channels", children: [
365
- /* @__PURE__ */ jsx3("h3", { className: "crm-card-title", children: "Contact" }),
366
- channels.length === 0 ? /* @__PURE__ */ jsx3("p", { className: "crm-card-empty", children: "No channels." }) : /* @__PURE__ */ jsx3("ul", { className: "crm-channel-list", children: channels.map((channel) => /* @__PURE__ */ jsxs3("li", { children: [
367
- /* @__PURE__ */ jsx3("span", { children: channel.value }),
368
- channel.isPrimary && /* @__PURE__ */ jsx3("span", { className: "crm-badge", children: "primary" }),
369
- /* @__PURE__ */ jsx3("span", { className: `crm-badge consent-${channel.consent}`, children: channel.consent })
370
- ] }, channel.id)) })
371
- ] });
372
- }
373
-
374
- // src/ui/fields-form.tsx
375
- import { useState as useState2 } from "preact/hooks";
376
- import { Button as Button2, Checkbox, Field, Input, Select, Textarea } from "@odla-ai/ui/components";
377
- import { jsx as jsx4, jsxs as jsxs4 } from "preact/jsx-runtime";
378
- var dateInput = (value) => {
379
- const ms = epochOf(value);
380
- return ms === void 0 ? "" : new Date(ms).toISOString().slice(0, 10);
381
- };
382
- function initialDraft(def, record) {
383
- const draft = {};
384
- for (const [name, field] of Object.entries(def)) {
385
- const value = record?.fields[name];
386
- if (field.type === "boolean") draft[name] = value === true;
387
- else if (field.type === "date") draft[name] = dateInput(value);
388
- else if (field.type === "json") draft[name] = value == null ? "" : JSON.stringify(value);
389
- else draft[name] = value == null ? "" : String(value);
390
- }
391
- return draft;
392
- }
393
- function parseDraft(field, raw) {
394
- if (field.type === "boolean") return raw === true;
395
- const text = String(raw).trim();
396
- if (text === "") return null;
397
- if (field.type === "number") return Number(text);
398
- if (field.type === "date") return Date.parse(text);
399
- if (field.type === "json") {
400
- try {
401
- return JSON.parse(text);
402
- } catch {
403
- return text;
404
- }
405
- }
406
- return text;
407
- }
408
- function FieldsForm({
409
- crm,
410
- type,
411
- record,
412
- onSubmit,
413
- submitting = false,
414
- errors = {},
415
- submitLabel
416
- }) {
417
- const fields = crm.type(type).fields;
418
- const [draft, setDraft] = useState2(() => initialDraft(fields, record));
419
- const set = (name, value) => setDraft((prev) => ({ ...prev, [name]: value }));
420
- const submit = () => {
421
- const input = {};
422
- for (const [name, field] of Object.entries(fields)) input[name] = parseDraft(field, draft[name]);
423
- onSubmit(input);
424
- };
425
- return /* @__PURE__ */ jsxs4(
426
- "form",
427
- {
428
- className: "crm-fields-form",
429
- onSubmit: (event) => {
430
- event.preventDefault();
431
- submit();
432
- },
433
- children: [
434
- Object.entries(fields).map(([name, field]) => {
435
- const label = field.label ?? name;
436
- const error = errors[name];
437
- const common = { invalid: Boolean(error) };
438
- return /* @__PURE__ */ jsx4(Field, { label: field.type === "boolean" ? void 0 : label, error, children: field.type === "enum" ? /* @__PURE__ */ jsx4(
439
- Select,
440
- {
441
- ...common,
442
- value: String(draft[name] ?? ""),
443
- onChange: (event) => set(name, event.currentTarget.value),
444
- options: [
445
- { value: "", label: "\u2014" },
446
- ...(field.options ?? []).map((option) => ({ value: option, label: option }))
447
- ]
448
- }
449
- ) : field.type === "boolean" ? /* @__PURE__ */ jsx4(
450
- Checkbox,
451
- {
452
- checked: draft[name] === true,
453
- onChange: (event) => set(name, event.currentTarget.checked),
454
- label
455
- }
456
- ) : field.type === "json" ? /* @__PURE__ */ jsx4(
457
- Textarea,
458
- {
459
- ...common,
460
- value: String(draft[name] ?? ""),
461
- rows: 3,
462
- onInput: (event) => set(name, event.currentTarget.value)
463
- }
464
- ) : /* @__PURE__ */ jsx4(
465
- Input,
466
- {
467
- ...common,
468
- type: field.type === "number" ? "number" : field.type === "date" ? "date" : field.type === "email" ? "email" : field.type === "url" ? "url" : "text",
469
- value: String(draft[name] ?? ""),
470
- onInput: (event) => set(name, event.currentTarget.value)
471
- }
472
- ) }, name);
473
- }),
474
- /* @__PURE__ */ jsx4(Button2, { type: "submit", disabled: submitting, children: submitLabel ?? (record ? "Save" : "Create") })
475
- ]
476
- }
477
- );
478
- }
479
-
480
- // src/ui/stage-control.tsx
481
- import { useState as useState3 } from "preact/hooks";
482
- import { Button as Button3, Field as Field2, Select as Select2 } from "@odla-ai/ui/components";
483
- import { jsx as jsx5, jsxs as jsxs5 } from "preact/jsx-runtime";
484
- function StageControl({ crm, record, onMove, disabled = false }) {
485
- const pipeline = crm.type(record.type).pipeline;
486
- const [target, setTarget] = useState3("");
487
- const [busy, setBusy] = useState3(false);
488
- const [error, setError] = useState3(null);
489
- if (!pipeline) return null;
490
- const move = async () => {
491
- if (!target) return;
492
- setBusy(true);
493
- setError(null);
494
- try {
495
- await onMove(target);
496
- setTarget("");
497
- } catch (failure) {
498
- setError(failure instanceof Error ? failure.message : String(failure));
499
- } finally {
500
- setBusy(false);
501
- }
502
- };
503
- return /* @__PURE__ */ jsx5(Field2, { label: "Stage", error, className: "crm-stage-control", children: /* @__PURE__ */ jsxs5("div", { className: "crm-stage-row", children: [
504
- /* @__PURE__ */ jsx5("span", { className: "crm-stage-current", children: record.stage ?? "\u2014" }),
505
- /* @__PURE__ */ jsx5(
506
- Select2,
507
- {
508
- "aria-label": "Move to stage",
509
- value: target,
510
- onChange: (event) => setTarget(event.currentTarget.value),
511
- options: [
512
- { value: "", label: "Move to\u2026" },
513
- ...pipeline.stages.filter((stage) => stage.id !== record.stage).map((stage) => ({ value: stage.id, label: stage.label ?? stage.id }))
514
- ]
515
- }
516
- ),
517
- /* @__PURE__ */ jsx5(Button3, { mini: true, onClick: move, disabled: disabled || busy || !target, children: "Move" })
518
- ] }) });
519
- }
520
-
521
- // src/ui/tag-editor.tsx
522
- import { Combobox } from "@odla-ai/ui/components";
523
- import { jsx as jsx6, jsxs as jsxs6 } from "preact/jsx-runtime";
524
- function TagEditor({ tags, onAdd, onRemove, suggest, disabled = false }) {
525
- return /* @__PURE__ */ jsxs6("div", { className: "crm-tag-editor", children: [
526
- /* @__PURE__ */ jsx6("div", { className: "chips", role: "group", "aria-label": "Tags", children: tags.map((tag) => /* @__PURE__ */ jsxs6("span", { className: "chip crm-tag-chip", children: [
527
- tag.tag,
528
- /* @__PURE__ */ jsx6(
529
- "button",
530
- {
531
- type: "button",
532
- className: "crm-tag-remove",
533
- "aria-label": `Remove tag ${tag.tag}`,
534
- disabled,
535
- onClick: () => onRemove(tag.tag),
536
- children: "\xD7"
537
- }
538
- )
539
- ] }, tag.id)) }),
540
- /* @__PURE__ */ jsx6(
541
- Combobox,
542
- {
543
- ariaLabel: "Add tag",
544
- placeholder: "Add tag\u2026",
545
- freeText: true,
546
- disabled,
547
- search: suggest ?? (() => []),
548
- onPick: (value) => onAdd(value)
549
- }
550
- )
551
- ] });
552
- }
553
-
554
- // src/ui/record-panel.tsx
555
- import { jsx as jsx7, jsxs as jsxs7 } from "preact/jsx-runtime";
556
- function RecordPanel({
557
- crm,
558
- detail,
559
- onSaveFields,
560
- onMoveStage,
561
- onAddTag,
562
- onRemoveTag,
563
- onLinkIdentity,
564
- saving = false,
565
- fieldErrors,
566
- activitySlot,
567
- stagePlacement = "header",
568
- facetPlacement = "cards",
569
- tabs,
570
- extendTabs,
571
- tab,
572
- defaultTab = "profile",
573
- onTabChange,
574
- hrefForTab,
575
- header,
576
- headerActions,
577
- mount = "active"
578
- }) {
579
- const { record, tags, channels, links } = detail;
580
- const def = crm.type(record.type);
581
- const facets = def.facets ?? {};
582
- const identity = facets.identity ? /* @__PURE__ */ jsx7(IdentityCard, { record, onLink: onLinkIdentity, linking: saving }) : null;
583
- const billing = facets.billing ? /* @__PURE__ */ jsx7(BillingCard, { record }) : null;
584
- const channelsCard = facets.email !== false && channels.length > 0 ? /* @__PURE__ */ jsx7(ChannelsCard, { channels }) : null;
585
- const profile = /* @__PURE__ */ jsxs7("div", { className: "crm-panel-profile", children: [
586
- /* @__PURE__ */ jsx7(
587
- FieldsForm,
588
- {
589
- crm,
590
- type: record.type,
591
- record,
592
- onSubmit: onSaveFields,
593
- submitting: saving,
594
- errors: fieldErrors
595
- },
596
- record.id + String(record.updatedAt)
597
- ),
598
- facetPlacement === "tabs" && (identity || channelsCard) ? /* @__PURE__ */ jsxs7("div", { className: "crm-panel-cards", children: [
599
- identity,
600
- channelsCard
601
- ] }) : null
602
- ] });
603
- const connections = /* @__PURE__ */ jsxs7("div", { className: "crm-panel-connections", children: [
604
- /* @__PURE__ */ jsx7(TagEditor, { tags, onAdd: onAddTag, onRemove: onRemoveTag, disabled: saving }),
605
- /* @__PURE__ */ jsx7("ul", { className: "crm-link-list", "aria-label": "Linked records", children: links.map((link) => /* @__PURE__ */ jsxs7("li", { children: [
606
- link.relation,
607
- ": ",
608
- /* @__PURE__ */ jsx7("code", { children: link.fromId === record.id ? link.toId : link.fromId })
609
- ] }, link.id)) })
610
- ] });
611
- const stage = def.pipeline ? /* @__PURE__ */ jsx7("div", { className: "crm-panel-stage", children: /* @__PURE__ */ jsx7(StageControl, { crm, record, onMove: onMoveStage, disabled: saving }) }) : null;
612
- const network = detail.origins?.length || detail.deliveries?.length ? /* @__PURE__ */ jsxs7("div", { className: "crm-panel-connections", children: [
613
- detail.origins?.length ? /* @__PURE__ */ jsxs7("section", { children: [
614
- /* @__PURE__ */ jsx7("h3", { children: "Received from" }),
615
- /* @__PURE__ */ jsx7("ul", { className: "crm-link-list", children: detail.origins.map((origin) => /* @__PURE__ */ jsxs7("li", { children: [
616
- /* @__PURE__ */ jsx7("strong", { children: origin.sourceId }),
617
- " ",
618
- /* @__PURE__ */ jsx7("code", { children: origin.sourceRecordId }),
619
- /* @__PURE__ */ jsxs7("span", { children: [
620
- " \xB7 ",
621
- new Date(origin.lastReceivedAt).toLocaleString()
622
- ] })
623
- ] }, origin.key)) })
624
- ] }) : null,
625
- detail.deliveries?.length ? /* @__PURE__ */ jsxs7("section", { children: [
626
- /* @__PURE__ */ jsx7("h3", { children: "Shared with" }),
627
- /* @__PURE__ */ jsx7("ul", { className: "crm-link-list", children: detail.deliveries.map((delivery) => /* @__PURE__ */ jsxs7("li", { children: [
628
- /* @__PURE__ */ jsx7("strong", { children: delivery.targetId }),
629
- " ",
630
- /* @__PURE__ */ jsx7("span", { className: "crm-badge", children: delivery.status }),
631
- /* @__PURE__ */ jsxs7("span", { children: [
632
- " \xB7 ",
633
- new Date(delivery.lastAttemptAt).toLocaleString()
634
- ] }),
635
- delivery.error ? /* @__PURE__ */ jsxs7("span", { children: [
636
- " \xB7 ",
637
- delivery.error
638
- ] }) : null
639
- ] }, delivery.key)) })
640
- ] }) : null
641
- ] }) : null;
642
- const builtInTabs = [
643
- ...stagePlacement === "tab" && stage ? [{ id: "stage", label: "Stage", panel: stage }] : [],
644
- { id: "profile", label: "Profile", panel: profile },
645
- ...activitySlot ? [{ id: "activity", label: "Activity", panel: activitySlot }] : [],
646
- ...facetPlacement === "tabs" && billing ? [{ id: "billing", label: "Billing", panel: billing }] : [],
647
- { id: "connections", label: "Connections", panel: connections },
648
- ...network ? [{ id: "network", label: "Network", panel: network }] : []
649
- ];
650
- const configuredTabs = tabs ?? extendTabs?.(builtInTabs) ?? builtInTabs;
651
- const resolvedTabs = configuredTabs.filter((item) => typeof item.visible === "function" ? item.visible(detail) : item.visible !== false).map((item) => ({
652
- ...item,
653
- disabled: typeof item.disabled === "function" ? item.disabled(detail) : item.disabled
654
- }));
655
- return /* @__PURE__ */ jsxs7("div", { className: "crm-record-panel", "aria-label": record.name, children: [
656
- header !== void 0 ? header : /* @__PURE__ */ jsxs7("header", { className: "crm-panel-header", children: [
657
- /* @__PURE__ */ jsx7("h2", { children: record.name }),
658
- stagePlacement === "header" ? stage : null,
659
- headerActions
660
- ] }),
661
- facetPlacement === "cards" && (identity || billing || channelsCard) ? /* @__PURE__ */ jsxs7("div", { className: "crm-panel-cards", children: [
662
- identity,
663
- billing,
664
- channelsCard
665
- ] }) : null,
666
- /* @__PURE__ */ jsx7(
667
- Tabs,
668
- {
669
- ariaLabel: "Record detail",
670
- items: resolvedTabs.map((item) => ({
671
- value: item.id,
672
- label: item.label,
673
- panel: item.panel,
674
- disabled: item.disabled,
675
- href: item.href ?? hrefForTab?.(item.id)
676
- })),
677
- value: tab,
678
- defaultValue: defaultTab,
679
- onValueChange: onTabChange,
680
- mount
681
- }
682
- )
683
- ] });
684
- }
685
-
686
- // src/ui/crm-workspace.tsx
687
- import { useState as useState4 } from "preact/hooks";
688
- import { MasterDetail } from "@odla-ai/ui/components";
689
- import { jsx as jsx8, jsxs as jsxs8 } from "preact/jsx-runtime";
690
- function CrmWorkspace({
691
- crm,
692
- client,
693
- type,
694
- recordId,
695
- defaultRecordId = null,
696
- onRecordIdChange,
697
- hrefForRecord,
698
- hrefForList,
699
- detailTab,
700
- defaultDetailTab,
701
- onDetailTabChange,
702
- hrefForDetailTab,
703
- extendRecordTabs,
704
- renderMaster,
705
- renderSummary,
706
- renderDetailHeader,
707
- renderDetail,
708
- renderEmptyDetail,
709
- collapseClosedDetail = true,
710
- lifecycle,
711
- requireLifecycleAdapter = false,
712
- stagePlacement = "tab",
713
- facetPlacement = "tabs",
714
- columns,
715
- toolbar,
716
- emptyDetail
717
- }) {
718
- const query = useCrmQuery(client, type);
719
- const [internalId, setInternalId] = useState4(defaultRecordId);
720
- const selectedId = recordId === void 0 ? internalId : recordId;
721
- const record = useCrmRecord(client, selectedId);
722
- const [saving, setSaving] = useState4(false);
723
- const select = (id, opened) => {
724
- if (recordId === void 0) setInternalId(id);
725
- onRecordIdChange?.(id, opened);
726
- };
727
- const refresh = () => {
728
- record.refresh();
729
- query.refresh();
730
- };
731
- const saveFields = async (input) => {
732
- if (!selectedId) return;
733
- setSaving(true);
734
- try {
735
- await client.updateRecord(selectedId, { input });
736
- refresh();
737
- } finally {
738
- setSaving(false);
739
- }
740
- };
741
- const moveStage = async (to) => {
742
- if (!selectedId || !record.detail) return;
743
- if (requireLifecycleAdapter && !lifecycle) {
744
- throw new Error("This collection requires a lifecycle adapter for stage transitions.");
745
- }
746
- setSaving(true);
747
- try {
748
- if (lifecycle) {
749
- await lifecycle.transitionStage({
750
- record: record.detail.record,
751
- detail: record.detail,
752
- from: record.detail.record.stage ?? null,
753
- to,
754
- client,
755
- refreshRecord: record.refresh,
756
- refreshList: query.refresh
757
- });
758
- } else {
759
- await client.setStage(selectedId, to);
760
- }
761
- refresh();
762
- } finally {
763
- setSaving(false);
764
- }
765
- };
766
- const addTag = async (tag) => {
767
- if (!selectedId) return;
768
- await client.addTag(selectedId, tag);
769
- record.refresh();
770
- };
771
- const removeTag = async (tag) => {
772
- if (!selectedId) return;
773
- await client.removeTag(selectedId, tag);
774
- record.refresh();
775
- };
776
- const linkIdentity = async () => {
777
- if (!selectedId) return;
778
- setSaving(true);
779
- try {
780
- await client.linkIdentity(selectedId);
781
- refresh();
782
- } finally {
783
- setSaving(false);
784
- }
785
- };
786
- const defaultMaster = /* @__PURE__ */ jsx8(
787
- CrmList,
788
- {
789
- crm,
790
- type,
791
- query,
792
- columns,
793
- toolbar,
794
- hrefForRecord,
795
- onOpenRecord: (opened) => select(opened.id, opened)
796
- }
797
- );
798
- const masterContext = {
799
- crm,
800
- client,
801
- type,
802
- query,
803
- selectedRecordId: selectedId,
804
- defaultMaster,
805
- openRecord: (opened) => select(opened.id, opened),
806
- closeRecord: () => select(null),
807
- ...hrefForRecord ? { hrefForRecord } : {}
808
- };
809
- const recordContext = record.detail ? {
810
- crm,
811
- type,
812
- detail: record.detail,
813
- client,
814
- query,
815
- saving,
816
- refreshRecord: record.refresh,
817
- refreshList: query.refresh,
818
- saveFields,
819
- moveStage,
820
- addTag,
821
- removeTag,
822
- linkIdentity
823
- } : null;
824
- const defaultDetail = record.detail && recordContext ? /* @__PURE__ */ jsx8(
825
- RecordPanel,
826
- {
827
- crm,
828
- detail: record.detail,
829
- onSaveFields: saveFields,
830
- onMoveStage: moveStage,
831
- onAddTag: addTag,
832
- onRemoveTag: removeTag,
833
- onLinkIdentity: linkIdentity,
834
- saving,
835
- stagePlacement,
836
- facetPlacement,
837
- tab: detailTab,
838
- defaultTab: defaultDetailTab,
839
- onTabChange: onDetailTabChange,
840
- hrefForTab: hrefForDetailTab,
841
- header: renderDetailHeader?.(recordContext),
842
- extendTabs: extendRecordTabs ? (base) => extendRecordTabs(base, recordContext) : void 0
843
- }
844
- ) : null;
845
- const detail = defaultDetail && recordContext ? renderDetail ? renderDetail(recordContext, defaultDetail) : defaultDetail : selectedId ? /* @__PURE__ */ jsx8("div", { className: "crm-workspace-state", role: record.error ? "alert" : "status", children: record.error ?? "Loading record\u2026" }) : null;
846
- return /* @__PURE__ */ jsxs8("div", { className: "crm-workspace", children: [
847
- renderSummary ? /* @__PURE__ */ jsx8("div", { className: "crm-workspace-summary", children: renderSummary(masterContext) }) : null,
848
- /* @__PURE__ */ jsx8(
849
- MasterDetail,
850
- {
851
- master: renderMaster?.(masterContext) ?? defaultMaster,
852
- detail,
853
- detailOpen: selectedId != null,
854
- emptyDetail: renderEmptyDetail?.(masterContext) ?? emptyDetail,
855
- collapseClosedDetail,
856
- masterLabel: `${crm.type(type).labelPlural ?? crm.type(type).label} list`,
857
- detailLabel: record.detail ? `${record.detail.record.name} details` : "Record details",
858
- closeHref: hrefForList,
859
- onClose: () => select(null)
860
- }
861
- )
862
- ] });
863
- }
864
-
865
- // src/ui/activity-feed.tsx
866
- import { useEffect as useEffect2, useRef as useRef2, useState as useState5 } from "preact/hooks";
867
- import { Button as Button4, Checkbox as Checkbox2, Input as Input2, Segmented, Textarea as Textarea2 } from "@odla-ai/ui/components";
868
- import { Fragment, jsx as jsx9, jsxs as jsxs9 } from "preact/jsx-runtime";
869
- var when = (at) => new Date(at).toISOString().replace("T", " ").slice(0, 16);
870
- function ActivityFeed({
871
- activities,
872
- onAddNote,
873
- onAddTask,
874
- onToggleTask,
875
- busy = false,
876
- focusedActivityId
877
- }) {
878
- const [mode, setMode] = useState5("note");
879
- const [body, setBody] = useState5("");
880
- const [dueAt, setDueAt] = useState5("");
881
- const [waitingOn, setWaitingOn] = useState5("");
882
- const focused = useRef2(null);
883
- useEffect2(() => {
884
- if (!focusedActivityId || !focused.current) return;
885
- focused.current.focus({ preventScroll: true });
886
- focused.current.scrollIntoView?.({ block: "center" });
887
- }, [activities, focusedActivityId]);
888
- const submit = () => {
889
- const text = body.trim();
890
- if (!text) return;
891
- if (mode === "note") onAddNote(text);
892
- else
893
- onAddTask({
894
- body: text,
895
- ...dueAt ? { dueAt: Date.parse(dueAt) } : {},
896
- ...waitingOn.trim() ? { waitingOn: waitingOn.trim() } : {}
897
- });
898
- setBody("");
899
- setDueAt("");
900
- setWaitingOn("");
901
- };
902
- return /* @__PURE__ */ jsxs9("div", { className: "crm-activity-feed", children: [
903
- /* @__PURE__ */ jsxs9("div", { className: "crm-activity-composer", children: [
904
- /* @__PURE__ */ jsx9(
905
- Segmented,
906
- {
907
- ariaLabel: "Entry kind",
908
- value: mode,
909
- onValueChange: (value) => setMode(value),
910
- options: [
911
- { value: "note", label: "Note" },
912
- { value: "task", label: "Follow-up" }
913
- ]
914
- }
915
- ),
916
- /* @__PURE__ */ jsx9(
917
- Textarea2,
918
- {
919
- "aria-label": mode === "note" ? "New note" : "New follow-up",
920
- value: body,
921
- rows: 2,
922
- placeholder: mode === "note" ? "What happened?" : "What needs doing?",
923
- onInput: (event) => setBody(event.currentTarget.value)
924
- }
925
- ),
926
- mode === "task" && /* @__PURE__ */ jsxs9("div", { className: "crm-activity-task-row", children: [
927
- /* @__PURE__ */ jsx9(
928
- Input2,
929
- {
930
- type: "date",
931
- "aria-label": "Due date",
932
- value: dueAt,
933
- onInput: (event) => setDueAt(event.currentTarget.value)
934
- }
935
- ),
936
- /* @__PURE__ */ jsx9(
937
- Input2,
938
- {
939
- "aria-label": "Waiting on",
940
- placeholder: "Waiting on\u2026",
941
- value: waitingOn,
942
- onInput: (event) => setWaitingOn(event.currentTarget.value)
943
- }
944
- )
945
- ] }),
946
- /* @__PURE__ */ jsx9(Button4, { mini: true, onClick: submit, disabled: busy || !body.trim(), children: "Add" })
947
- ] }),
948
- /* @__PURE__ */ jsx9("ul", { className: "crm-activity-list", children: activities.map((activity) => /* @__PURE__ */ jsxs9(
949
- "li",
950
- {
951
- className: `crm-activity kind-${activity.kind}`,
952
- ref: activity.id === focusedActivityId ? focused : void 0,
953
- tabIndex: activity.id === focusedActivityId ? -1 : void 0,
954
- "aria-current": activity.id === focusedActivityId ? "true" : void 0,
955
- children: [
956
- activity.kind === "task" ? /* @__PURE__ */ jsx9(
957
- Checkbox2,
958
- {
959
- checked: activity.status === "done",
960
- disabled: busy,
961
- onChange: (event) => onToggleTask(activity, event.currentTarget.checked),
962
- label: /* @__PURE__ */ jsxs9(Fragment, { children: [
963
- activity.body,
964
- activity.dueAt != null && /* @__PURE__ */ jsxs9("span", { className: "crm-activity-due", children: [
965
- " \xB7 due ",
966
- when(activity.dueAt).slice(0, 10)
967
- ] }),
968
- activity.waitingOn && /* @__PURE__ */ jsxs9("span", { className: "crm-activity-waiting", children: [
969
- " \xB7 waiting on ",
970
- activity.waitingOn
971
- ] })
972
- ] })
973
- }
974
- ) : /* @__PURE__ */ jsx9("p", { children: activity.body }),
975
- /* @__PURE__ */ jsxs9("span", { className: "crm-activity-meta", children: [
976
- activity.kind,
977
- " \xB7 ",
978
- when(activity.createdAt)
979
- ] })
980
- ]
981
- },
982
- activity.id
983
- )) })
984
- ] });
985
- }
986
-
987
- // src/ui/bulk-bar.tsx
988
- import { Button as Button5 } from "@odla-ai/ui/components";
989
- import { jsx as jsx10, jsxs as jsxs10 } from "preact/jsx-runtime";
990
- function BulkBar({ selected, actions, onClear, busy = false }) {
991
- if (selected.length === 0) return null;
992
- return /* @__PURE__ */ jsxs10("div", { className: "crm-bulk-bar", role: "toolbar", "aria-label": "Bulk actions", children: [
993
- /* @__PURE__ */ jsxs10("span", { className: "crm-bulk-count", children: [
994
- selected.length,
995
- " selected"
996
- ] }),
997
- actions.map((action) => /* @__PURE__ */ jsx10(Button5, { mini: true, disabled: busy, onClick: () => void action.run(selected), children: action.label }, action.id)),
998
- /* @__PURE__ */ jsx10(Button5, { mini: true, variant: "ghost", onClick: onClear, disabled: busy, children: "Clear" })
999
- ] });
1000
- }
1001
-
1002
- // src/ui/view-editor.tsx
1003
- import { useState as useState6 } from "preact/hooks";
1004
- import { Button as Button6, Checkbox as Checkbox3, Input as Input3 } from "@odla-ai/ui/components";
1005
- import { jsx as jsx11, jsxs as jsxs11 } from "preact/jsx-runtime";
1006
- function ViewEditor({ current, onSave }) {
1007
- const [open, setOpen] = useState6(false);
1008
- const [name, setName] = useState6("");
1009
- const [shared, setShared] = useState6(false);
1010
- const [busy, setBusy] = useState6(false);
1011
- const [error, setError] = useState6(null);
1012
- if (!open)
1013
- return /* @__PURE__ */ jsx11(Button6, { mini: true, variant: "ghost", onClick: () => setOpen(true), children: "Save view\u2026" });
1014
- const save = async () => {
1015
- setBusy(true);
1016
- setError(null);
1017
- try {
1018
- await onSave({ ...current, name: name.trim(), shared });
1019
- setOpen(false);
1020
- setName("");
1021
- setShared(false);
1022
- } catch (failure) {
1023
- setError(failure instanceof Error ? failure.message : String(failure));
1024
- } finally {
1025
- setBusy(false);
1026
- }
1027
- };
1028
- return /* @__PURE__ */ jsxs11("div", { className: "crm-view-editor", role: "group", "aria-label": "Save view", children: [
1029
- /* @__PURE__ */ jsx11(
1030
- Input3,
1031
- {
1032
- "aria-label": "View name",
1033
- placeholder: "View name",
1034
- value: name,
1035
- onInput: (event) => setName(event.currentTarget.value)
1036
- }
1037
- ),
1038
- /* @__PURE__ */ jsx11(
1039
- Checkbox3,
1040
- {
1041
- checked: shared,
1042
- onChange: (event) => setShared(event.currentTarget.checked),
1043
- label: "Shared"
1044
- }
1045
- ),
1046
- /* @__PURE__ */ jsx11(Button6, { mini: true, onClick: save, disabled: busy || !name.trim(), children: "Save" }),
1047
- /* @__PURE__ */ jsx11(Button6, { mini: true, variant: "ghost", onClick: () => setOpen(false), disabled: busy, children: "Cancel" }),
1048
- error && /* @__PURE__ */ jsx11("span", { className: "crm-view-error", children: error })
1049
- ] });
1050
- }
1
+ import {
2
+ ActivityFeed,
3
+ BillingCard,
4
+ BulkBar,
5
+ ChannelsCard,
6
+ CrmList,
7
+ CrmShell,
8
+ CrmWorkspace,
9
+ FieldsForm,
10
+ IdentityCard,
11
+ RecordPanel,
12
+ StageControl,
13
+ TagEditor,
14
+ ViewEditor,
15
+ columnsFor,
16
+ defaultColumns,
17
+ sortFieldFor,
18
+ useCrmQuery,
19
+ useCrmRecord
20
+ } from "../chunk-7LH3WGSU.js";
1051
21
  export {
1052
22
  ActivityFeed,
1053
23
  BillingCard,