@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.
@@ -0,0 +1,1279 @@
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_NS = {
80
+ record: "crm_record",
81
+ tag: "crm_tag",
82
+ link: "crm_link",
83
+ activity: "crm_activity",
84
+ channel: "crm_contact_channel",
85
+ emailLog: "crm_email_log",
86
+ view: "crm_view",
87
+ config: "crm_config",
88
+ origin: "crm_record_origin",
89
+ delivery: "crm_record_delivery"
90
+ };
91
+ var CRM_SLOTS = ["s1", "s2", "s3", "s4", "n1", "n2", "n3", "d1", "d2"];
92
+ var SORTABLE_COLUMNS = [
93
+ "name",
94
+ "primaryEmail",
95
+ "stageIndex",
96
+ "stageChangedAt",
97
+ "rank",
98
+ "score",
99
+ "emailStatus",
100
+ "billingStatus",
101
+ "renewalAt",
102
+ "createdAt",
103
+ "updatedAt"
104
+ ];
105
+ var RESERVED_FIELDS = /* @__PURE__ */ new Set([
106
+ "id",
107
+ "type",
108
+ "primaryEmail",
109
+ "stage",
110
+ "stageIndex",
111
+ "stageChangedAt",
112
+ "rank",
113
+ "score",
114
+ "ownerId",
115
+ "emailStatus",
116
+ "clerkUserId",
117
+ "stripeCustomerId",
118
+ "subscriptionId",
119
+ "billingStatus",
120
+ "renewalAt",
121
+ "mrr",
122
+ "fields",
123
+ "searchText",
124
+ "archived",
125
+ "createdAt",
126
+ "updatedAt",
127
+ "archivedAt",
128
+ ...CRM_SLOTS
129
+ ]);
130
+
131
+ // src/schema.ts
132
+ var a = (type, o = {}) => ({
133
+ type,
134
+ unique: false,
135
+ indexed: false,
136
+ optional: false,
137
+ ...o
138
+ });
139
+ var uniq = (type) => a(type, { unique: true, indexed: true });
140
+ var idx = (type, optional = false) => a(type, { indexed: true, optional });
141
+ var opt = (type) => a(type, { optional: true });
142
+ var CRM_SCHEMA = {
143
+ entities: {
144
+ [CRM_NS.record]: {
145
+ attrs: {
146
+ // Entity ids are not attributes in odla-db, so `where: { id }` matches
147
+ // nothing unless the id is mirrored as a written attr — the getRecord /
148
+ // tag-filter / neighbor reads all filter by id. Mirror it as unique.
149
+ id: uniq("string"),
150
+ type: idx("string"),
151
+ name: idx("string"),
152
+ primaryEmail: idx("string", true),
153
+ stage: idx("string", true),
154
+ stageIndex: idx("number", true),
155
+ stageChangedAt: idx("date", true),
156
+ rank: idx("number", true),
157
+ score: idx("number", true),
158
+ ownerId: idx("string", true),
159
+ emailStatus: idx("string", true),
160
+ clerkUserId: a("string", { unique: true, indexed: true, optional: true }),
161
+ stripeCustomerId: idx("string", true),
162
+ subscriptionId: opt("string"),
163
+ billingStatus: idx("string", true),
164
+ renewalAt: idx("date", true),
165
+ mrr: opt("number"),
166
+ s1: idx("string", true),
167
+ s2: idx("string", true),
168
+ s3: idx("string", true),
169
+ s4: idx("string", true),
170
+ n1: idx("number", true),
171
+ n2: idx("number", true),
172
+ n3: idx("number", true),
173
+ d1: idx("date", true),
174
+ d2: idx("date", true),
175
+ fields: a("json"),
176
+ searchText: a("string"),
177
+ archived: idx("boolean"),
178
+ createdAt: idx("date"),
179
+ updatedAt: idx("date"),
180
+ archivedAt: opt("date")
181
+ }
182
+ },
183
+ [CRM_NS.tag]: {
184
+ attrs: {
185
+ key: uniq("string"),
186
+ // `${recordId}:${tag}`
187
+ recordId: idx("string"),
188
+ tag: idx("string"),
189
+ createdAt: idx("date")
190
+ }
191
+ },
192
+ [CRM_NS.link]: {
193
+ attrs: {
194
+ key: uniq("string"),
195
+ // `${fromId}:${relation}:${toId}`
196
+ fromId: idx("string"),
197
+ toId: idx("string"),
198
+ relation: idx("string"),
199
+ createdAt: idx("date")
200
+ }
201
+ },
202
+ [CRM_NS.activity]: {
203
+ attrs: {
204
+ id: uniq("string"),
205
+ // mirrored so updateTask can read one by id
206
+ recordId: idx("string"),
207
+ kind: idx("string"),
208
+ body: a("string"),
209
+ status: idx("string", true),
210
+ // tasks: open | waiting | done
211
+ dueAt: idx("date", true),
212
+ waitingOn: opt("string"),
213
+ assigneeId: idx("string", true),
214
+ authorId: idx("string"),
215
+ createdAt: idx("date"),
216
+ completedAt: opt("date"),
217
+ meta: opt("json")
218
+ }
219
+ },
220
+ [CRM_NS.channel]: {
221
+ attrs: {
222
+ key: uniq("string"),
223
+ // `${recordId}:${kind}:${value}`
224
+ recordId: idx("string"),
225
+ kind: a("string"),
226
+ // email | phone
227
+ value: idx("string"),
228
+ // emails lowercased
229
+ isPrimary: a("boolean"),
230
+ consent: idx("string"),
231
+ consentAt: idx("date"),
232
+ consentSource: opt("string"),
233
+ unsubscribeToken: uniq("string"),
234
+ createdAt: idx("date")
235
+ }
236
+ },
237
+ [CRM_NS.emailLog]: {
238
+ attrs: {
239
+ recordId: idx("string", true),
240
+ channelId: opt("string"),
241
+ to: a("string"),
242
+ templateId: idx("string"),
243
+ subject: a("string"),
244
+ transport: a("string"),
245
+ // sent | log-only
246
+ redirected: opt("boolean"),
247
+ dedupeKey: idx("string", true),
248
+ messageId: opt("string"),
249
+ error: opt("string"),
250
+ sentAt: idx("date")
251
+ }
252
+ },
253
+ [CRM_NS.view]: {
254
+ attrs: {
255
+ id: uniq("string"),
256
+ // mirrored so getView/updateView can read one by id
257
+ name: a("string"),
258
+ type: idx("string"),
259
+ ownerId: idx("string", true),
260
+ shared: idx("boolean"),
261
+ filters: a("json"),
262
+ sort: a("json"),
263
+ columns: a("json"),
264
+ createdAt: idx("date"),
265
+ updatedAt: idx("date")
266
+ }
267
+ },
268
+ [CRM_NS.config]: {
269
+ attrs: {
270
+ key: uniq("string"),
271
+ // CONFIG_KEY singleton
272
+ notificationEmail: opt("string"),
273
+ replyTo: opt("string"),
274
+ debugEmail: opt("string"),
275
+ emailTemplates: a("json"),
276
+ // Record<templateId, {subject, text, enabled}>
277
+ settings: a("json"),
278
+ updatedAt: idx("date")
279
+ }
280
+ },
281
+ [CRM_NS.origin]: {
282
+ attrs: {
283
+ // One upstream record maps to one local record. The structured source
284
+ // survives tag edits and supports more than one upstream site.
285
+ key: uniq("string"),
286
+ // `${sourceId}:${sourceRecordId}`
287
+ recordId: idx("string"),
288
+ sourceId: idx("string"),
289
+ sourceRecordId: idx("string"),
290
+ sourceUrl: opt("string"),
291
+ payloadVersion: idx("number"),
292
+ firstReceivedAt: idx("date"),
293
+ lastReceivedAt: idx("date")
294
+ }
295
+ },
296
+ [CRM_NS.delivery]: {
297
+ attrs: {
298
+ // The latest delivery state for one local record → target edge.
299
+ key: uniq("string"),
300
+ // `${recordId}:${targetId}`
301
+ recordId: idx("string"),
302
+ targetId: idx("string"),
303
+ remoteRecordId: idx("string", true),
304
+ status: idx("string"),
305
+ attempts: idx("number"),
306
+ payloadVersion: idx("number"),
307
+ lastAttemptAt: idx("date"),
308
+ deliveredAt: idx("date", true),
309
+ error: opt("string")
310
+ }
311
+ }
312
+ },
313
+ links: {}
314
+ };
315
+
316
+ // src/time.ts
317
+ var DATE_FIELDS = new Map(
318
+ Object.entries(CRM_SCHEMA.entities).map(([ns, entity]) => [
319
+ ns,
320
+ new Set(
321
+ Object.entries(entity.attrs).filter(([, attr]) => attr.type === "date").map(([label]) => label)
322
+ )
323
+ ])
324
+ );
325
+ function epochOf(value) {
326
+ if (typeof value === "number") return Number.isFinite(value) ? value : void 0;
327
+ if (typeof value !== "string") return void 0;
328
+ const parsed = Date.parse(value);
329
+ return Number.isNaN(parsed) ? void 0 : parsed;
330
+ }
331
+
332
+ // src/ui/columns.tsx
333
+ var dateText = (value) => {
334
+ const ms = epochOf(value);
335
+ return ms === void 0 ? "\u2014" : new Date(ms).toISOString().slice(0, 10);
336
+ };
337
+ var PROMOTED = {
338
+ name: { header: "Name", cell: (r) => r.name },
339
+ primaryEmail: { header: "Email", cell: (r) => r.primaryEmail ?? "\u2014" },
340
+ stage: { header: "Stage", cell: (r) => r.stage ?? "\u2014" },
341
+ emailStatus: { header: "Contactable", cell: (r) => r.emailStatus ?? "\u2014" },
342
+ billingStatus: { header: "Billing", cell: (r) => r.billingStatus ?? "\u2014" },
343
+ renewalAt: { header: "Renews", cell: (r) => dateText(r.renewalAt) },
344
+ score: { header: "Score", cell: (r) => r.score != null ? String(r.score) : "\u2014" },
345
+ rank: { header: "Rank", cell: (r) => r.rank != null ? String(r.rank) : "\u2014" },
346
+ createdAt: { header: "Created", cell: (r) => dateText(r.createdAt) },
347
+ updatedAt: { header: "Updated", cell: (r) => dateText(r.updatedAt) }
348
+ };
349
+ var SORT_ALIAS = { stage: "stageIndex" };
350
+ function fieldCell(field, name) {
351
+ return (record) => {
352
+ const value = record.fields[name];
353
+ if (value == null || value === "") return "\u2014";
354
+ if (field.type === "date") return dateText(value);
355
+ if (field.type === "boolean") return value ? "yes" : "no";
356
+ if (field.type === "json") return JSON.stringify(value);
357
+ return String(value);
358
+ };
359
+ }
360
+ var fieldSortAs = (field) => {
361
+ if (!field.slot) return void 0;
362
+ if (field.type === "number") return "number";
363
+ if (field.type === "date") return "date";
364
+ return "string";
365
+ };
366
+ function sortFieldFor(crm, type, key) {
367
+ if (SORT_ALIAS[key]) return SORT_ALIAS[key];
368
+ if (SORTABLE_COLUMNS.includes(key)) return key;
369
+ return crm.type(type).fields[key]?.slot ? key : null;
370
+ }
371
+ function columnsFor(crm, type, keys) {
372
+ const def = crm.type(type);
373
+ const columns = [];
374
+ for (const key of keys) {
375
+ const promoted = PROMOTED[key];
376
+ if (promoted) {
377
+ const sortable = sortFieldFor(crm, type, key) != null;
378
+ const cell = key === "stage" ? (record) => {
379
+ if (!record.stage) return "\u2014";
380
+ const stage = def.pipeline?.stages.find((candidate) => candidate.id === record.stage);
381
+ return stage?.label ?? stage?.id ?? record.stage;
382
+ } : promoted.cell;
383
+ columns.push({
384
+ key,
385
+ header: promoted.header,
386
+ cell,
387
+ ...sortable ? { sortAs: key === "name" || key === "primaryEmail" ? "string" : "number" } : {}
388
+ });
389
+ continue;
390
+ }
391
+ const field = def.fields[key];
392
+ if (!field) continue;
393
+ const sortAs = fieldSortAs(field);
394
+ columns.push({
395
+ key,
396
+ header: field.label ?? key,
397
+ cell: fieldCell(field, key),
398
+ ...sortAs ? { sortAs } : {}
399
+ });
400
+ }
401
+ return columns;
402
+ }
403
+ function defaultColumns(crm, type) {
404
+ const def = crm.type(type);
405
+ const keys = ["name"];
406
+ if (def.pipeline) keys.push("stage");
407
+ if (def.emailField) keys.push("primaryEmail", "emailStatus");
408
+ for (const [name, field] of Object.entries(def.fields)) {
409
+ if (field.slot && name !== def.emailField && name !== (def.nameField ?? "name"))
410
+ keys.push(name);
411
+ }
412
+ keys.push("updatedAt");
413
+ return keys;
414
+ }
415
+
416
+ // src/ui/crm-list.tsx
417
+ import { DataTable, FilterChips, Pagination } from "@odla-ai/ui/components";
418
+ import { jsx, jsxs } from "preact/jsx-runtime";
419
+ function CrmList({
420
+ crm,
421
+ type,
422
+ query,
423
+ columns,
424
+ onOpenRecord,
425
+ hrefForRecord,
426
+ selectable = false,
427
+ selected,
428
+ onSelectionChange,
429
+ toolbar
430
+ }) {
431
+ const def = crm.type(type);
432
+ const keys = columns ?? defaultColumns(crm, type);
433
+ const tableColumns = columnsFor(crm, type, keys).map(
434
+ (column) => (onOpenRecord || hrefForRecord) && column.key === "name" ? {
435
+ ...column,
436
+ cell: (record) => {
437
+ const href = hrefForRecord?.(record);
438
+ 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 });
439
+ }
440
+ } : column
441
+ );
442
+ const limit = query.page?.limit ?? query.params.limit ?? 50;
443
+ const sort = query.params.sort ? { key: query.params.sort.field, dir: query.params.sort.dir } : null;
444
+ return /* @__PURE__ */ jsxs("div", { className: "crm-list", children: [
445
+ (def.pipeline || toolbar) && /* @__PURE__ */ jsxs("div", { className: "crm-list-toolbar", children: [
446
+ def.pipeline && /* @__PURE__ */ jsx(
447
+ FilterChips,
448
+ {
449
+ ariaLabel: "Stage",
450
+ allLabel: "All stages",
451
+ chips: def.pipeline.stages.map((stage) => ({
452
+ value: stage.id,
453
+ label: stage.label ?? stage.id
454
+ })),
455
+ value: query.params.stage ?? [],
456
+ onValueChange: (stage) => query.update({ stage })
457
+ }
458
+ ),
459
+ toolbar
460
+ ] }),
461
+ /* @__PURE__ */ jsx(
462
+ DataTable,
463
+ {
464
+ manual: true,
465
+ filterable: true,
466
+ rows: query.page?.records ?? [],
467
+ rowKey: (record) => record.id,
468
+ columns: tableColumns,
469
+ loading: query.loading && !query.page,
470
+ sort,
471
+ onSortChange: (next) => {
472
+ const field = sortFieldFor(crm, type, next.key);
473
+ if (field) query.setSort({ field, dir: next.dir });
474
+ },
475
+ filter: query.params.search ?? "",
476
+ onFilterChange: (search) => query.setSearch(search),
477
+ filterPlaceholder: "Search records\u2026",
478
+ selectable,
479
+ selected,
480
+ onSelectionChange,
481
+ emptyState: query.error ?? "No records yet",
482
+ ariaLabel: `${def.labelPlural ?? def.label} list`
483
+ }
484
+ ),
485
+ /* @__PURE__ */ jsx(
486
+ Pagination,
487
+ {
488
+ offset: query.params.offset ?? 0,
489
+ limit,
490
+ total: query.page?.total,
491
+ count: query.page?.records.length,
492
+ onOffsetChange: (offset) => query.setOffset(offset),
493
+ disabled: query.loading
494
+ }
495
+ )
496
+ ] });
497
+ }
498
+
499
+ // src/ui/crm-shell.tsx
500
+ import { AppShell, Sidebar, SidebarItem, SidebarSection } from "@odla-ai/ui/components";
501
+ import { jsx as jsx2, jsxs as jsxs2 } from "preact/jsx-runtime";
502
+ function CrmShell({ crm, active, views = [], onNavigate, onOpenSettings, header, children }) {
503
+ return /* @__PURE__ */ jsx2(
504
+ AppShell,
505
+ {
506
+ header,
507
+ sidebar: /* @__PURE__ */ jsxs2(Sidebar, { "aria-label": "CRM navigation", children: [
508
+ /* @__PURE__ */ jsx2(SidebarSection, { label: "Records", children: Object.entries(crm.config.types).map(([type, def]) => /* @__PURE__ */ jsx2(
509
+ SidebarItem,
510
+ {
511
+ active: active.type === type && !active.viewId,
512
+ onClick: () => onNavigate({ type }),
513
+ children: def.labelPlural ?? def.label
514
+ },
515
+ type
516
+ )) }),
517
+ views.length > 0 && /* @__PURE__ */ jsx2(SidebarSection, { label: "Views", children: views.map((view) => /* @__PURE__ */ jsxs2(
518
+ SidebarItem,
519
+ {
520
+ active: active.viewId === view.id,
521
+ onClick: () => onNavigate({ type: view.type, viewId: view.id }),
522
+ children: [
523
+ view.name,
524
+ view.shared ? " \xB7shared" : ""
525
+ ]
526
+ },
527
+ view.id
528
+ )) }),
529
+ onOpenSettings && /* @__PURE__ */ jsx2(SidebarSection, { label: "Manage", children: /* @__PURE__ */ jsx2(SidebarItem, { onClick: onOpenSettings, children: "Settings" }) })
530
+ ] }),
531
+ children
532
+ }
533
+ );
534
+ }
535
+
536
+ // src/ui/cards.tsx
537
+ import { Button } from "@odla-ai/ui/components";
538
+ import { jsx as jsx3, jsxs as jsxs3 } from "preact/jsx-runtime";
539
+ var dateText2 = (value) => value != null ? new Date(value).toISOString().slice(0, 10) : "\u2014";
540
+ function IdentityCard({ record, onLink, linking = false }) {
541
+ return /* @__PURE__ */ jsxs3("section", { className: "crm-card", "aria-label": "Identity", children: [
542
+ /* @__PURE__ */ jsx3("h3", { className: "crm-card-title", children: "Identity" }),
543
+ record.clerkUserId ? /* @__PURE__ */ jsxs3("p", { children: [
544
+ "Linked account: ",
545
+ /* @__PURE__ */ jsx3("code", { children: record.clerkUserId })
546
+ ] }) : /* @__PURE__ */ jsxs3("p", { className: "crm-card-empty", children: [
547
+ "No linked account.",
548
+ onLink && /* @__PURE__ */ jsx3(Button, { mini: true, onClick: onLink, disabled: linking, children: "Find by email" })
549
+ ] })
550
+ ] });
551
+ }
552
+ function BillingCard({ record }) {
553
+ return /* @__PURE__ */ jsxs3("section", { className: "crm-card", "aria-label": "Billing", children: [
554
+ /* @__PURE__ */ jsx3("h3", { className: "crm-card-title", children: "Billing" }),
555
+ record.stripeCustomerId ? /* @__PURE__ */ jsxs3("dl", { className: "crm-card-facts", children: [
556
+ /* @__PURE__ */ jsx3("dt", { children: "Status" }),
557
+ /* @__PURE__ */ jsx3("dd", { children: record.billingStatus ?? "\u2014" }),
558
+ /* @__PURE__ */ jsx3("dt", { children: "Renews" }),
559
+ /* @__PURE__ */ jsx3("dd", { children: dateText2(record.renewalAt) }),
560
+ /* @__PURE__ */ jsx3("dt", { children: "Customer" }),
561
+ /* @__PURE__ */ jsx3("dd", { children: /* @__PURE__ */ jsx3("code", { children: record.stripeCustomerId }) })
562
+ ] }) : /* @__PURE__ */ jsx3("p", { className: "crm-card-empty", children: "No billing on file." })
563
+ ] });
564
+ }
565
+ function ChannelsCard({ channels }) {
566
+ return /* @__PURE__ */ jsxs3("section", { className: "crm-card", "aria-label": "Contact channels", children: [
567
+ /* @__PURE__ */ jsx3("h3", { className: "crm-card-title", children: "Contact" }),
568
+ 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: [
569
+ /* @__PURE__ */ jsx3("span", { children: channel.value }),
570
+ channel.isPrimary && /* @__PURE__ */ jsx3("span", { className: "crm-badge", children: "primary" }),
571
+ /* @__PURE__ */ jsx3("span", { className: `crm-badge consent-${channel.consent}`, children: channel.consent })
572
+ ] }, channel.id)) })
573
+ ] });
574
+ }
575
+
576
+ // src/ui/fields-form.tsx
577
+ import { useState as useState2 } from "preact/hooks";
578
+ import { Button as Button2, Checkbox, Field, Input, Select, Textarea } from "@odla-ai/ui/components";
579
+ import { jsx as jsx4, jsxs as jsxs4 } from "preact/jsx-runtime";
580
+ var dateInput = (value) => {
581
+ const ms = epochOf(value);
582
+ return ms === void 0 ? "" : new Date(ms).toISOString().slice(0, 10);
583
+ };
584
+ function initialDraft(def, record) {
585
+ const draft = {};
586
+ for (const [name, field] of Object.entries(def)) {
587
+ const value = record?.fields[name];
588
+ if (field.type === "boolean") draft[name] = value === true;
589
+ else if (field.type === "date") draft[name] = dateInput(value);
590
+ else if (field.type === "json") draft[name] = value == null ? "" : JSON.stringify(value);
591
+ else draft[name] = value == null ? "" : String(value);
592
+ }
593
+ return draft;
594
+ }
595
+ function parseDraft(field, raw) {
596
+ if (field.type === "boolean") return raw === true;
597
+ const text = String(raw).trim();
598
+ if (text === "") return null;
599
+ if (field.type === "number") return Number(text);
600
+ if (field.type === "date") return Date.parse(text);
601
+ if (field.type === "json") {
602
+ try {
603
+ return JSON.parse(text);
604
+ } catch {
605
+ return text;
606
+ }
607
+ }
608
+ return text;
609
+ }
610
+ function FieldsForm({
611
+ crm,
612
+ type,
613
+ record,
614
+ onSubmit,
615
+ submitting = false,
616
+ errors = {},
617
+ submitLabel
618
+ }) {
619
+ const fields = crm.type(type).fields;
620
+ const [draft, setDraft] = useState2(() => initialDraft(fields, record));
621
+ const set = (name, value) => setDraft((prev) => ({ ...prev, [name]: value }));
622
+ const submit = () => {
623
+ const input = {};
624
+ for (const [name, field] of Object.entries(fields)) input[name] = parseDraft(field, draft[name]);
625
+ onSubmit(input);
626
+ };
627
+ return /* @__PURE__ */ jsxs4(
628
+ "form",
629
+ {
630
+ className: "crm-fields-form",
631
+ onSubmit: (event) => {
632
+ event.preventDefault();
633
+ submit();
634
+ },
635
+ children: [
636
+ Object.entries(fields).map(([name, field]) => {
637
+ const label = field.label ?? name;
638
+ const error = errors[name];
639
+ const common = { invalid: Boolean(error) };
640
+ return /* @__PURE__ */ jsx4(Field, { label: field.type === "boolean" ? void 0 : label, error, children: field.type === "enum" ? /* @__PURE__ */ jsx4(
641
+ Select,
642
+ {
643
+ ...common,
644
+ value: String(draft[name] ?? ""),
645
+ onChange: (event) => set(name, event.currentTarget.value),
646
+ options: [
647
+ { value: "", label: "\u2014" },
648
+ ...(field.options ?? []).map((option) => ({ value: option, label: option }))
649
+ ]
650
+ }
651
+ ) : field.type === "boolean" ? /* @__PURE__ */ jsx4(
652
+ Checkbox,
653
+ {
654
+ checked: draft[name] === true,
655
+ onChange: (event) => set(name, event.currentTarget.checked),
656
+ label
657
+ }
658
+ ) : field.type === "json" ? /* @__PURE__ */ jsx4(
659
+ Textarea,
660
+ {
661
+ ...common,
662
+ value: String(draft[name] ?? ""),
663
+ rows: 3,
664
+ onInput: (event) => set(name, event.currentTarget.value)
665
+ }
666
+ ) : /* @__PURE__ */ jsx4(
667
+ Input,
668
+ {
669
+ ...common,
670
+ type: field.type === "number" ? "number" : field.type === "date" ? "date" : field.type === "email" ? "email" : field.type === "url" ? "url" : "text",
671
+ value: String(draft[name] ?? ""),
672
+ onInput: (event) => set(name, event.currentTarget.value)
673
+ }
674
+ ) }, name);
675
+ }),
676
+ /* @__PURE__ */ jsx4(Button2, { type: "submit", disabled: submitting, children: submitLabel ?? (record ? "Save" : "Create") })
677
+ ]
678
+ }
679
+ );
680
+ }
681
+
682
+ // src/ui/stage-control.tsx
683
+ import { useState as useState3 } from "preact/hooks";
684
+ import { Button as Button3, Field as Field2, Select as Select2 } from "@odla-ai/ui/components";
685
+ import { jsx as jsx5, jsxs as jsxs5 } from "preact/jsx-runtime";
686
+ function StageControl({ crm, record, onMove, disabled = false }) {
687
+ const pipeline = crm.type(record.type).pipeline;
688
+ const [target, setTarget] = useState3("");
689
+ const [busy, setBusy] = useState3(false);
690
+ const [error, setError] = useState3(null);
691
+ if (!pipeline) return null;
692
+ const move = async () => {
693
+ if (!target) return;
694
+ setBusy(true);
695
+ setError(null);
696
+ try {
697
+ await onMove(target);
698
+ setTarget("");
699
+ } catch (failure) {
700
+ setError(failure instanceof Error ? failure.message : String(failure));
701
+ } finally {
702
+ setBusy(false);
703
+ }
704
+ };
705
+ return /* @__PURE__ */ jsx5(Field2, { label: "Stage", error, className: "crm-stage-control", children: /* @__PURE__ */ jsxs5("div", { className: "crm-stage-row", children: [
706
+ /* @__PURE__ */ jsx5("span", { className: "crm-stage-current", children: record.stage ?? "\u2014" }),
707
+ /* @__PURE__ */ jsx5(
708
+ Select2,
709
+ {
710
+ "aria-label": "Move to stage",
711
+ value: target,
712
+ onChange: (event) => setTarget(event.currentTarget.value),
713
+ options: [
714
+ { value: "", label: "Move to\u2026" },
715
+ ...pipeline.stages.filter((stage) => stage.id !== record.stage).map((stage) => ({ value: stage.id, label: stage.label ?? stage.id }))
716
+ ]
717
+ }
718
+ ),
719
+ /* @__PURE__ */ jsx5(Button3, { mini: true, onClick: move, disabled: disabled || busy || !target, children: "Move" })
720
+ ] }) });
721
+ }
722
+
723
+ // src/ui/tag-editor.tsx
724
+ import { Combobox } from "@odla-ai/ui/components";
725
+ import { jsx as jsx6, jsxs as jsxs6 } from "preact/jsx-runtime";
726
+ function TagEditor({ tags, onAdd, onRemove, suggest, disabled = false }) {
727
+ return /* @__PURE__ */ jsxs6("div", { className: "crm-tag-editor", children: [
728
+ /* @__PURE__ */ jsx6("div", { className: "chips", role: "group", "aria-label": "Tags", children: tags.map((tag) => /* @__PURE__ */ jsxs6("span", { className: "chip crm-tag-chip", children: [
729
+ tag.tag,
730
+ /* @__PURE__ */ jsx6(
731
+ "button",
732
+ {
733
+ type: "button",
734
+ className: "crm-tag-remove",
735
+ "aria-label": `Remove tag ${tag.tag}`,
736
+ disabled,
737
+ onClick: () => onRemove(tag.tag),
738
+ children: "\xD7"
739
+ }
740
+ )
741
+ ] }, tag.id)) }),
742
+ /* @__PURE__ */ jsx6(
743
+ Combobox,
744
+ {
745
+ ariaLabel: "Add tag",
746
+ placeholder: "Add tag\u2026",
747
+ freeText: true,
748
+ disabled,
749
+ search: suggest ?? (() => []),
750
+ onPick: (value) => onAdd(value)
751
+ }
752
+ )
753
+ ] });
754
+ }
755
+
756
+ // src/ui/record-panel.tsx
757
+ import { Tabs } from "@odla-ai/ui/components";
758
+ import { jsx as jsx7, jsxs as jsxs7 } from "preact/jsx-runtime";
759
+ function RecordPanel({
760
+ crm,
761
+ detail,
762
+ onSaveFields,
763
+ onMoveStage,
764
+ onAddTag,
765
+ onRemoveTag,
766
+ onLinkIdentity,
767
+ saving = false,
768
+ fieldErrors,
769
+ activitySlot,
770
+ stagePlacement = "header",
771
+ facetPlacement = "cards",
772
+ tabs,
773
+ extendTabs,
774
+ tab,
775
+ defaultTab = "profile",
776
+ onTabChange,
777
+ hrefForTab,
778
+ header,
779
+ headerActions,
780
+ mount = "active"
781
+ }) {
782
+ const { record, tags, channels, links } = detail;
783
+ const def = crm.type(record.type);
784
+ const facets = def.facets ?? {};
785
+ const identity = facets.identity ? /* @__PURE__ */ jsx7(IdentityCard, { record, onLink: onLinkIdentity, linking: saving }) : null;
786
+ const billing = facets.billing ? /* @__PURE__ */ jsx7(BillingCard, { record }) : null;
787
+ const channelsCard = facets.email !== false && channels.length > 0 ? /* @__PURE__ */ jsx7(ChannelsCard, { channels }) : null;
788
+ const profile = /* @__PURE__ */ jsxs7("div", { className: "crm-panel-profile", children: [
789
+ /* @__PURE__ */ jsx7(
790
+ FieldsForm,
791
+ {
792
+ crm,
793
+ type: record.type,
794
+ record,
795
+ onSubmit: onSaveFields,
796
+ submitting: saving,
797
+ errors: fieldErrors
798
+ },
799
+ record.id + String(record.updatedAt)
800
+ ),
801
+ facetPlacement === "tabs" && (identity || channelsCard) ? /* @__PURE__ */ jsxs7("div", { className: "crm-panel-cards", children: [
802
+ identity,
803
+ channelsCard
804
+ ] }) : null
805
+ ] });
806
+ const connections = /* @__PURE__ */ jsxs7("div", { className: "crm-panel-connections", children: [
807
+ /* @__PURE__ */ jsx7(TagEditor, { tags, onAdd: onAddTag, onRemove: onRemoveTag, disabled: saving }),
808
+ /* @__PURE__ */ jsx7("ul", { className: "crm-link-list", "aria-label": "Linked records", children: links.map((link) => /* @__PURE__ */ jsxs7("li", { children: [
809
+ link.relation,
810
+ ": ",
811
+ /* @__PURE__ */ jsx7("code", { children: link.fromId === record.id ? link.toId : link.fromId })
812
+ ] }, link.id)) })
813
+ ] });
814
+ const stage = def.pipeline ? /* @__PURE__ */ jsx7("div", { className: "crm-panel-stage", children: /* @__PURE__ */ jsx7(StageControl, { crm, record, onMove: onMoveStage, disabled: saving }) }) : null;
815
+ const network = detail.origins?.length || detail.deliveries?.length ? /* @__PURE__ */ jsxs7("div", { className: "crm-panel-connections", children: [
816
+ detail.origins?.length ? /* @__PURE__ */ jsxs7("section", { children: [
817
+ /* @__PURE__ */ jsx7("h3", { children: "Received from" }),
818
+ /* @__PURE__ */ jsx7("ul", { className: "crm-link-list", children: detail.origins.map((origin) => /* @__PURE__ */ jsxs7("li", { children: [
819
+ /* @__PURE__ */ jsx7("strong", { children: origin.sourceId }),
820
+ " ",
821
+ /* @__PURE__ */ jsx7("code", { children: origin.sourceRecordId }),
822
+ /* @__PURE__ */ jsxs7("span", { children: [
823
+ " \xB7 ",
824
+ new Date(origin.lastReceivedAt).toLocaleString()
825
+ ] })
826
+ ] }, origin.key)) })
827
+ ] }) : null,
828
+ detail.deliveries?.length ? /* @__PURE__ */ jsxs7("section", { children: [
829
+ /* @__PURE__ */ jsx7("h3", { children: "Shared with" }),
830
+ /* @__PURE__ */ jsx7("ul", { className: "crm-link-list", children: detail.deliveries.map((delivery) => /* @__PURE__ */ jsxs7("li", { children: [
831
+ /* @__PURE__ */ jsx7("strong", { children: delivery.targetId }),
832
+ " ",
833
+ /* @__PURE__ */ jsx7("span", { className: "crm-badge", children: delivery.status }),
834
+ /* @__PURE__ */ jsxs7("span", { children: [
835
+ " \xB7 ",
836
+ new Date(delivery.lastAttemptAt).toLocaleString()
837
+ ] }),
838
+ delivery.error ? /* @__PURE__ */ jsxs7("span", { children: [
839
+ " \xB7 ",
840
+ delivery.error
841
+ ] }) : null
842
+ ] }, delivery.key)) })
843
+ ] }) : null
844
+ ] }) : null;
845
+ const builtInTabs = [
846
+ ...stagePlacement === "tab" && stage ? [{ id: "stage", label: "Stage", panel: stage }] : [],
847
+ { id: "profile", label: "Profile", panel: profile },
848
+ ...activitySlot ? [{ id: "activity", label: "Activity", panel: activitySlot }] : [],
849
+ ...facetPlacement === "tabs" && billing ? [{ id: "billing", label: "Billing", panel: billing }] : [],
850
+ { id: "connections", label: "Connections", panel: connections },
851
+ ...network ? [{ id: "network", label: "Network", panel: network }] : []
852
+ ];
853
+ const configuredTabs = tabs ?? extendTabs?.(builtInTabs) ?? builtInTabs;
854
+ const resolvedTabs = configuredTabs.filter((item) => typeof item.visible === "function" ? item.visible(detail) : item.visible !== false).map((item) => ({
855
+ ...item,
856
+ disabled: typeof item.disabled === "function" ? item.disabled(detail) : item.disabled
857
+ }));
858
+ return /* @__PURE__ */ jsxs7("div", { className: "crm-record-panel", "aria-label": record.name, children: [
859
+ header !== void 0 ? header : /* @__PURE__ */ jsxs7("header", { className: "crm-panel-header", children: [
860
+ /* @__PURE__ */ jsx7("h2", { children: record.name }),
861
+ stagePlacement === "header" ? stage : null,
862
+ headerActions
863
+ ] }),
864
+ facetPlacement === "cards" && (identity || billing || channelsCard) ? /* @__PURE__ */ jsxs7("div", { className: "crm-panel-cards", children: [
865
+ identity,
866
+ billing,
867
+ channelsCard
868
+ ] }) : null,
869
+ /* @__PURE__ */ jsx7(
870
+ Tabs,
871
+ {
872
+ ariaLabel: "Record detail",
873
+ items: resolvedTabs.map((item) => ({
874
+ value: item.id,
875
+ label: item.label,
876
+ panel: item.panel,
877
+ disabled: item.disabled,
878
+ href: item.href ?? hrefForTab?.(item.id)
879
+ })),
880
+ value: tab,
881
+ defaultValue: defaultTab,
882
+ onValueChange: onTabChange,
883
+ mount
884
+ }
885
+ )
886
+ ] });
887
+ }
888
+
889
+ // src/ui/crm-workspace.tsx
890
+ import { useState as useState4 } from "preact/hooks";
891
+ import { MasterDetail } from "@odla-ai/ui/components";
892
+ import { jsx as jsx8, jsxs as jsxs8 } from "preact/jsx-runtime";
893
+ function CrmWorkspace({
894
+ crm,
895
+ client,
896
+ type,
897
+ recordId,
898
+ defaultRecordId = null,
899
+ onRecordIdChange,
900
+ hrefForRecord,
901
+ hrefForList,
902
+ detailTab,
903
+ defaultDetailTab,
904
+ onDetailTabChange,
905
+ hrefForDetailTab,
906
+ extendRecordTabs,
907
+ renderMaster,
908
+ renderSummary,
909
+ renderDetailHeader,
910
+ renderDetail,
911
+ renderEmptyDetail,
912
+ collapseClosedDetail = true,
913
+ lifecycle,
914
+ requireLifecycleAdapter = false,
915
+ stagePlacement = "tab",
916
+ facetPlacement = "tabs",
917
+ columns,
918
+ toolbar,
919
+ emptyDetail
920
+ }) {
921
+ const query = useCrmQuery(client, type);
922
+ const [internalId, setInternalId] = useState4(defaultRecordId);
923
+ const selectedId = recordId === void 0 ? internalId : recordId;
924
+ const record = useCrmRecord(client, selectedId);
925
+ const [saving, setSaving] = useState4(false);
926
+ const select = (id, opened) => {
927
+ if (recordId === void 0) setInternalId(id);
928
+ onRecordIdChange?.(id, opened);
929
+ };
930
+ const refresh = () => {
931
+ record.refresh();
932
+ query.refresh();
933
+ };
934
+ const saveFields = async (input) => {
935
+ if (!selectedId) return;
936
+ setSaving(true);
937
+ try {
938
+ await client.updateRecord(selectedId, { input });
939
+ refresh();
940
+ } finally {
941
+ setSaving(false);
942
+ }
943
+ };
944
+ const moveStage = async (to) => {
945
+ if (!selectedId || !record.detail) return;
946
+ if (requireLifecycleAdapter && !lifecycle) {
947
+ throw new Error("This collection requires a lifecycle adapter for stage transitions.");
948
+ }
949
+ setSaving(true);
950
+ try {
951
+ if (lifecycle) {
952
+ await lifecycle.transitionStage({
953
+ record: record.detail.record,
954
+ detail: record.detail,
955
+ from: record.detail.record.stage ?? null,
956
+ to,
957
+ client,
958
+ refreshRecord: record.refresh,
959
+ refreshList: query.refresh
960
+ });
961
+ } else {
962
+ await client.setStage(selectedId, to);
963
+ }
964
+ refresh();
965
+ } finally {
966
+ setSaving(false);
967
+ }
968
+ };
969
+ const addTag = async (tag) => {
970
+ if (!selectedId) return;
971
+ await client.addTag(selectedId, tag);
972
+ record.refresh();
973
+ };
974
+ const removeTag = async (tag) => {
975
+ if (!selectedId) return;
976
+ await client.removeTag(selectedId, tag);
977
+ record.refresh();
978
+ };
979
+ const linkIdentity = async () => {
980
+ if (!selectedId) return;
981
+ setSaving(true);
982
+ try {
983
+ await client.linkIdentity(selectedId);
984
+ refresh();
985
+ } finally {
986
+ setSaving(false);
987
+ }
988
+ };
989
+ const defaultMaster = /* @__PURE__ */ jsx8(
990
+ CrmList,
991
+ {
992
+ crm,
993
+ type,
994
+ query,
995
+ columns,
996
+ toolbar,
997
+ hrefForRecord,
998
+ onOpenRecord: (opened) => select(opened.id, opened)
999
+ }
1000
+ );
1001
+ const masterContext = {
1002
+ crm,
1003
+ client,
1004
+ type,
1005
+ query,
1006
+ selectedRecordId: selectedId,
1007
+ defaultMaster,
1008
+ openRecord: (opened) => select(opened.id, opened),
1009
+ closeRecord: () => select(null),
1010
+ ...hrefForRecord ? { hrefForRecord } : {}
1011
+ };
1012
+ const recordContext = record.detail ? {
1013
+ crm,
1014
+ type,
1015
+ detail: record.detail,
1016
+ client,
1017
+ query,
1018
+ saving,
1019
+ refreshRecord: record.refresh,
1020
+ refreshList: query.refresh,
1021
+ saveFields,
1022
+ moveStage,
1023
+ addTag,
1024
+ removeTag,
1025
+ linkIdentity
1026
+ } : null;
1027
+ const defaultDetail = record.detail && recordContext ? /* @__PURE__ */ jsx8(
1028
+ RecordPanel,
1029
+ {
1030
+ crm,
1031
+ detail: record.detail,
1032
+ onSaveFields: saveFields,
1033
+ onMoveStage: moveStage,
1034
+ onAddTag: addTag,
1035
+ onRemoveTag: removeTag,
1036
+ onLinkIdentity: linkIdentity,
1037
+ saving,
1038
+ stagePlacement,
1039
+ facetPlacement,
1040
+ tab: detailTab,
1041
+ defaultTab: defaultDetailTab,
1042
+ onTabChange: onDetailTabChange,
1043
+ hrefForTab: hrefForDetailTab,
1044
+ header: renderDetailHeader?.(recordContext),
1045
+ extendTabs: extendRecordTabs ? (base) => extendRecordTabs(base, recordContext) : void 0
1046
+ }
1047
+ ) : null;
1048
+ 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;
1049
+ return /* @__PURE__ */ jsxs8("div", { className: "crm-workspace", children: [
1050
+ renderSummary ? /* @__PURE__ */ jsx8("div", { className: "crm-workspace-summary", children: renderSummary(masterContext) }) : null,
1051
+ /* @__PURE__ */ jsx8(
1052
+ MasterDetail,
1053
+ {
1054
+ master: renderMaster?.(masterContext) ?? defaultMaster,
1055
+ detail,
1056
+ detailOpen: selectedId != null,
1057
+ emptyDetail: renderEmptyDetail?.(masterContext) ?? emptyDetail,
1058
+ collapseClosedDetail,
1059
+ masterLabel: `${crm.type(type).labelPlural ?? crm.type(type).label} list`,
1060
+ detailLabel: record.detail ? `${record.detail.record.name} details` : "Record details",
1061
+ closeHref: hrefForList,
1062
+ onClose: () => select(null)
1063
+ }
1064
+ )
1065
+ ] });
1066
+ }
1067
+
1068
+ // src/ui/activity-feed.tsx
1069
+ import { useEffect as useEffect2, useRef as useRef2, useState as useState5 } from "preact/hooks";
1070
+ import { Button as Button4, Checkbox as Checkbox2, Input as Input2, Segmented, Textarea as Textarea2 } from "@odla-ai/ui/components";
1071
+ import { Fragment, jsx as jsx9, jsxs as jsxs9 } from "preact/jsx-runtime";
1072
+ var when = (at) => new Date(at).toISOString().replace("T", " ").slice(0, 16);
1073
+ function ActivityFeed({
1074
+ activities,
1075
+ onAddNote,
1076
+ onAddTask,
1077
+ onToggleTask,
1078
+ busy = false,
1079
+ focusedActivityId
1080
+ }) {
1081
+ const [mode, setMode] = useState5("note");
1082
+ const [body, setBody] = useState5("");
1083
+ const [dueAt, setDueAt] = useState5("");
1084
+ const [waitingOn, setWaitingOn] = useState5("");
1085
+ const focused = useRef2(null);
1086
+ useEffect2(() => {
1087
+ if (!focusedActivityId || !focused.current) return;
1088
+ focused.current.focus({ preventScroll: true });
1089
+ focused.current.scrollIntoView?.({ block: "center" });
1090
+ }, [activities, focusedActivityId]);
1091
+ const submit = () => {
1092
+ const text = body.trim();
1093
+ if (!text) return;
1094
+ if (mode === "note") onAddNote(text);
1095
+ else
1096
+ onAddTask({
1097
+ body: text,
1098
+ ...dueAt ? { dueAt: Date.parse(dueAt) } : {},
1099
+ ...waitingOn.trim() ? { waitingOn: waitingOn.trim() } : {}
1100
+ });
1101
+ setBody("");
1102
+ setDueAt("");
1103
+ setWaitingOn("");
1104
+ };
1105
+ return /* @__PURE__ */ jsxs9("div", { className: "crm-activity-feed", children: [
1106
+ /* @__PURE__ */ jsxs9("div", { className: "crm-activity-composer", children: [
1107
+ /* @__PURE__ */ jsx9(
1108
+ Segmented,
1109
+ {
1110
+ ariaLabel: "Entry kind",
1111
+ value: mode,
1112
+ onValueChange: (value) => setMode(value),
1113
+ options: [
1114
+ { value: "note", label: "Note" },
1115
+ { value: "task", label: "Follow-up" }
1116
+ ]
1117
+ }
1118
+ ),
1119
+ /* @__PURE__ */ jsx9(
1120
+ Textarea2,
1121
+ {
1122
+ "aria-label": mode === "note" ? "New note" : "New follow-up",
1123
+ value: body,
1124
+ rows: 2,
1125
+ placeholder: mode === "note" ? "What happened?" : "What needs doing?",
1126
+ onInput: (event) => setBody(event.currentTarget.value)
1127
+ }
1128
+ ),
1129
+ mode === "task" && /* @__PURE__ */ jsxs9("div", { className: "crm-activity-task-row", children: [
1130
+ /* @__PURE__ */ jsx9(
1131
+ Input2,
1132
+ {
1133
+ type: "date",
1134
+ "aria-label": "Due date",
1135
+ value: dueAt,
1136
+ onInput: (event) => setDueAt(event.currentTarget.value)
1137
+ }
1138
+ ),
1139
+ /* @__PURE__ */ jsx9(
1140
+ Input2,
1141
+ {
1142
+ "aria-label": "Waiting on",
1143
+ placeholder: "Waiting on\u2026",
1144
+ value: waitingOn,
1145
+ onInput: (event) => setWaitingOn(event.currentTarget.value)
1146
+ }
1147
+ )
1148
+ ] }),
1149
+ /* @__PURE__ */ jsx9(Button4, { mini: true, onClick: submit, disabled: busy || !body.trim(), children: "Add" })
1150
+ ] }),
1151
+ /* @__PURE__ */ jsx9("ul", { className: "crm-activity-list", children: activities.map((activity) => /* @__PURE__ */ jsxs9(
1152
+ "li",
1153
+ {
1154
+ className: `crm-activity kind-${activity.kind}`,
1155
+ ref: activity.id === focusedActivityId ? focused : void 0,
1156
+ tabIndex: activity.id === focusedActivityId ? -1 : void 0,
1157
+ "aria-current": activity.id === focusedActivityId ? "true" : void 0,
1158
+ children: [
1159
+ activity.kind === "task" ? /* @__PURE__ */ jsx9(
1160
+ Checkbox2,
1161
+ {
1162
+ checked: activity.status === "done",
1163
+ disabled: busy,
1164
+ onChange: (event) => onToggleTask(activity, event.currentTarget.checked),
1165
+ label: /* @__PURE__ */ jsxs9(Fragment, { children: [
1166
+ activity.body,
1167
+ activity.dueAt != null && /* @__PURE__ */ jsxs9("span", { className: "crm-activity-due", children: [
1168
+ " \xB7 due ",
1169
+ when(activity.dueAt).slice(0, 10)
1170
+ ] }),
1171
+ activity.waitingOn && /* @__PURE__ */ jsxs9("span", { className: "crm-activity-waiting", children: [
1172
+ " \xB7 waiting on ",
1173
+ activity.waitingOn
1174
+ ] })
1175
+ ] })
1176
+ }
1177
+ ) : /* @__PURE__ */ jsx9("p", { children: activity.body }),
1178
+ /* @__PURE__ */ jsxs9("span", { className: "crm-activity-meta", children: [
1179
+ activity.kind,
1180
+ " \xB7 ",
1181
+ when(activity.createdAt)
1182
+ ] })
1183
+ ]
1184
+ },
1185
+ activity.id
1186
+ )) })
1187
+ ] });
1188
+ }
1189
+
1190
+ // src/ui/bulk-bar.tsx
1191
+ import { Button as Button5 } from "@odla-ai/ui/components";
1192
+ import { jsx as jsx10, jsxs as jsxs10 } from "preact/jsx-runtime";
1193
+ function BulkBar({ selected, actions, onClear, busy = false }) {
1194
+ if (selected.length === 0) return null;
1195
+ return /* @__PURE__ */ jsxs10("div", { className: "crm-bulk-bar", role: "toolbar", "aria-label": "Bulk actions", children: [
1196
+ /* @__PURE__ */ jsxs10("span", { className: "crm-bulk-count", children: [
1197
+ selected.length,
1198
+ " selected"
1199
+ ] }),
1200
+ actions.map((action) => /* @__PURE__ */ jsx10(Button5, { mini: true, disabled: busy, onClick: () => void action.run(selected), children: action.label }, action.id)),
1201
+ /* @__PURE__ */ jsx10(Button5, { mini: true, variant: "ghost", onClick: onClear, disabled: busy, children: "Clear" })
1202
+ ] });
1203
+ }
1204
+
1205
+ // src/ui/view-editor.tsx
1206
+ import { useState as useState6 } from "preact/hooks";
1207
+ import { Button as Button6, Checkbox as Checkbox3, Input as Input3 } from "@odla-ai/ui/components";
1208
+ import { jsx as jsx11, jsxs as jsxs11 } from "preact/jsx-runtime";
1209
+ function ViewEditor({ current, onSave }) {
1210
+ const [open, setOpen] = useState6(false);
1211
+ const [name, setName] = useState6("");
1212
+ const [shared, setShared] = useState6(false);
1213
+ const [busy, setBusy] = useState6(false);
1214
+ const [error, setError] = useState6(null);
1215
+ if (!open)
1216
+ return /* @__PURE__ */ jsx11(Button6, { mini: true, variant: "ghost", onClick: () => setOpen(true), children: "Save view\u2026" });
1217
+ const save = async () => {
1218
+ setBusy(true);
1219
+ setError(null);
1220
+ try {
1221
+ await onSave({ ...current, name: name.trim(), shared });
1222
+ setOpen(false);
1223
+ setName("");
1224
+ setShared(false);
1225
+ } catch (failure) {
1226
+ setError(failure instanceof Error ? failure.message : String(failure));
1227
+ } finally {
1228
+ setBusy(false);
1229
+ }
1230
+ };
1231
+ return /* @__PURE__ */ jsxs11("div", { className: "crm-view-editor", role: "group", "aria-label": "Save view", children: [
1232
+ /* @__PURE__ */ jsx11(
1233
+ Input3,
1234
+ {
1235
+ "aria-label": "View name",
1236
+ placeholder: "View name",
1237
+ value: name,
1238
+ onInput: (event) => setName(event.currentTarget.value)
1239
+ }
1240
+ ),
1241
+ /* @__PURE__ */ jsx11(
1242
+ Checkbox3,
1243
+ {
1244
+ checked: shared,
1245
+ onChange: (event) => setShared(event.currentTarget.checked),
1246
+ label: "Shared"
1247
+ }
1248
+ ),
1249
+ /* @__PURE__ */ jsx11(Button6, { mini: true, onClick: save, disabled: busy || !name.trim(), children: "Save" }),
1250
+ /* @__PURE__ */ jsx11(Button6, { mini: true, variant: "ghost", onClick: () => setOpen(false), disabled: busy, children: "Cancel" }),
1251
+ error && /* @__PURE__ */ jsx11("span", { className: "crm-view-error", children: error })
1252
+ ] });
1253
+ }
1254
+
1255
+ export {
1256
+ useCrmQuery,
1257
+ useCrmRecord,
1258
+ CRM_NS,
1259
+ CRM_SLOTS,
1260
+ RESERVED_FIELDS,
1261
+ CRM_SCHEMA,
1262
+ sortFieldFor,
1263
+ columnsFor,
1264
+ defaultColumns,
1265
+ CrmList,
1266
+ CrmShell,
1267
+ IdentityCard,
1268
+ BillingCard,
1269
+ ChannelsCard,
1270
+ FieldsForm,
1271
+ StageControl,
1272
+ TagEditor,
1273
+ RecordPanel,
1274
+ CrmWorkspace,
1275
+ ActivityFeed,
1276
+ BulkBar,
1277
+ ViewEditor
1278
+ };
1279
+ //# sourceMappingURL=chunk-7LH3WGSU.js.map