@nivaro/react 0.3.32 → 0.3.34

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 (31) hide show
  1. package/dist/full.css +1 -1
  2. package/dist/hooks/useSessionRecorder.js +1 -1
  3. package/dist/index.d.ts +89 -8
  4. package/dist/index.js +338 -328
  5. package/dist/shared/components/ForceReloadBanner.js +56 -46
  6. package/dist/shared/components/ItemEditForm.js +5637 -5442
  7. package/dist/shared/components/ProfileView.js +46 -44
  8. package/dist/shared/components/ReportView.js +208 -205
  9. package/dist/shared/components/SearchHighlight.js +30 -0
  10. package/dist/shared/components/TipLayer.js +39 -37
  11. package/dist/shared/components/item-edit/CatalogPickerField.js +1484 -1232
  12. package/dist/shared/components/item-edit/FieldAffordances.js +370 -203
  13. package/dist/shared/components/item-edit/FieldRow.js +396 -354
  14. package/dist/shared/components/item-edit/PersonalDefaultPill.js +50 -0
  15. package/dist/shared/components/item-edit/RelationCombobox.js +42 -41
  16. package/dist/shared/components/item-edit/catalog-search.js +34 -0
  17. package/dist/shared/components/item-edit/entry/useEntryKeyboard.js +34 -34
  18. package/dist/shared/components/item-edit/personal-default-checks.js +134 -0
  19. package/dist/shared/components/item-edit/usePersonalDefaults.js +358 -0
  20. package/dist/shared/components/layout-editor/inspector/BehaviourSettings.js +130 -111
  21. package/dist/shared/components/layout-editor/inspector/RulesTab.js +76 -75
  22. package/dist/shared/components/layout-editor/parity-keys.json.js +1 -1
  23. package/dist/shared/components/profile/MyDefaultsCard.js +667 -0
  24. package/dist/shared/components/profile/PersonProfile.js +42 -40
  25. package/dist/shared/components/profile/my-defaults-labels.js +76 -0
  26. package/dist/shared/components/profile/primitives.js +72 -67
  27. package/dist/shared/lib/personal-defaults.js +76 -0
  28. package/dist/shared/lib/request-gate.js +78 -76
  29. package/dist/shared/lib/search-text.js +111 -0
  30. package/dist/shared/lib/unsaved-work.js +86 -38
  31. package/package.json +1 -1
@@ -0,0 +1,358 @@
1
+ import { useQueryClient as Le, useQuery as Ne } from "@tanstack/react-query";
2
+ import { useRef as h, useMemo as Pe, useState as ne, useEffect as L, useCallback as a, createContext as Qe, useContext as ze } from "react";
3
+ import { useNivaroClient as Te } from "../../context.js";
4
+ import { get as Ye, del as Ee, put as Re } from "../../lib/commands.js";
5
+ import { isEmptyValue as w, pickDefault as se, cascadeParents as V, orderTopDown as He, captureContext as Be } from "../../lib/personal-defaults.js";
6
+ import { titleCase as Je } from "../../lib/utils.js";
7
+ const We = Qe(null);
8
+ function rt() {
9
+ return ze(We);
10
+ }
11
+ function N(u) {
12
+ return ["field-defaults", u];
13
+ }
14
+ function J(u) {
15
+ return Array.isArray(u) ? u.map(J) : u && typeof u == "object" && "id" in u ? u.id : u;
16
+ }
17
+ function S(u, f) {
18
+ if (w(u) && w(f)) return !0;
19
+ const m = (P) => {
20
+ const x = J(P);
21
+ return Array.isArray(x) ? x.map((b) => String(b)).sort() : String(x);
22
+ };
23
+ return JSON.stringify(m(u)) === JSON.stringify(m(f));
24
+ }
25
+ function nt(u, f) {
26
+ let m = 0;
27
+ for (const [P, x] of u) {
28
+ const b = f.get(P);
29
+ for (const k of x) b != null && b.has(String(k)) || m++;
30
+ }
31
+ return m;
32
+ }
33
+ function Ge(u) {
34
+ return Array.isArray(u) ? u : w(u) ? [] : [u];
35
+ }
36
+ function st(u) {
37
+ const { collection: f, isNew: m, disabled: P, enabled: x = !0, ready: b = !0 } = u, k = Te(), F = Le(), d = h(u);
38
+ d.current = u;
39
+ const { data: Q } = Ne({
40
+ queryKey: N(f),
41
+ queryFn: () => k.request(Ye("/field-defaults/mine", { collection: f })).then((e) => e.data ?? []),
42
+ staleTime: 5 * 6e4,
43
+ enabled: !!f && x
44
+ }), z = Pe(() => {
45
+ const e = /* @__PURE__ */ new Map();
46
+ for (const t of Q ?? []) {
47
+ const r = e.get(t.field) ?? [];
48
+ r.push(t), e.set(t.field, r);
49
+ }
50
+ return e;
51
+ }, [Q]), W = h(z);
52
+ W.current = z;
53
+ const i = h(/* @__PURE__ */ new Map()), [G, Me] = ne(() => /* @__PURE__ */ new Map()), v = h(/* @__PURE__ */ new Map()), [ce, qe] = ne(() => /* @__PURE__ */ new Map()), R = h(/* @__PURE__ */ new Set()), T = h(!0);
54
+ L(() => (T.current = !0, () => {
55
+ T.current = !1;
56
+ }), []);
57
+ const p = a(() => {
58
+ T.current && (Me((e) => {
59
+ const t = i.current;
60
+ return e.size === t.size && [...t].every(([r, n]) => e.get(r) === n) ? e : new Map(t);
61
+ }), qe((e) => {
62
+ const t = v.current;
63
+ return e.size === t.size && [...t].every(([r, n]) => e.get(r) === n) ? e : new Map(t);
64
+ }));
65
+ }, []), l = a((e) => {
66
+ var n;
67
+ const t = d.current, r = (n = t.readValue) == null ? void 0 : n.call(t, e);
68
+ return r !== void 0 ? r : t.draftRef.current[e];
69
+ }, []), A = a(
70
+ (e) => {
71
+ const t = {};
72
+ for (const r of e) t[r] = l(r);
73
+ return t;
74
+ },
75
+ [l]
76
+ ), M = a(async (e, t) => {
77
+ const r = d.current, s = (await Promise.all(
78
+ Ge(J(t)).map(async (o) => {
79
+ if (!r.valueLabel) return String(o);
80
+ try {
81
+ return await r.valueLabel(e, o) ?? null;
82
+ } catch {
83
+ return null;
84
+ }
85
+ })
86
+ )).filter((o) => !!o);
87
+ return s.length > 0 ? s.join(", ") : null;
88
+ }, []), q = h(0), Y = a(
89
+ async (e, t, r) => {
90
+ const n = await t;
91
+ q.current === r && (v.current.set(e, n), p());
92
+ },
93
+ [p]
94
+ ), I = a(
95
+ async (e, t) => {
96
+ var s, o;
97
+ const r = ((o = (s = d.current).fieldLabel) == null ? void 0 : o.call(s, e)) ?? e, n = await M(e, t);
98
+ return n ? `Your default is for ${r} “${n}”` : `Your saved default is for a different ${r}`;
99
+ },
100
+ [M]
101
+ ), E = a((e, t, r) => {
102
+ const n = d.current;
103
+ if (n.setFieldValue(e, t, r), w(t)) {
104
+ i.current.delete(e);
105
+ return;
106
+ }
107
+ i.current.set(e, t), v.current.delete(e), n.markAutoFilled([e]);
108
+ }, []), ue = a(
109
+ async (e, t) => {
110
+ var ke, ve, be, Fe, xe, Ae;
111
+ const r = d.current, n = () => q.current !== t, s = W.current.get(e) ?? [];
112
+ if (s.length === 0 || !r.layoutAllows(e)) return [];
113
+ if (R.current.has(e) || !(((ke = r.canFill) == null ? void 0 : ke.call(r, e)) ?? !0)) return [];
114
+ const o = l(e), y = !w(o);
115
+ if (y && !(((ve = r.replaceable) == null ? void 0 : ve.call(r, e, o)) ?? !1)) return [];
116
+ const K = new Set(s.flatMap((c) => Object.keys(c.context ?? {}))), we = A(K), { pick: j, fillParents: te, blockedBy: C } = se(s, we);
117
+ if (!j) {
118
+ if (C) {
119
+ const c = (be = s.find(
120
+ (g) => g.context && C in g.context && !S(g.context[C], we[C])
121
+ )) == null ? void 0 : be.context[C];
122
+ await Y(e, I(C, c), t);
123
+ }
124
+ return [];
125
+ }
126
+ const ge = V(e, r.rulesFor).reverse(), je = [
127
+ ...ge.filter((c) => c in te),
128
+ ...Object.keys(te).filter((c) => !ge.includes(c))
129
+ ], re = [], O = () => {
130
+ for (const [c, g] of re.reverse())
131
+ S(l(c), g) ? E(c, null, g) : i.current.delete(c);
132
+ };
133
+ for (const c of je) {
134
+ const g = te[c], Se = r.layoutAllows(c) && !R.current.has(c) && (((Fe = r.canFill) == null ? void 0 : Fe.call(r, c)) ?? !0) ? await r.revalidate(c, g) : !1;
135
+ if (n()) return [];
136
+ if (Se === null)
137
+ return O(), [];
138
+ if (!Se || !w(l(c)))
139
+ return O(), await Y(e, I(c, g), t), [];
140
+ E(c, g), re.push([c, g]);
141
+ }
142
+ const he = await r.revalidate(e, j.value);
143
+ if (n()) return [];
144
+ if (he === null)
145
+ return O(), [];
146
+ if (!he)
147
+ return O(), await Y(
148
+ e,
149
+ M(e, j.value).then(
150
+ (c) => c ? `Your default "${c}" is no longer available` : "Your saved default is no longer available"
151
+ ),
152
+ t
153
+ ), [];
154
+ if (!S(l(e), o) || !(((xe = r.canFill) == null ? void 0 : xe.call(r, e)) ?? !0))
155
+ return O(), [];
156
+ const me = se(s, A(K));
157
+ return ((Ae = me.pick) == null ? void 0 : Ae.id) !== j.id || Object.keys(me.fillParents).length > 0 ? (O(), "stale") : (E(e, j.value, y ? o : void 0), [...re.map(([c]) => c), e]);
158
+ },
159
+ [l, A, Y, I, M, E]
160
+ ), oe = a(
161
+ async (e, t) => {
162
+ for (let r = 0; r < 3; r++) {
163
+ const n = await ue(e, t);
164
+ if (n !== "stale") return n;
165
+ }
166
+ return [];
167
+ },
168
+ [ue]
169
+ ), U = a(
170
+ async (e) => {
171
+ var n, s;
172
+ const t = q.current, r = /* @__PURE__ */ new Set();
173
+ for (const o of e) {
174
+ if (!T.current || q.current !== t) return;
175
+ for (const y of await oe(o, t)) r.add(y);
176
+ p();
177
+ }
178
+ if (q.current === t) {
179
+ for (const o of r)
180
+ i.current.has(o) && ((s = (n = d.current).afterApply) == null || s.call(n, o, i.current.get(o)));
181
+ p();
182
+ }
183
+ },
184
+ [oe, p]
185
+ ), X = a(() => {
186
+ const e = d.current;
187
+ return He(
188
+ [...W.current.keys()].filter((t) => e.layoutAllows(t)),
189
+ e.rulesFor
190
+ );
191
+ }, []), Z = h(!1);
192
+ L(() => {
193
+ !m || P || !b || Z.current || Q === void 0 || (Z.current = !0, U(X()));
194
+ }, [m, P, b, Q, U, X]), L(() => {
195
+ let e = !1;
196
+ for (const [t, r] of i.current)
197
+ S(l(t), r) || (i.current.delete(t), e = !0);
198
+ for (const t of v.current.keys())
199
+ w(l(t)) || (v.current.delete(t), e = !0);
200
+ e && p();
201
+ });
202
+ const D = h(/* @__PURE__ */ new Set()), $ = h(/* @__PURE__ */ new Map()), [Ce, Oe] = ne(0), Ve = a(
203
+ (e) => {
204
+ let t = i.current.delete(e);
205
+ if (v.current.delete(e) && (t = !0), R.current.delete(e), $.current.delete(e), !d.current.isNew) {
206
+ t && p();
207
+ return;
208
+ }
209
+ const r = d.current.rulesFor;
210
+ for (const [n, s] of [...i.current])
211
+ V(n, r).includes(e) && ($.current.set(n, s), i.current.delete(n), t = !0);
212
+ t && p(), D.current.add(e), Oe((n) => n + 1);
213
+ },
214
+ [p]
215
+ );
216
+ L(() => {
217
+ if (D.current.size === 0 || d.current.disabled) return;
218
+ const e = [...D.current];
219
+ D.current.clear();
220
+ const t = new Map($.current);
221
+ $.current.clear();
222
+ const r = d.current, n = [];
223
+ for (const s of X()) {
224
+ if (t.has(s)) {
225
+ const y = t.get(s), K = l(s);
226
+ if (S(K, y)) r.setFieldValue(s, null, y);
227
+ else if (!w(K)) continue;
228
+ n.push(s);
229
+ continue;
230
+ }
231
+ const o = V(s, r.rulesFor);
232
+ e.some((y) => o.includes(y)) && v.current.has(s) && w(l(s)) && (v.current.delete(s), n.push(s));
233
+ }
234
+ p(), n.length > 0 && U(n);
235
+ }, [Ce]);
236
+ const _ = a(
237
+ (e) => {
238
+ const t = d.current;
239
+ if (!i.current.has(e)) return;
240
+ const r = [
241
+ e,
242
+ ...[...i.current.keys()].filter(
243
+ (n) => n !== e && V(n, t.rulesFor).includes(e)
244
+ )
245
+ ];
246
+ for (const n of r) {
247
+ const s = i.current.get(n);
248
+ S(l(n), s) && t.setFieldValue(n, null, s), i.current.delete(n);
249
+ }
250
+ R.current.add(e), p();
251
+ },
252
+ [l, p]
253
+ ), De = a(
254
+ (e) => {
255
+ if (!i.current.has(e)) return;
256
+ const t = i.current.get(e);
257
+ S(l(e), t) && d.current.setFieldValue(e, null, t), i.current.delete(e), R.current.add(e), p();
258
+ },
259
+ [l, p]
260
+ ), H = `${f}|${m ? "new" : "saved"}|${u.resetKey ?? ""}`, ae = h(H);
261
+ L(() => {
262
+ ae.current !== H && (ae.current = H, i.current.clear(), v.current.clear(), R.current.clear(), $.current.clear(), D.current.clear(), Z.current = !1, q.current += 1, p());
263
+ }, [H, p]);
264
+ const ee = a((e) => ce.get(e) ?? null, [ce]), $e = a(() => i.current, []), B = a(
265
+ (e) => {
266
+ const t = z.get(e) ?? [];
267
+ if (t.length === 0) return null;
268
+ const r = new Set(t.flatMap((n) => Object.keys(n.context ?? {})));
269
+ return se(t, A(r)).pick;
270
+ },
271
+ [z, A]
272
+ ), le = a(() => {
273
+ F.invalidateQueries({ queryKey: N(f) });
274
+ }, [F, f]), ie = a(
275
+ (e) => !w(l(e)),
276
+ [l]
277
+ ), fe = a(
278
+ async (e) => {
279
+ const t = d.current, r = [
280
+ ...[...V(e, t.rulesFor)].reverse().filter((n) => t.layoutAllows(n) && !w(l(n))),
281
+ e
282
+ ];
283
+ return Promise.all(
284
+ // A value with no label says which field it is, never its stored id ("Save 4521 · …").
285
+ r.map(
286
+ async (n) => {
287
+ var s;
288
+ return await M(n, l(n)) ?? ((s = t.fieldLabel) == null ? void 0 : s.call(t, n)) ?? Je(n);
289
+ }
290
+ )
291
+ );
292
+ },
293
+ [l, M]
294
+ ), de = a(
295
+ async (e) => {
296
+ const t = d.current, r = l(e);
297
+ if (w(r)) throw new Error("Pick a value first");
298
+ const n = V(e, t.rulesFor), s = A([e, ...n]), o = (y) => k.request(
299
+ Re("/field-defaults/mine", {
300
+ collection: f,
301
+ field: y,
302
+ value: J(s[y]),
303
+ context: Be(y, s, t.rulesFor)
304
+ })
305
+ );
306
+ try {
307
+ await o(e);
308
+ for (const y of [...n].reverse())
309
+ t.layoutAllows(y) && !w(s[y]) && await o(y);
310
+ } finally {
311
+ await F.invalidateQueries({ queryKey: N(f) });
312
+ }
313
+ },
314
+ [k, F, f, l, A]
315
+ ), ye = a(
316
+ async (e) => {
317
+ const t = B(e);
318
+ return t ? (await k.request(Ee(`/field-defaults/mine/${t.id}`)), await F.invalidateQueries({ queryKey: N(f) }), t) : null;
319
+ },
320
+ [k, F, f, B]
321
+ ), pe = a(
322
+ async (e) => {
323
+ await k.request(
324
+ Re("/field-defaults/mine", {
325
+ collection: f,
326
+ field: e.field,
327
+ value: e.value,
328
+ context: e.context ?? {}
329
+ })
330
+ ), await F.invalidateQueries({ queryKey: N(f) });
331
+ },
332
+ [k, F, f]
333
+ ), Ke = Pe(
334
+ () => ({
335
+ allowed: (e) => d.current.layoutAllows(e),
336
+ defaultFor: B,
337
+ save: de,
338
+ clear: ye,
339
+ restore: pe,
340
+ refresh: le,
341
+ hasValue: ie,
342
+ describeSave: fe,
343
+ applied: (e) => G.has(e),
344
+ hintFor: ee,
345
+ undo: _
346
+ }),
347
+ [B, de, ye, pe, le, ie, fe, G, ee, _]
348
+ );
349
+ return { ledger: G, hintFor: ee, undo: _, onPersonChange: Ve, release: De, appliedNow: $e, context: Ke };
350
+ }
351
+ export {
352
+ We as PersonalDefaultsContext,
353
+ nt as linksBeyond,
354
+ N as personalDefaultsKey,
355
+ S as sameDefaultValue,
356
+ st as usePersonalDefaults,
357
+ rt as usePersonalDefaultsApi
358
+ };