@nivaro/react 0.2.54 → 0.2.55

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 (150) hide show
  1. package/dist/full.css +1 -1
  2. package/dist/hooks/useSessionRecorder.js +99 -98
  3. package/dist/index.d.ts +339 -2
  4. package/dist/index.js +644 -618
  5. package/dist/layout-editor.d.ts +4 -0
  6. package/dist/shared/components/BackgroundRunOffer.js +117 -0
  7. package/dist/shared/components/BroadcastView.js +214 -213
  8. package/dist/shared/components/CollectionBrowserView.js +2592 -2695
  9. package/dist/shared/components/ItemEditForm.js +4066 -3832
  10. package/dist/shared/components/JobsTray.js +100 -55
  11. package/dist/shared/components/MyWorkView.js +91 -89
  12. package/dist/shared/components/NotificationBell.js +80 -79
  13. package/dist/shared/components/ProfileView.js +568 -520
  14. package/dist/shared/components/RecordCompare.js +254 -0
  15. package/dist/shared/components/RecordReadView.js +599 -552
  16. package/dist/shared/components/ReportView.js +578 -543
  17. package/dist/shared/components/RowPresence.js +91 -0
  18. package/dist/shared/components/WidgetSlot.js +474 -458
  19. package/dist/shared/components/ai/AiAnalyticsView.js +16 -6
  20. package/dist/shared/components/ai/AskPanel.js +139 -119
  21. package/dist/shared/components/ai/FirstWeekCard.js +126 -0
  22. package/dist/shared/components/bulk/BulkFieldUpdate.js +289 -0
  23. package/dist/shared/components/bulk/bulk-field-options.js +45 -0
  24. package/dist/shared/components/charts/CumulativeFlowChart.js +176 -0
  25. package/dist/shared/components/chat/ChannelArchive.js +326 -0
  26. package/dist/shared/components/chat/ChannelExtras.js +141 -121
  27. package/dist/shared/components/chat/ChatAnalyticsView.js +112 -110
  28. package/dist/shared/components/chat/ChatPanel.js +1695 -1733
  29. package/dist/shared/components/chat/Composer.js +357 -303
  30. package/dist/shared/components/chat/MessageActions.js +92 -0
  31. package/dist/shared/components/chat/MessageFormat.js +290 -264
  32. package/dist/shared/components/chat/OutboxBubbles.js +146 -0
  33. package/dist/shared/components/chat/RequestIdCard.js +135 -0
  34. package/dist/shared/components/chat/RoomHeaderMenu.js +202 -0
  35. package/dist/shared/components/chat/RoomParts.js +213 -274
  36. package/dist/shared/components/chat/ScreenClip.js +184 -0
  37. package/dist/shared/components/chat/chat-core.js +402 -483
  38. package/dist/shared/components/chat/chat-drafts.js +59 -0
  39. package/dist/shared/components/chat/chat-emoji.js +348 -0
  40. package/dist/shared/components/chat/chat-local-time.js +41 -0
  41. package/dist/shared/components/chat/chat-outbox.js +303 -0
  42. package/dist/shared/components/chat/chat-window.js +163 -0
  43. package/dist/shared/components/chat/huddle/HuddleControls.js +143 -0
  44. package/dist/shared/components/chat/huddle/HuddleStage.js +164 -0
  45. package/dist/shared/components/chat/huddle/huddle-store.js +342 -0
  46. package/dist/shared/components/chat/huddle/useHuddle.js +58 -0
  47. package/dist/shared/components/help-videos/api.js +76 -73
  48. package/dist/shared/components/item-edit/CatalogPickerField.js +113 -112
  49. package/dist/shared/components/item-edit/ChangeReasonDialog.js +13 -13
  50. package/dist/shared/components/item-edit/FieldAffordances.js +2 -2
  51. package/dist/shared/components/item-edit/FieldRenderer.js +248 -245
  52. package/dist/shared/components/item-edit/FieldRow.js +292 -279
  53. package/dist/shared/components/item-edit/GroupSection.js +698 -574
  54. package/dist/shared/components/item-edit/InlineTableField.js +3 -1
  55. package/dist/shared/components/item-edit/RelationCombobox.js +186 -176
  56. package/dist/shared/components/item-edit/entry/EntryStrip.js +54 -0
  57. package/dist/shared/components/item-edit/entry/entry-keys.js +27 -0
  58. package/dist/shared/components/item-edit/entry/useEntryKeyboard.js +70 -0
  59. package/dist/shared/components/item-edit/entry/useEntryMode.js +59 -0
  60. package/dist/shared/components/item-edit/form-structure-ui.js +105 -0
  61. package/dist/shared/components/item-edit/lazy-sections.js +36 -0
  62. package/dist/shared/components/item-edit/lock-why.js +39 -32
  63. package/dist/shared/components/item-edit/picker-miss.js +26 -0
  64. package/dist/shared/components/item-edit/placement-behaviour.js +196 -0
  65. package/dist/shared/components/item-edit/useFormUsageBeacon.js +51 -45
  66. package/dist/shared/components/layout-editor/AddPanel.js +134 -117
  67. package/dist/shared/components/layout-editor/ChangeNote.js +25 -24
  68. package/dist/shared/components/layout-editor/CollabParts.js +80 -75
  69. package/dist/shared/components/layout-editor/DesignNode.js +126 -120
  70. package/dist/shared/components/layout-editor/EditorCanvas.js +30 -27
  71. package/dist/shared/components/layout-editor/EditorTopBar.js +203 -192
  72. package/dist/shared/components/layout-editor/HistoryPanel.js +120 -104
  73. package/dist/shared/components/layout-editor/LayoutEditor.js +768 -671
  74. package/dist/shared/components/layout-editor/RestorePiece.js +143 -0
  75. package/dist/shared/components/layout-editor/StagedChip.js +22 -0
  76. package/dist/shared/components/layout-editor/StructureMenu.js +237 -186
  77. package/dist/shared/components/layout-editor/collab/FormFollowShare.js +149 -103
  78. package/dist/shared/components/layout-editor/collab/collab-api.js +29 -26
  79. package/dist/shared/components/layout-editor/dnd/LayoutDndProvider.js +81 -80
  80. package/dist/shared/components/layout-editor/dnd/node.js +69 -57
  81. package/dist/shared/components/layout-editor/draft-recovery.js +113 -43
  82. package/dist/shared/components/layout-editor/entry/PlacementPresets.js +149 -0
  83. package/dist/shared/components/layout-editor/entry/entry-editor-context.js +10 -0
  84. package/dist/shared/components/layout-editor/entry/presets.js +71 -0
  85. package/dist/shared/components/layout-editor/entry/tab-order.js +110 -0
  86. package/dist/shared/components/layout-editor/entry/theme-preview.js +37 -0
  87. package/dist/shared/components/layout-editor/grid-guides.js +142 -0
  88. package/dist/shared/components/layout-editor/inspector/BehaviourSettings.js +402 -0
  89. package/dist/shared/components/layout-editor/inspector/BulkInspector.js +61 -57
  90. package/dist/shared/components/layout-editor/inspector/GroupInspector.js +247 -151
  91. package/dist/shared/components/layout-editor/inspector/Inspector.js +176 -155
  92. package/dist/shared/components/layout-editor/inspector/LookTab.js +167 -116
  93. package/dist/shared/components/layout-editor/inspector/PickerMisses.js +129 -0
  94. package/dist/shared/components/layout-editor/inspector/PickerTab.js +84 -82
  95. package/dist/shared/components/layout-editor/inspector/RulesTab.js +66 -62
  96. package/dist/shared/components/layout-editor/inspector/kit.js +69 -68
  97. package/dist/shared/components/layout-editor/outputs/OutputsButton.js +254 -0
  98. package/dist/shared/components/layout-editor/outputs/help-sheet.js +154 -0
  99. package/dist/shared/components/layout-editor/outputs/output-api.js +61 -0
  100. package/dist/shared/components/layout-editor/overview/describe-conditions.js +6 -0
  101. package/dist/shared/components/layout-editor/overview/purpose.js +15 -13
  102. package/dist/shared/components/layout-editor/parity-keys.json.js +1 -1
  103. package/dist/shared/components/layout-editor/restore-piece.js +192 -0
  104. package/dist/shared/components/layout-editor/section-structure.js +241 -0
  105. package/dist/shared/components/layout-editor/settings/EntrySection.js +296 -0
  106. package/dist/shared/components/layout-editor/settings/FormSettings.js +41 -39
  107. package/dist/shared/components/layout-editor/settings/ResolveSettings.js +255 -0
  108. package/dist/shared/components/layout-editor/settings/ToolsSection.js +147 -111
  109. package/dist/shared/components/layout-editor/settings/WhoSection.js +29 -26
  110. package/dist/shared/components/layout-editor/settings/settings-registry.js +114 -17
  111. package/dist/shared/components/layout-editor/staged-api.js +64 -0
  112. package/dist/shared/components/layout-editor/structure-ops.js +81 -77
  113. package/dist/shared/components/layout-editor/testing/TestingPanels.js +65 -60
  114. package/dist/shared/components/layout-editor/testing/testing-model.js +198 -185
  115. package/dist/shared/components/layout-editor/toolbar-kit.js +11 -11
  116. package/dist/shared/components/layout-editor/usage/AbTestSection.js +355 -0
  117. package/dist/shared/components/layout-editor/usage/UsagePanel.js +144 -70
  118. package/dist/shared/components/layout-editor/usage/ab-api.js +86 -0
  119. package/dist/shared/components/layout-editor/usage/suggest-collapse.js +32 -0
  120. package/dist/shared/components/layout-editor/useLayoutDraft.js +25 -24
  121. package/dist/shared/components/notifications/NotificationAnalyticsView.js +148 -77
  122. package/dist/shared/components/panels/ErpFailureBanner.js +18 -17
  123. package/dist/shared/components/panels/ExternalRequestsChip.js +63 -62
  124. package/dist/shared/components/pipeline/PipelineEditorView.js +106 -104
  125. package/dist/shared/components/profile/AgentsPanel.js +479 -0
  126. package/dist/shared/components/queue/QueueWorklist.js +1026 -1040
  127. package/dist/shared/components/support/SupportAnalytics.js +171 -0
  128. package/dist/shared/components/support/SupportView.js +75 -73
  129. package/dist/shared/lib/background-runs.js +150 -0
  130. package/dist/shared/lib/delivery-ack.js +9 -0
  131. package/dist/shared/lib/entry-badge-style.js +13 -0
  132. package/dist/shared/lib/entry-config.js +71 -0
  133. package/dist/shared/lib/form-sections.js +64 -0
  134. package/dist/shared/lib/form-structure.js +99 -0
  135. package/dist/shared/lib/form-usage-beacon.js +101 -52
  136. package/dist/shared/lib/jobs-tray.js +25 -23
  137. package/dist/shared/lib/keyset-pager.js +37 -0
  138. package/dist/shared/lib/layout-ops.js +84 -82
  139. package/dist/shared/lib/layout-resolve.js +113 -0
  140. package/dist/shared/lib/placement-behaviour.js +144 -0
  141. package/dist/shared/lib/prefetch.js +149 -0
  142. package/dist/shared/lib/presence-hooks.js +199 -0
  143. package/dist/shared/lib/record-outbox.js +82 -81
  144. package/dist/shared/lib/replay-socket.js +93 -0
  145. package/dist/shared/lib/request-gate.js +51 -50
  146. package/dist/shared/lib/room-gaps.js +96 -0
  147. package/dist/shared/lib/search-log.js +27 -0
  148. package/dist/shared/lib/socket-realtime.js +101 -80
  149. package/dist/shared/lib/worker-socket.js +200 -0
  150. package/package.json +1 -1
@@ -0,0 +1,144 @@
1
+ function c(t) {
2
+ if (typeof t == "string")
3
+ try {
4
+ const e = JSON.parse(t);
5
+ return e && typeof e == "object" && !Array.isArray(e) ? e : {};
6
+ } catch {
7
+ return {};
8
+ }
9
+ return t && typeof t == "object" && !Array.isArray(t) ? t : {};
10
+ }
11
+ const u = (t) => Array.isArray(t) ? t.filter((e) => typeof e == "string" && e !== "") : [], f = (t) => typeof t == "string" && t.trim() !== "" ? t : void 0;
12
+ function y(t) {
13
+ const e = c(t).text_rules;
14
+ if (!Array.isArray(e)) return [];
15
+ const r = [];
16
+ for (const n of e) {
17
+ if (!n || typeof n != "object") continue;
18
+ const i = n, s = i.type === "role" ? "role" : i.type === "pipeline_state" ? "pipeline_state" : null;
19
+ if (!s) continue;
20
+ const o = { type: s };
21
+ typeof i.pipeline_id == "string" && i.pipeline_id && (o.pipeline_id = i.pipeline_id), s === "pipeline_state" ? o.state_keys = u(i.state_keys) : o.role_ids = u(i.role_ids);
22
+ const l = f(i.label), a = f(i.note);
23
+ l !== void 0 && (o.label = l), a !== void 0 && (o.note = a), r.push(o);
24
+ }
25
+ return r;
26
+ }
27
+ function p(t, e) {
28
+ if (t.type === "pipeline_state") {
29
+ const n = t.state_keys ?? [];
30
+ return !!e.stateKey && n.includes(e.stateKey);
31
+ }
32
+ const r = (t.role_ids ?? []).map((n) => n.toUpperCase());
33
+ return !!e.roleId && r.includes(e.roleId.toUpperCase());
34
+ }
35
+ function h(t, e) {
36
+ const r = {};
37
+ for (const n of y(t))
38
+ if (p(n, e) && (r.label === void 0 && n.label !== void 0 && (r.label = n.label), r.note === void 0 && n.note !== void 0 && (r.note = n.note), r.label !== void 0 && r.note !== void 0))
39
+ break;
40
+ return r;
41
+ }
42
+ function k(t) {
43
+ const e = c(t);
44
+ return e.recommended === !0 && e.required !== !0;
45
+ }
46
+ function F(t) {
47
+ return t == null || t === "" ? !0 : Array.isArray(t) ? t.length === 0 : typeof t == "string" && t.trim() === "";
48
+ }
49
+ function m(t) {
50
+ const e = c(t).confirm_change;
51
+ return e === !0 ? { state_keys: [] } : !e || typeof e != "object" || Array.isArray(e) ? null : { state_keys: u(e.state_keys) };
52
+ }
53
+ function U(t, e) {
54
+ const r = m(t);
55
+ return r ? r.state_keys.length === 0 ? !0 : !!e && r.state_keys.includes(e) : !1;
56
+ }
57
+ const d = 1e6;
58
+ function g(t) {
59
+ const e = c(t).counter;
60
+ if (!e || typeof e != "object" || Array.isArray(e)) return null;
61
+ const r = e, n = Number(r.limit);
62
+ return {
63
+ unit: r.unit === "words" ? "words" : "characters",
64
+ limit: r.limit != null && r.limit !== "" && Number.isFinite(n) && n > 0 ? Math.min(d, Math.floor(n)) : null
65
+ };
66
+ }
67
+ function _(t) {
68
+ if (t == null) return "";
69
+ const e = typeof t == "string" ? t : typeof t == "object" ? JSON.stringify(t) : String(t);
70
+ return /<[a-z!/][^>]*>/i.test(e) ? e.replace(/<(br|\/p|\/div|\/li|\/h[1-6])\b[^>]*>/gi, " ").replace(/<[^>]*>/g, "").replace(/&nbsp;/g, " ").replace(/&(amp|lt|gt|quot|#39);/g, "x") : e;
71
+ }
72
+ function b(t, e) {
73
+ const r = _(t);
74
+ if (e === "words") {
75
+ const n = r.trim();
76
+ return n ? n.split(/\s+/).length : 0;
77
+ }
78
+ return r.length;
79
+ }
80
+ function L(t, e) {
81
+ return e == null ? "ok" : t > e ? "over" : t >= e * 0.9 ? "near" : "ok";
82
+ }
83
+ function x(t, e, r) {
84
+ const n = g(e);
85
+ if (!n || n.limit == null) return null;
86
+ const i = b(t, n.unit);
87
+ return i <= n.limit ? null : `${r} is over its limit of ${n.limit.toLocaleString("en-US")} ${n.unit} (${i.toLocaleString("en-US")})`;
88
+ }
89
+ const S = /^__calc_[a-z0-9_]{1,40}__$/;
90
+ function D(t) {
91
+ return S.test(t);
92
+ }
93
+ function w(t) {
94
+ const e = new Set(t);
95
+ for (let r = 1; ; r++) {
96
+ const n = `__calc_${r}__`;
97
+ if (!e.has(n)) return n;
98
+ }
99
+ }
100
+ const A = ["", "integer", "number", "currency", "percent"];
101
+ function O(t) {
102
+ const e = c(t).display_formula;
103
+ if (!e || typeof e != "object" || Array.isArray(e)) return null;
104
+ const r = e, n = A.includes(r.format) ? r.format : "", i = typeof r.currency == "string" && /^[A-Z]{3}$/.test(r.currency) ? r.currency : null;
105
+ return { expr: typeof r.expr == "string" ? r.expr : "", format: n, currency: i };
106
+ }
107
+ function j(t, e) {
108
+ if (t == null || !Number.isFinite(t)) return "—";
109
+ switch (e.format) {
110
+ case "integer":
111
+ return Math.round(t).toLocaleString("en-US");
112
+ case "currency":
113
+ try {
114
+ return t.toLocaleString("en-US", { style: "currency", currency: e.currency || "USD" });
115
+ } catch {
116
+ return t.toLocaleString("en-US", { style: "currency", currency: "USD" });
117
+ }
118
+ case "percent":
119
+ return `${t.toLocaleString("en-US", { maximumFractionDigits: 1 })}%`;
120
+ case "number":
121
+ return t.toLocaleString("en-US", { minimumFractionDigits: 2, maximumFractionDigits: 2 });
122
+ default:
123
+ return t.toLocaleString("en-US", { maximumFractionDigits: 4 });
124
+ }
125
+ }
126
+ export {
127
+ d as COUNTER_LIMIT_MAX,
128
+ U as confirmChangeApplies,
129
+ m as confirmChangeOf,
130
+ b as countValue,
131
+ x as counterError,
132
+ g as counterOf,
133
+ L as counterState,
134
+ O as displayFormulaOf,
135
+ j as formatDisplayValue,
136
+ D as isDisplayFormulaField,
137
+ F as isEmptyValue,
138
+ k as isRecommended,
139
+ w as nextDisplayFormulaField,
140
+ c as overridesObject,
141
+ h as resolvePlacementText,
142
+ p as textRuleMatches,
143
+ y as textRulesOf
144
+ };
@@ -0,0 +1,149 @@
1
+ import { useRef as E, useEffect as T } from "react";
2
+ import { get as v } from "./commands.js";
3
+ const q = 30, w = 150, R = 3e4;
4
+ let d = [];
5
+ function L(e = Date.now(), t = q) {
6
+ return d = d.filter((r) => e - r < 6e4), d.length >= t ? !1 : (d.push(e), !0);
7
+ }
8
+ function b() {
9
+ if (typeof navigator > "u") return !1;
10
+ const e = navigator.connection;
11
+ return !!(e != null && e.saveData);
12
+ }
13
+ function I(e, t) {
14
+ return {
15
+ item: ["item", e, String(t)],
16
+ layout: ["active-layout", e, null, String(t)]
17
+ };
18
+ }
19
+ function S(e, t, r) {
20
+ const n = e.getQueryState(t);
21
+ return n ? n.fetchStatus === "fetching" ? !0 : n.dataUpdatedAt > 0 && r - n.dataUpdatedAt < R : !1;
22
+ }
23
+ function h(e, t, r, n = Date.now()) {
24
+ return b() || S(e, t, n) || !L(n) ? !1 : (e.prefetchQuery({ queryKey: t, queryFn: r, staleTime: R }), !0);
25
+ }
26
+ function P(e, t, r, n, u = {}) {
27
+ if (!r || n == null || n === "" || n === "new") return 0;
28
+ const c = I(r, n);
29
+ let f = 0;
30
+ return h(
31
+ e,
32
+ c.item,
33
+ () => t.request(v(`/items/${r}/${n}`)).then((s) => s.data)
34
+ ) && f++, u.layout !== !1 && h(
35
+ e,
36
+ c.layout,
37
+ () => t.request(
38
+ v("/collection-layouts/active", { collection: r, item: String(n) })
39
+ ).then((s) => s.data ?? null)
40
+ ) && f++, f;
41
+ }
42
+ function _(e, t = 2e3) {
43
+ if (typeof window > "u") return () => {
44
+ };
45
+ const r = window;
46
+ if (r.requestIdleCallback) {
47
+ const u = r.requestIdleCallback(e, { timeout: t });
48
+ return () => {
49
+ var c;
50
+ return (c = r.cancelIdleCallback) == null ? void 0 : c.call(r, u);
51
+ };
52
+ }
53
+ const n = setTimeout(e, 300);
54
+ return () => clearTimeout(n);
55
+ }
56
+ function D(e) {
57
+ const t = E(e);
58
+ t.current = e;
59
+ const r = JSON.stringify(e.queryKey);
60
+ T(() => {
61
+ if (e.enabled)
62
+ return _(() => {
63
+ const n = t.current;
64
+ h(n.qc, n.queryKey, n.queryFn);
65
+ });
66
+ }, [e.enabled, r]);
67
+ }
68
+ function k(e, t) {
69
+ const r = e.getAttribute("href");
70
+ if (!r || r.startsWith("#") || e.hasAttribute("download") || e.target && e.target !== "_self") return null;
71
+ try {
72
+ const n = new URL(r, t);
73
+ return n.origin !== t ? null : n.pathname + n.search;
74
+ } catch {
75
+ return null;
76
+ }
77
+ }
78
+ function p(e) {
79
+ if (typeof document > "u") return () => {
80
+ };
81
+ const t = e.root ?? document, r = window.location.origin;
82
+ let n = null, u = null;
83
+ const c = () => {
84
+ n && clearTimeout(n), n = null, u = null;
85
+ }, f = (l) => {
86
+ var i, g;
87
+ const o = k(l, r);
88
+ if (!o) return;
89
+ try {
90
+ (i = e.preloadRoute) == null || i.call(e, o);
91
+ } catch {
92
+ }
93
+ const a = (g = e.matchRecord) == null ? void 0 : g.call(e, o.split("?")[0]);
94
+ a && e.client && P(e.qc, e.client, a.collection, a.id);
95
+ }, s = (l) => {
96
+ var a, i;
97
+ const o = (i = (a = l.target) == null ? void 0 : a.closest) == null ? void 0 : i.call(a, "a[href]");
98
+ !o || o === u || (c(), u = o, n = setTimeout(() => {
99
+ n = null, f(o);
100
+ }, w));
101
+ }, m = (l) => {
102
+ const o = l.relatedTarget;
103
+ u && o && u.contains(o) || c();
104
+ }, y = (l) => {
105
+ var a, i;
106
+ const o = (i = (a = l.target) == null ? void 0 : a.closest) == null ? void 0 : i.call(a, "a[href]");
107
+ o && f(o);
108
+ };
109
+ return t.addEventListener("pointerover", s, { passive: !0 }), t.addEventListener("pointerout", m, { passive: !0 }), t.addEventListener("focusin", y), () => {
110
+ c(), t.removeEventListener("pointerover", s), t.removeEventListener("pointerout", m), t.removeEventListener("focusin", y);
111
+ };
112
+ }
113
+ function K(e) {
114
+ const t = E(e);
115
+ t.current = e;
116
+ const r = e.enabled !== !1;
117
+ T(() => {
118
+ if (r)
119
+ return p({
120
+ qc: t.current.qc,
121
+ get client() {
122
+ return t.current.client;
123
+ },
124
+ matchRecord: (n) => {
125
+ var u, c;
126
+ return ((c = (u = t.current).matchRecord) == null ? void 0 : c.call(u, n)) ?? null;
127
+ },
128
+ preloadRoute: (n) => {
129
+ var u, c;
130
+ return (c = (u = t.current).preloadRoute) == null ? void 0 : c.call(u, n);
131
+ },
132
+ root: t.current.root
133
+ });
134
+ }, [r]);
135
+ }
136
+ export {
137
+ w as INTENT_DWELL_MS,
138
+ q as PREFETCH_PER_MINUTE,
139
+ R as PREFETCH_STALE_MS,
140
+ p as installIntentPrefetch,
141
+ k as linkPath,
142
+ h as prefetchKey,
143
+ P as prefetchRecord,
144
+ I as recordQueryKeys,
145
+ L as takePrefetchToken,
146
+ D as useIdleNextPage,
147
+ K as useIntentPrefetch,
148
+ _ as whenIdle
149
+ };
@@ -0,0 +1,199 @@
1
+ import { useQuery as k } from "@tanstack/react-query";
2
+ import { useState as C, useEffect as S, useCallback as O, useMemo as j, useRef as R } from "react";
3
+ import { useOptionalNivaroClient as I } from "../context.js";
4
+ import { post as L, get as _ } from "./commands.js";
5
+ import { useLiveEvent as E } from "./live.js";
6
+ import { useMyUserId as T, useRecordSignalPeers as q, useRecordSignalWhile as K } from "./presence-signals.js";
7
+ import { useOptionalRealtime as A, useRealtimeStatus as F } from "./realtime.js";
8
+ function P(i, c) {
9
+ const e = F(), s = R(!1), r = R(i);
10
+ r.current = i, S(() => {
11
+ c && ((e == null ? void 0 : e.state) === "reconnecting" ? s.current = !0 : s.current && (s.current = !1, r.current()));
12
+ }, [e == null ? void 0 : e.state, c]);
13
+ }
14
+ function G(i, c) {
15
+ const e = A(), s = T(), r = c == null ? "" : String(c), t = !!e && !!i && !!r && r !== "new", [l, g] = C([]), v = O(() => {
16
+ t && (e == null || e.emit("record:join", { collection: i, item: r }));
17
+ }, [e, i, r, t]);
18
+ return S(() => {
19
+ if (g([]), !!t)
20
+ return v(), () => e == null ? void 0 : e.emit("record:leave", { collection: i, item: r });
21
+ }, [e, i, r, t, v]), P(v, t), E(
22
+ "record:viewers",
23
+ (u) => {
24
+ Array.isArray(u == null ? void 0 : u.viewers) && g(u.viewers);
25
+ },
26
+ {
27
+ enabled: t,
28
+ includeOwn: !0,
29
+ // An older server names no record: accept, as the hosts' own hooks do.
30
+ match: (u) => (u == null ? void 0 : u.collection) == null || (u == null ? void 0 : u.item) == null ? !0 : u.collection === i && String(u.item) === r
31
+ }
32
+ ), { viewers: j(
33
+ () => l.filter((u) => !s || String(u.id).toUpperCase() !== s.toUpperCase()),
34
+ [l, s]
35
+ ) };
36
+ }
37
+ function H(i, c, e = {}) {
38
+ const s = A(), r = T(), t = c == null ? "" : String(c), l = !!s && !!i && !!t && t !== "new", [g, v] = C({});
39
+ S(() => v({}), [i, t]), E(
40
+ "field:editing",
41
+ (o) => {
42
+ typeof (o == null ? void 0 : o.field) == "string" && v((b) => {
43
+ const y = { ...b }, d = o.user;
44
+ return d && (!r || String(d.id).toUpperCase() !== r.toUpperCase()) ? y[o.field] = d : delete y[o.field], y;
45
+ });
46
+ },
47
+ {
48
+ enabled: l,
49
+ includeOwn: !0,
50
+ match: (o) => (o == null ? void 0 : o.collection) == null || (o == null ? void 0 : o.item) == null ? !0 : o.collection === i && String(o.item) === t
51
+ }
52
+ );
53
+ const p = O(
54
+ (o) => {
55
+ l && o && (s == null || s.emit("field:focus", { field: o, collection: i, item: t }));
56
+ },
57
+ [s, i, t, l]
58
+ ), u = O(
59
+ (o) => {
60
+ l && o && (s == null || s.emit("field:blur", { field: o, collection: i, item: t }));
61
+ },
62
+ [s, i, t, l]
63
+ );
64
+ return S(() => {
65
+ if (!l || !e.capture || typeof document > "u") return;
66
+ const o = (d) => {
67
+ var n, w, a;
68
+ return (a = (w = (n = d.target) == null ? void 0 : n.closest) == null ? void 0 : w.call(n, "[data-field]")) == null ? void 0 : a.getAttribute("data-field");
69
+ }, b = (d) => {
70
+ const n = o(d);
71
+ n && p(n);
72
+ }, y = (d) => {
73
+ const n = o(d);
74
+ n && u(n);
75
+ };
76
+ return document.addEventListener("focusin", b), document.addEventListener("focusout", y), () => {
77
+ document.removeEventListener("focusin", b), document.removeEventListener("focusout", y);
78
+ };
79
+ }, [l, e.capture, p, u]), { editing: g, focus: p, blur: u };
80
+ }
81
+ function J(i, c) {
82
+ const e = q(i, c, "note"), [s, r] = C(!1);
83
+ K(i, c, "note", s);
84
+ const t = R(null), l = O((g) => {
85
+ t.current && clearTimeout(t.current), t.current = null, r(g), g && (t.current = setTimeout(() => r(!1), 6e3));
86
+ }, []);
87
+ return S(
88
+ () => () => {
89
+ t.current && clearTimeout(t.current);
90
+ },
91
+ []
92
+ ), { typing: e, setTyping: l };
93
+ }
94
+ const $ = {};
95
+ function x(i, c) {
96
+ return `${i}:${c}`;
97
+ }
98
+ function B(i) {
99
+ const c = I(), e = [...new Set(i.map((r) => r.toUpperCase()))].sort().join(","), { data: s } = k({
100
+ queryKey: ["presence-visible-people", e],
101
+ queryFn: () => c.request(
102
+ _(`/presence/people?ids=${encodeURIComponent(e.split(",").slice(0, 100).join(","))}`)
103
+ ).then((r) => new Set((r.data ?? []).map((t) => String(t.user_id).toUpperCase()))).catch(() => /* @__PURE__ */ new Set()),
104
+ enabled: !!c && e.length > 0,
105
+ staleTime: 5 * 6e4
106
+ });
107
+ return e.length === 0 ? null : s ?? null;
108
+ }
109
+ function U(i) {
110
+ const c = /* @__PURE__ */ new Map();
111
+ for (const s of i) {
112
+ const r = s.indexOf(":");
113
+ if (r <= 0) continue;
114
+ const t = s.slice(0, r), l = c.get(t) ?? [];
115
+ l.push(s.slice(r + 1)), c.set(t, l);
116
+ }
117
+ const e = [];
118
+ for (const [s, r] of c)
119
+ for (let t = 0; t < r.length; t += 100)
120
+ e.push({ collection: s, items: r.slice(t, t + 100) });
121
+ return e;
122
+ }
123
+ function X(i, c = {}) {
124
+ const e = A(), s = I(), r = T(), t = c.enabled !== !1 && i.length > 0, l = i.filter((n) => n.collection && n.item != null).map((n) => x(n.collection, n.item)).join(""), [g, v] = C({}), [p, u] = C({}), o = R(/* @__PURE__ */ new Set()), b = O(() => {
125
+ if (!(!e || !t))
126
+ for (const n of U(o.current)) e.emit("presence:watch", n);
127
+ }, [e, t]);
128
+ S(() => {
129
+ if (!e || !t) return;
130
+ const n = new Set(l.split("").filter(Boolean)), w = [...n].filter((f) => !o.current.has(f)), a = [...o.current].filter((f) => !n.has(f));
131
+ o.current = n;
132
+ for (const f of U(w)) e.emit("presence:watch", f);
133
+ if (a.length) {
134
+ for (const f of U(a)) e.emit("presence:unwatch", f);
135
+ v((f) => {
136
+ const m = { ...f };
137
+ for (const h of a) delete m[h];
138
+ return m;
139
+ });
140
+ }
141
+ }, [e, l, t]), S(() => {
142
+ if (e)
143
+ return () => {
144
+ o.current.size && e.emit("presence:unwatch", {}), o.current = /* @__PURE__ */ new Set(), v({});
145
+ };
146
+ }, [e]), P(b, !!e && t), E(
147
+ "presence:rows",
148
+ (n) => {
149
+ if (!(n != null && n.collection) || (n == null ? void 0 : n.item) == null) return;
150
+ const w = x(String(n.collection), String(n.item)), a = (Array.isArray(n.user_ids) ? n.user_ids : []).map(String);
151
+ v((f) => {
152
+ const m = { ...f };
153
+ return a.length ? m[w] = a : delete m[w], m;
154
+ });
155
+ },
156
+ { enabled: !!e && t, includeOwn: !0 }
157
+ ), S(() => {
158
+ if (e || !s || !t) return;
159
+ const n = c.pollMs ?? 3e4, w = l.split("").filter(Boolean).slice(0, 100).map((h) => {
160
+ const M = h.indexOf(":");
161
+ return { collection: h.slice(0, M), item_id: h.slice(M + 1) };
162
+ });
163
+ let a = !0;
164
+ const f = () => {
165
+ s.request(L("/presence/record-viewers", { pairs: w })).then((h) => {
166
+ a && u(h.data ?? {});
167
+ }).catch(() => {
168
+ });
169
+ };
170
+ f();
171
+ const m = n > 0 ? setInterval(f, n) : null;
172
+ return () => {
173
+ a = !1, m && clearInterval(m);
174
+ };
175
+ }, [e, s, l, t, c.pollMs]);
176
+ const y = j(() => [...new Set(Object.values(g).flat())], [g]), d = B(y);
177
+ return j(() => {
178
+ if (!t) return $;
179
+ const n = {}, w = r == null ? void 0 : r.toUpperCase();
180
+ if (e)
181
+ for (const [a, f] of Object.entries(g)) {
182
+ const m = f.filter((h) => h.toUpperCase() !== w);
183
+ m.length !== 0 && (n[a] = {
184
+ ids: d ? m.filter((h) => d.has(h.toUpperCase())) : [],
185
+ count: m.length
186
+ });
187
+ }
188
+ else
189
+ for (const [a, f] of Object.entries(p)) f > 1 && (n[a] = { ids: [], count: f - 1 });
190
+ return n;
191
+ }, [t, e, g, p, d, r]);
192
+ }
193
+ export {
194
+ x as rowPresenceKey,
195
+ H as useFieldFocus,
196
+ G as usePresence,
197
+ X as useRowPresence,
198
+ J as useTyping
199
+ };