@nivaro/react 0.3.96 → 0.3.98

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.
@@ -1,52 +1,53 @@
1
1
  import { jsx as l, jsxs as f } from "react/jsx-runtime";
2
- import { useQueryClient as V, useQuery as U } from "@tanstack/react-query";
3
- import { Wrench as X, ShieldAlert as Y, Loader2 as F } from "lucide-react";
4
- import { useState as w, useMemo as K } from "react";
2
+ import { useQueryClient as Y, useQuery as U } from "@tanstack/react-query";
3
+ import { Wrench as Z, ShieldAlert as I, Loader2 as F } from "lucide-react";
4
+ import { useState as w, useMemo as P } from "react";
5
5
  import { toast as Q } from "sonner";
6
- import { useNivaroClient as Z } from "../../context.js";
7
- import { get as C, patch as I, del as ee, post as te } from "../../lib/commands.js";
6
+ import { useNivaroClient as ee } from "../../context.js";
7
+ import { get as $, patch as te, del as ne, post as ae } from "../../lib/commands.js";
8
8
  import { titleCase as T } from "../../lib/utils.js";
9
9
  import { Button as W } from "../ui/button.js";
10
- import { Sheet as ne, SheetContent as ae, SheetHeader as re, SheetTitle as se } from "../ui/sheet.js";
11
- import { ChangeReasonDialog as ie, changeReasonChallenge as le } from "./ChangeReasonDialog.js";
12
- import { FieldRenderer as oe } from "./FieldRenderer.js";
13
- import { M2MCombobox as ce, m2aWriteMeta as de } from "./M2MCombobox.js";
14
- import { M2MStagingContext as me } from "./M2MStagingContext.js";
15
- import { invalidateM2MFor as fe } from "./m2m-invalidate.js";
16
- function A(a, i) {
10
+ import { Sheet as re, SheetContent as ie, SheetHeader as se, SheetTitle as le } from "../ui/sheet.js";
11
+ import { ChangeReasonDialog as oe, changeReasonChallenge as ce } from "./ChangeReasonDialog.js";
12
+ import { FieldRenderer as de } from "./FieldRenderer.js";
13
+ import { M2MCombobox as me, m2aAllowedCount as fe, m2aWriteMeta as ue } from "./M2MCombobox.js";
14
+ import { M2MStagingContext as D } from "./M2MStagingContext.js";
15
+ import { invalidateM2MFor as pe } from "./m2m-invalidate.js";
16
+ import { RawM2ALinks as he } from "./RawM2ALinks.js";
17
+ function E(i, s) {
17
18
  const u = (p) => p == null || p === "";
18
- return u(a) && u(i) ? !0 : typeof a == "number" || typeof i == "number" ? Number(a) === Number(i) : typeof a == "boolean" || typeof i == "boolean" ? String(a) === String(i) : typeof a == "object" || typeof i == "object" ? JSON.stringify(a) === JSON.stringify(i) : String(a) === String(i);
19
+ return u(i) && u(s) ? !0 : typeof i == "number" || typeof s == "number" ? Number(i) === Number(s) : typeof i == "boolean" || typeof s == "boolean" ? String(i) === String(s) : typeof i == "object" || typeof s == "object" ? JSON.stringify(i) === JSON.stringify(s) : String(i) === String(s);
19
20
  }
20
- function E(a) {
21
- if (a == null || a === "") return "∅";
22
- const i = typeof a == "object" ? JSON.stringify(a) : String(a);
23
- return i.length > 60 ? `${i.slice(0, 57)}…` : i;
21
+ function J(i) {
22
+ if (i == null || i === "") return "∅";
23
+ const s = typeof i == "object" ? JSON.stringify(i) : String(i);
24
+ return s.length > 60 ? `${s.slice(0, 57)}…` : s;
24
25
  }
25
- function qe({
26
- collection: a,
27
- itemId: i,
26
+ function Oe({
27
+ collection: i,
28
+ itemId: s,
28
29
  open: u,
29
30
  onClose: p,
30
31
  onSaved: S
31
32
  }) {
32
- const h = Z(), M = V(), [m, _] = w({}), [g, v] = w(/* @__PURE__ */ new Map()), [x, N] = w(/* @__PURE__ */ new Map()), [k, L] = w(!1), [J, j] = w(null), { data: c } = U({
33
- queryKey: ["raw-edit-config", a],
33
+ const h = ee(), M = Y(), [m, j] = w({}), [g, v] = w(/* @__PURE__ */ new Map()), [y, _] = w(/* @__PURE__ */ new Map()), [k, L] = w(!1), [B, q] = w(null), { data: c } = U({
34
+ queryKey: ["raw-edit-config", i],
34
35
  queryFn: async () => {
35
- const [e, r] = await Promise.all([
36
- h.request(C(`/field-config/${a}`)).then((s) => s.data ?? []),
37
- h.request(C(`/collections/${a}`)).then((s) => s.data)
36
+ const [e, n] = await Promise.all([
37
+ h.request($(`/field-config/${i}`)).then((r) => r.data ?? []),
38
+ h.request($(`/collections/${i}`)).then((r) => r.data)
38
39
  ]);
39
- return { fields: e, relations: r.relations ?? [] };
40
+ return { fields: e, relations: n.relations ?? [] };
40
41
  },
41
42
  enabled: u,
42
43
  staleTime: 6e4
43
- }), { data: o, isLoading: P } = U({
44
- queryKey: ["raw-edit-record", a, i],
45
- queryFn: () => h.request(C(`/items/${a}/${i}`)).then((e) => e.data),
44
+ }), { data: o, isLoading: H } = U({
45
+ queryKey: ["raw-edit-record", i, s],
46
+ queryFn: () => h.request($(`/items/${i}/${s}`)).then((e) => e.data),
46
47
  enabled: u,
47
48
  staleTime: 0
48
- }), D = K(() => {
49
- const e = (c == null ? void 0 : c.fields) ?? [], r = (c == null ? void 0 : c.relations) ?? [], s = (t) => r.some((n) => n.one_collection === a && n.one_field === t.field);
49
+ }), G = P(() => {
50
+ const e = (c == null ? void 0 : c.fields) ?? [], n = (c == null ? void 0 : c.relations) ?? [], r = (t) => n.some((a) => a.one_collection === i && a.one_field === t.field);
50
51
  return e.filter((t) => !t.field.startsWith("__")).map((t) => ({
51
52
  // Strip every conditional/curation config — raw mode ignores them all
52
53
  field: {
@@ -57,147 +58,165 @@ function qe({
57
58
  lock_condition: null,
58
59
  dependency_config: null
59
60
  },
60
- alias: s(t),
61
- m2mRel: r.find(
62
- (n) => n.one_collection === a && n.one_field === t.field && n.junction_field != null
61
+ alias: r(t),
62
+ m2mRel: n.find(
63
+ (a) => a.one_collection === i && a.one_field === t.field && a.junction_field != null
63
64
  )
64
- })).sort((t, n) => (t.field.sort ?? 0) - (n.field.sort ?? 0));
65
- }, [c, a]), q = (c == null ? void 0 : c.relations) ?? [], O = (e) => q.find(
66
- (r) => r.one_collection === a && r.one_field === e && r.junction_field != null
67
- ), B = K(
65
+ })).sort((t, a) => (t.field.sort ?? 0) - (a.field.sort ?? 0));
66
+ }, [c, i]), N = (c == null ? void 0 : c.relations) ?? [], O = (e) => N.find(
67
+ (n) => n.one_collection === i && n.one_field === e && n.junction_field != null
68
+ ), z = (e) => {
69
+ const n = N.find(
70
+ (r) => r.many_collection === e.many_collection && r.many_field === e.junction_field && r.id !== e.id
71
+ );
72
+ return !n || n.one_collection || fe(n) < 2 ? null : n.one_collection_field || "collection";
73
+ }, A = P(
68
74
  () => ({
69
75
  getStagedLinks: (e) => g.get(e) ?? [],
70
- getStagedUnlinks: (e) => x.get(e) ?? /* @__PURE__ */ new Set(),
71
- stageLink: (e, r) => v((s) => {
72
- const t = new Map(s);
73
- return t.set(e, [...t.get(e) ?? [], r]), t;
76
+ getStagedUnlinks: (e) => y.get(e) ?? /* @__PURE__ */ new Set(),
77
+ stageLink: (e, n) => v((r) => {
78
+ const t = new Map(r);
79
+ return t.set(e, [...t.get(e) ?? [], n]), t;
74
80
  }),
75
- unstageLink: (e, r) => v((s) => {
76
- const t = new Map(s);
81
+ unstageLink: (e, n) => v((r) => {
82
+ const t = new Map(r);
77
83
  return t.set(
78
84
  e,
79
- (t.get(e) ?? []).filter((n) => String(n) !== String(r))
85
+ (t.get(e) ?? []).filter((a) => String(a) !== String(n))
80
86
  ), t;
81
87
  }),
82
- stageUnlink: (e, r) => N((s) => {
83
- const t = new Map(s), n = new Set(t.get(e) ?? []);
84
- return n.add(r), t.set(e, n), t;
88
+ stageUnlink: (e, n) => _((r) => {
89
+ const t = new Map(r), a = new Set(t.get(e) ?? []);
90
+ return a.add(n), t.set(e, a), t;
85
91
  }),
86
- unstageUnlink: (e, r) => N((s) => {
87
- const t = new Map(s), n = new Set(t.get(e) ?? []);
88
- return n.delete(r), t.set(e, n), t;
92
+ unstageUnlink: (e, n) => _((r) => {
93
+ const t = new Map(r), a = new Set(t.get(e) ?? []);
94
+ return a.delete(n), t.set(e, a), t;
89
95
  })
90
96
  }),
91
- [g, x]
92
- ), H = [...g.values()].some((e) => e.length > 0) || [...x.values()].some((e) => e.size > 0), R = Object.keys(m).length > 0 || H, z = async (e) => {
93
- var r;
97
+ [g, y]
98
+ ), V = [...g.values()].some((e) => e.length > 0) || [...y.values()].some((e) => e.size > 0), K = Object.keys(m).length > 0 || V, R = async (e) => {
99
+ var n;
94
100
  L(!0);
95
101
  try {
96
102
  if (Object.keys(m).length > 0) {
97
103
  const t = { ...m };
98
- e && (t._change_reason = e), await h.request(I(`/items/${a}/${i}`, t));
104
+ e && (t._change_reason = e), await h.request(te(`/items/${i}/${s}`, t));
99
105
  }
100
- const s = [];
101
- for (const [t, n] of x.entries()) {
102
- if (!n.size) continue;
106
+ const r = [];
107
+ for (const [t, a] of y.entries()) {
108
+ if (!a.size) continue;
103
109
  const d = O(t);
104
110
  if (d)
105
- for (const y of n)
106
- s.push(h.request(ee(`/items/${d.many_collection}/${y}`)).catch(() => {
111
+ for (const x of a)
112
+ r.push(h.request(ne(`/items/${d.many_collection}/${x}`)).catch(() => {
107
113
  }));
108
114
  }
109
- for (const [t, n] of g.entries()) {
110
- if (!n.length) continue;
115
+ for (const [t, a] of g.entries()) {
116
+ if (!a.length) continue;
111
117
  const d = O(t);
112
118
  if (!d) continue;
113
- const y = q.find(
119
+ const x = N.find(
114
120
  (b) => b.many_collection === d.many_collection && b.many_field === d.junction_field && b.id !== d.id
115
- ), $ = de(y), G = $ ? { [$.field]: $.value } : {};
116
- for (const b of n)
117
- s.push(
121
+ ), C = ue(x), X = C ? { [C.field]: C.value } : {};
122
+ for (const b of a)
123
+ r.push(
118
124
  h.request(
119
- te(`/items/${d.many_collection}`, {
120
- [d.many_field ?? ""]: i,
125
+ ae(`/items/${d.many_collection}`, {
126
+ [d.many_field ?? ""]: s,
121
127
  [d.junction_field ?? ""]: b,
122
- ...G
128
+ ...X
123
129
  })
124
130
  ).catch(() => {
125
131
  })
126
132
  );
127
133
  }
128
- await Promise.all(s), Q.success("Raw changes saved"), _({}), v(/* @__PURE__ */ new Map()), N(/* @__PURE__ */ new Map()), M.invalidateQueries({ queryKey: ["item", a, String(i)] }), M.invalidateQueries({ queryKey: ["raw-edit-record", a, i] }), fe(M, [i]), S == null || S(), p();
129
- } catch (s) {
130
- const t = le(s);
134
+ await Promise.all(r), Q.success("Raw changes saved"), j({}), v(/* @__PURE__ */ new Map()), _(/* @__PURE__ */ new Map()), M.invalidateQueries({ queryKey: ["item", i, String(s)] }), M.invalidateQueries({ queryKey: ["raw-edit-record", i, s] }), pe(M, [s]), S == null || S(), p();
135
+ } catch (r) {
136
+ const t = ce(r);
131
137
  if (t) {
132
- j(t);
138
+ q(t);
133
139
  return;
134
140
  }
135
- const n = s == null ? void 0 : s.response;
136
- Q.error(((r = n == null ? void 0 : n.data) == null ? void 0 : r.error) ?? (n == null ? void 0 : n.error) ?? "Save failed");
141
+ const a = r == null ? void 0 : r.response;
142
+ Q.error(((n = a == null ? void 0 : a.data) == null ? void 0 : n.error) ?? (a == null ? void 0 : a.error) ?? "Save failed");
137
143
  } finally {
138
144
  L(!1);
139
145
  }
140
146
  };
141
147
  return /* @__PURE__ */ l(
142
- ne,
148
+ re,
143
149
  {
144
150
  open: u,
145
151
  onOpenChange: (e) => {
146
- e || k || (_({}), v(/* @__PURE__ */ new Map()), N(/* @__PURE__ */ new Map()), p());
152
+ e || k || (j({}), v(/* @__PURE__ */ new Map()), _(/* @__PURE__ */ new Map()), p());
147
153
  },
148
154
  children: /* @__PURE__ */ f(
149
- ae,
155
+ ie,
150
156
  {
151
157
  side: "right",
152
158
  className: "flex w-[1040px] max-w-[94vw] flex-col p-0 sm:max-w-[94vw]",
153
159
  children: [
154
- /* @__PURE__ */ l(re, { className: "shrink-0 border-b border-slate-200 px-5 py-3 dark:border-border", children: /* @__PURE__ */ f(se, { className: "flex items-center gap-2 text-[14px]", children: [
155
- /* @__PURE__ */ l(X, { className: "h-4 w-4 text-nvr-cyan" }),
160
+ /* @__PURE__ */ l(se, { className: "shrink-0 border-b border-slate-200 px-5 py-3 dark:border-border", children: /* @__PURE__ */ f(le, { className: "flex items-center gap-2 text-[14px]", children: [
161
+ /* @__PURE__ */ l(Z, { className: "h-4 w-4 text-nvr-cyan" }),
156
162
  "Raw edit — ",
157
- T(a),
163
+ T(i),
158
164
  " #",
159
- i
165
+ s
160
166
  ] }) }),
161
167
  /* @__PURE__ */ f("p", { className: "flex shrink-0 items-center gap-2 border-b border-amber-200 bg-amber-50 px-5 py-2 text-[12px] text-amber-800 dark:border-amber-500/20 dark:bg-amber-500/10 dark:text-amber-300", children: [
162
- /* @__PURE__ */ l(Y, { className: "h-3.5 w-3.5 shrink-0" }),
168
+ /* @__PURE__ */ l(I, { className: "h-3.5 w-3.5 shrink-0" }),
163
169
  "Admin raw mode — visibility rules, locks, cascades and layout restrictions are bypassed. Server-side validation still applies. Every change is recorded in the revision history."
164
170
  ] }),
165
- /* @__PURE__ */ l("div", { className: "min-h-0 flex-1 overflow-y-auto px-5 py-4", children: P || !c ? /* @__PURE__ */ l("div", { className: "flex justify-center py-10", children: /* @__PURE__ */ l(F, { className: "h-5 w-5 animate-spin text-slate-400" }) }) : /* @__PURE__ */ l("div", { className: "space-y-3", children: D.map(({ field: e, alias: r, m2mRel: s }) => {
166
- const t = e.field in m ? m[e.field] : o == null ? void 0 : o[e.field], n = e.field in m && !A(m[e.field], o == null ? void 0 : o[e.field]);
171
+ /* @__PURE__ */ l("div", { className: "min-h-0 flex-1 overflow-y-auto px-5 py-4", children: H || !c ? /* @__PURE__ */ l("div", { className: "flex justify-center py-10", children: /* @__PURE__ */ l(F, { className: "h-5 w-5 animate-spin text-slate-400" }) }) : /* @__PURE__ */ l("div", { className: "space-y-3", children: G.map(({ field: e, alias: n, m2mRel: r }) => {
172
+ const t = e.field in m ? m[e.field] : o == null ? void 0 : o[e.field], a = e.field in m && !E(m[e.field], o == null ? void 0 : o[e.field]);
167
173
  return /* @__PURE__ */ f(
168
174
  "div",
169
175
  {
170
- className: `grid grid-cols-[220px_1fr] items-start gap-3 rounded-md ${n ? "-mx-2 bg-amber-50 px-2 py-1 ring-1 ring-amber-200 dark:bg-amber-400/10 dark:ring-amber-500/30" : ""}`,
171
- "data-raw-changed": n ? e.field : void 0,
176
+ className: `grid grid-cols-[220px_1fr] items-start gap-3 rounded-md ${a ? "-mx-2 bg-amber-50 px-2 py-1 ring-1 ring-amber-200 dark:bg-amber-400/10 dark:ring-amber-500/30" : ""}`,
177
+ "data-raw-changed": a ? e.field : void 0,
172
178
  "data-raw-field": e.field,
173
179
  children: [
174
180
  /* @__PURE__ */ f("div", { className: "pt-1.5", children: [
175
181
  /* @__PURE__ */ f("p", { className: "text-[12px] font-medium text-slate-700 dark:text-slate-200", children: [
176
182
  e.label || T(e.field),
177
- n && /* @__PURE__ */ l("span", { className: "ml-1.5 rounded bg-amber-200/70 px-1 text-[9px] font-semibold uppercase tracking-wide text-amber-900 dark:bg-amber-500/20 dark:text-amber-200", children: "changed" })
183
+ a && /* @__PURE__ */ l("span", { className: "ml-1.5 rounded bg-amber-200/70 px-1 text-[9px] font-semibold uppercase tracking-wide text-amber-900 dark:bg-amber-500/20 dark:text-amber-200", children: "changed" })
178
184
  ] }),
179
185
  /* @__PURE__ */ l("p", { className: "font-mono text-[10.5px] text-slate-400", children: e.field }),
180
- n && /* @__PURE__ */ f(
186
+ a && /* @__PURE__ */ f(
181
187
  "p",
182
188
  {
183
189
  className: "mt-0.5 truncate text-[10.5px] text-slate-400",
184
- "data-tip": `Was: ${E(o == null ? void 0 : o[e.field])}`,
190
+ "data-tip": `Was: ${J(o == null ? void 0 : o[e.field])}`,
185
191
  children: [
186
192
  "was ",
187
- E(o == null ? void 0 : o[e.field])
193
+ J(o == null ? void 0 : o[e.field])
188
194
  ]
189
195
  }
190
196
  )
191
197
  ] }),
192
- e.field === "id" ? /* @__PURE__ */ l("p", { className: "pt-1.5 font-mono text-[12px] text-slate-500", children: String(t ?? "") }) : r && s ? /* @__PURE__ */ l(me.Provider, { value: B, children: /* @__PURE__ */ l(ce, { relation: s, parentId: i, allRelations: q }) }) : r ? /* @__PURE__ */ l("p", { className: "pt-1.5 text-[11.5px] italic text-slate-400", children: "Child rows (one-to-many) — edit through the normal form" }) : /* @__PURE__ */ l(
193
- oe,
198
+ e.field === "id" ? /* @__PURE__ */ l("p", { className: "pt-1.5 font-mono text-[12px] text-slate-500", children: String(t ?? "") }) : n && r && z(r) ? (
199
+ // A ship-to contact is a person OR an external email: the
200
+ // plain picker read every link as a person, so an external
201
+ // email showed "Record not found". List each link as it is.
202
+ /* @__PURE__ */ l(D.Provider, { value: A, children: /* @__PURE__ */ l(
203
+ he,
204
+ {
205
+ collection: i,
206
+ itemId: s,
207
+ field: e.field,
208
+ discriminator: z(r)
209
+ }
210
+ ) })
211
+ ) : n && r ? /* @__PURE__ */ l(D.Provider, { value: A, children: /* @__PURE__ */ l(me, { relation: r, parentId: s, allRelations: N }) }) : n ? /* @__PURE__ */ l("p", { className: "pt-1.5 text-[11.5px] italic text-slate-400", children: "Child rows (one-to-many) — edit through the normal form" }) : /* @__PURE__ */ l(
212
+ de,
194
213
  {
195
214
  field: e,
196
215
  value: t,
197
- onChange: (d) => _((y) => ({ ...y, [e.field]: d })),
216
+ onChange: (d) => j((x) => ({ ...x, [e.field]: d })),
198
217
  relations: c.relations,
199
- collection: a,
200
- itemId: i
218
+ collection: i,
219
+ itemId: s
201
220
  }
202
221
  )
203
222
  ]
@@ -207,22 +226,22 @@ function qe({
207
226
  }) }) }),
208
227
  /* @__PURE__ */ f("div", { className: "flex shrink-0 items-center justify-between gap-3 border-t border-slate-200 px-5 py-3 dark:border-border", children: [
209
228
  /* @__PURE__ */ l("span", { className: "text-[11.5px] text-slate-400", children: (() => {
210
- if (!R) return "No changes yet";
211
- const e = Object.keys(m).filter((r) => !A(m[r], o == null ? void 0 : o[r])).length + [...g.values()].filter((r) => r.length > 0).length + [...x.values()].filter((r) => r.size > 0).length;
229
+ if (!K) return "No changes yet";
230
+ const e = Object.keys(m).filter((n) => !E(m[n], o == null ? void 0 : o[n])).length + [...g.values()].filter((n) => n.length > 0).length + [...y.values()].filter((n) => n.size > 0).length;
212
231
  return e === 0 ? "No changes yet" : `${e} field${e !== 1 ? "s" : ""} changed`;
213
232
  })() }),
214
233
  /* @__PURE__ */ f("div", { className: "flex gap-2", children: [
215
234
  /* @__PURE__ */ l(W, { variant: "outline", size: "sm", onClick: p, disabled: k, children: "Cancel" }),
216
- /* @__PURE__ */ l(W, { size: "sm", onClick: () => void z(), disabled: !R || k, children: k ? /* @__PURE__ */ l(F, { className: "h-3.5 w-3.5 animate-spin" }) : "Save raw changes" })
235
+ /* @__PURE__ */ l(W, { size: "sm", onClick: () => void R(), disabled: !K || k, children: k ? /* @__PURE__ */ l(F, { className: "h-3.5 w-3.5 animate-spin" }) : "Save raw changes" })
217
236
  ] })
218
237
  ] }),
219
238
  /* @__PURE__ */ l(
220
- ie,
239
+ oe,
221
240
  {
222
- challenge: J,
223
- onCancel: () => j(null),
241
+ challenge: B,
242
+ onCancel: () => q(null),
224
243
  onSubmit: (e) => {
225
- j(null), z(e);
244
+ q(null), R(e);
226
245
  }
227
246
  }
228
247
  )
@@ -233,5 +252,5 @@ function qe({
233
252
  );
234
253
  }
235
254
  export {
236
- qe as RawEditSheet
255
+ Oe as RawEditSheet
237
256
  };
@@ -0,0 +1,101 @@
1
+ import { jsxs as d, jsx as t } from "react/jsx-runtime";
2
+ import { useQuery as k } from "@tanstack/react-query";
3
+ import { Loader2 as f, RotateCcw as b, X as N } from "lucide-react";
4
+ import { useNivaroClient as w } from "../../context.js";
5
+ import { get as v } from "../../lib/commands.js";
6
+ import { cn as y, titleCase as S } from "../../lib/utils.js";
7
+ import { useM2MStaging as C } from "./M2MStagingContext.js";
8
+ function L(e) {
9
+ const r = String(e ?? "").toLowerCase();
10
+ return r === "directus_users" || r === "nivaro_users" ? "Person" : r === "additional_emails" ? "External email" : r ? S(r.replace(/_/g, " ")) : "Record";
11
+ }
12
+ function _(e) {
13
+ if (!e) return { name: null, email: null };
14
+ const r = String(e.first_name ?? "").trim(), n = String(e.last_name ?? "").trim(), m = [r, n].filter(Boolean).join(" ") || typeof e.name == "string" && e.name.trim() || typeof e.label == "string" && e.label.trim() || null, c = typeof e.email == "string" && e.email.trim() ? e.email.trim() : null;
15
+ return { name: m, email: c };
16
+ }
17
+ function M({
18
+ collection: e,
19
+ itemId: r,
20
+ field: n,
21
+ discriminator: m
22
+ }) {
23
+ const c = w(), s = C(), p = k({
24
+ queryKey: ["raw-edit-m2a", e, String(r), n],
25
+ queryFn: () => c.request(
26
+ v(`/items/${e}/${encodeURIComponent(String(r))}`, {
27
+ fields: `id,${n}.*`
28
+ })
29
+ ).then((a) => {
30
+ var i;
31
+ const l = (i = a.data) == null ? void 0 : i[n];
32
+ return Array.isArray(l) ? l : [];
33
+ }),
34
+ staleTime: 15e3
35
+ }), h = (s == null ? void 0 : s.getStagedUnlinks(n)) ?? /* @__PURE__ */ new Set();
36
+ if (p.isLoading)
37
+ return /* @__PURE__ */ d("p", { className: "flex items-center gap-1.5 pt-1.5 text-[11.5px] text-slate-400", children: [
38
+ /* @__PURE__ */ t(f, { className: "h-3 w-3 animate-spin", "aria-hidden": !0 }),
39
+ " Loading links…"
40
+ ] });
41
+ if (p.error)
42
+ return /* @__PURE__ */ t("p", { className: "pt-1.5 text-[11.5px] text-red-600 dark:text-red-400", children: "Couldn’t load the links." });
43
+ const u = p.data ?? [];
44
+ return /* @__PURE__ */ d("div", { className: "space-y-1.5", "data-raw-m2a": n, children: [
45
+ u.length === 0 ? /* @__PURE__ */ t("p", { className: "pt-1.5 text-[11.5px] italic text-slate-400", children: "No links" }) : /* @__PURE__ */ t("ul", { className: "flex flex-wrap gap-1.5", children: u.map((a) => {
46
+ const l = [...h].some((g) => String(g) === String(a.id)), { name: i, email: o } = _(a.item), x = L(a[m]);
47
+ return /* @__PURE__ */ d(
48
+ "li",
49
+ {
50
+ "data-raw-m2a-link": String(a.id),
51
+ className: y(
52
+ "inline-flex max-w-full items-center gap-2 rounded-md border px-2 py-1 text-[12px]",
53
+ l ? "border-dashed border-red-300 bg-red-50/60 text-slate-400 line-through dark:border-red-500/40 dark:bg-red-500/10" : "border-slate-200 bg-white text-slate-800 dark:border-border dark:bg-card dark:text-foreground"
54
+ ),
55
+ children: [
56
+ /* @__PURE__ */ t("span", { className: "shrink-0 rounded bg-slate-100 px-1.5 text-[10px] font-medium uppercase tracking-wide text-slate-500 no-underline dark:bg-muted dark:text-muted-foreground", children: x }),
57
+ a.item ? /* @__PURE__ */ d("span", { className: "min-w-0 truncate", children: [
58
+ i && /* @__PURE__ */ t("span", { className: "font-medium", children: i }),
59
+ i && o && /* @__PURE__ */ t("span", { className: "text-slate-400", children: " · " }),
60
+ o && /* @__PURE__ */ t("span", { className: "font-mono text-[11.5px]", children: o }),
61
+ !i && !o && /* @__PURE__ */ t("span", { className: "italic text-slate-400", children: "No name or email" })
62
+ ] }) : /* @__PURE__ */ d("span", { className: "italic text-slate-400", children: [
63
+ x,
64
+ " #",
65
+ String(a.item_id ?? ""),
66
+ " is gone or hidden from you"
67
+ ] }),
68
+ s && (l ? /* @__PURE__ */ t(
69
+ "button",
70
+ {
71
+ type: "button",
72
+ "aria-label": "Keep this link",
73
+ "data-tip": "Keep this link",
74
+ onClick: () => s.unstageUnlink(n, a.id),
75
+ className: "rounded p-0.5 text-slate-400 hover:bg-slate-100 hover:text-slate-700 dark:hover:bg-muted",
76
+ children: /* @__PURE__ */ t(b, { className: "h-3 w-3" })
77
+ }
78
+ ) : /* @__PURE__ */ t(
79
+ "button",
80
+ {
81
+ type: "button",
82
+ "aria-label": `Remove ${i ?? o ?? "this link"}`,
83
+ "data-tip": "Remove this link (saved with Save)",
84
+ onClick: () => s.stageUnlink(n, a.id),
85
+ className: "rounded p-0.5 text-slate-400 hover:bg-red-50 hover:text-red-600 dark:hover:bg-red-500/10",
86
+ children: /* @__PURE__ */ t(N, { className: "h-3 w-3" })
87
+ }
88
+ ))
89
+ ]
90
+ },
91
+ String(a.id)
92
+ );
93
+ }) }),
94
+ /* @__PURE__ */ t("p", { className: "text-[10.5px] text-slate-400", children: "Links to a person or an external email. Add one through the normal form." })
95
+ ] });
96
+ }
97
+ export {
98
+ M as RawM2ALinks,
99
+ L as m2aKindLabel,
100
+ _ as m2aLinkText
101
+ };
@@ -1,86 +1,101 @@
1
- import { m2aWriteMeta as x } from "./M2MCombobox.js";
2
- function F(n, l, s) {
3
- var r;
4
- let e = l.find((o) => o.one_collection === n && o.one_field === s);
5
- if (!e && s.includes(".")) {
6
- const o = s.slice(0, s.indexOf("."));
7
- e = l.find(
8
- (i) => i.one_collection === n && i.many_collection === o && !!i.one_field
1
+ import { m2aWriteMeta as x, parseM2aToken as E } from "./M2MCombobox.js";
2
+ function L(n, r, a) {
3
+ var _;
4
+ let e = r.find((t) => t.one_collection === n && t.one_field === a);
5
+ if (!e && a.includes(".")) {
6
+ const t = a.slice(0, a.indexOf("."));
7
+ e = r.find(
8
+ (i) => i.one_collection === n && i.many_collection === t && !!i.one_field
9
9
  );
10
10
  }
11
11
  if (!(e != null && e.one_field) || !e.many_collection) return null;
12
- const c = e.junction_field ?? ((r = l.find((o) => o.many_collection === e.many_collection && o.id !== e.id)) == null ? void 0 : r.many_field) ?? null;
13
- if (!c) return null;
14
- const d = l.find(
15
- (o) => o.many_collection === e.many_collection && o.many_field === c && o.id !== e.id
16
- ), _ = x(d);
17
- return { field: e.one_field, junctionField: c, m2aCollection: (_ == null ? void 0 : _.value) ?? null };
12
+ const s = e.junction_field ?? ((_ = r.find((t) => t.many_collection === e.many_collection && t.id !== e.id)) == null ? void 0 : _.many_field) ?? null;
13
+ if (!s) return null;
14
+ const u = r.find(
15
+ (t) => t.many_collection === e.many_collection && t.many_field === s && t.id !== e.id
16
+ ), d = x(u);
17
+ return {
18
+ field: e.one_field,
19
+ junctionField: s,
20
+ m2aCollection: (d == null ? void 0 : d.value) ?? null,
21
+ m2aField: (d == null ? void 0 : d.field) ?? null
22
+ };
18
23
  }
19
- function L(n) {
24
+ function O(n) {
20
25
  return n == null || n === "" ? [] : Array.isArray(n) ? n : typeof n == "object" ? [] : [n];
21
26
  }
22
- const C = (n) => n && typeof n == "object" ? `${String(n.collection ?? "").toLowerCase()}:${String(n.id ?? "").toLowerCase()}` : `:${String(n).toLowerCase()}`;
23
- function A(n) {
27
+ const F = (n) => n && typeof n == "object" ? `${String(n.collection ?? "").toLowerCase()}:${String(n.id ?? "").toLowerCase()}` : `:${String(n).toLowerCase()}`;
28
+ function M(n) {
24
29
  var i;
25
- const { collection: l, relations: s, links: e, unlinks: c, payload: d, isNew: _ } = n, r = /* @__PURE__ */ new Set(), o = /* @__PURE__ */ new Set([...e.keys(), ...c.keys()]);
26
- for (const f of o) {
27
- const g = e.get(f) ?? [], y = c.get(f) ?? /* @__PURE__ */ new Set();
30
+ const { collection: r, relations: a, links: e, unlinks: s, payload: u, isNew: d } = n, _ = /* @__PURE__ */ new Set(), t = /* @__PURE__ */ new Set([...e.keys(), ...s.keys()]);
31
+ for (const m of t) {
32
+ const g = e.get(m) ?? [], y = s.get(m) ?? /* @__PURE__ */ new Set();
28
33
  if (g.length === 0 && y.size === 0) continue;
29
- const u = F(l, s, f);
30
- if (!u) continue;
31
- const p = (t) => u.m2aCollection ? { collection: u.m2aCollection, id: t } : t;
32
- if (_) {
34
+ const f = L(r, a, m);
35
+ if (!f) continue;
36
+ const k = (o) => {
37
+ if (!f.m2aCollection) return o;
38
+ const l = E(o);
39
+ return l ? { collection: l.collection, id: l.id } : { collection: f.m2aCollection, id: o };
40
+ }, C = (o) => {
41
+ const l = o[f.junctionField];
42
+ if (l == null) return null;
43
+ if (!f.m2aCollection) return l;
44
+ const c = f.m2aField ? o[f.m2aField] : null;
45
+ return typeof c == "string" && c ? { collection: c, id: l } : null;
46
+ };
47
+ if (d) {
33
48
  if (y.size > 0) continue;
34
- const t = [], m = /* @__PURE__ */ new Set();
35
- for (const a of [...L(d[u.field]), ...g.map(p)]) {
36
- const S = C(a);
37
- m.has(S) || (m.add(S), t.push(a));
49
+ const o = [], l = /* @__PURE__ */ new Set();
50
+ for (const c of [...O(u[f.field]), ...g.map(k)]) {
51
+ const w = F(c);
52
+ l.has(w) || (l.add(w), o.push(c));
38
53
  }
39
- d[u.field] = t, r.add(f);
54
+ u[f.field] = o, _.add(m);
40
55
  continue;
41
56
  }
42
- const w = (i = n.committed) == null ? void 0 : i.call(n, f);
43
- let h = [];
57
+ const S = (i = n.committed) == null ? void 0 : i.call(n, m);
58
+ let p = [];
44
59
  if (y.size > 0) {
45
- if (!w) continue;
46
- const t = new Map(w.map((a) => [String(a.id), a])), m = [...y].map((a) => {
47
- var S;
48
- return (S = t.get(String(a))) == null ? void 0 : S[u.junctionField];
60
+ if (!S) continue;
61
+ const o = new Map(S.map((c) => [String(c.id), c])), l = [...y].map((c) => {
62
+ const w = o.get(String(c));
63
+ return w ? C(w) : null;
49
64
  });
50
- if (m.some((a) => a == null)) continue;
51
- h = m.map(p);
65
+ if (l.some((c) => c == null)) continue;
66
+ p = l;
52
67
  }
53
- const b = (w ?? []).filter((t) => !y.has(t.id) && !y.has(String(t.id))).map((t) => t[u.junctionField]).filter((t) => t != null), k = [], j = /* @__PURE__ */ new Set();
54
- for (const t of [...b.map(p), ...g.map(p)]) {
55
- const m = C(t);
56
- j.has(m) || (j.add(m), k.push(t));
68
+ const b = (S ?? []).filter((o) => !y.has(o.id) && !y.has(String(o.id))).map(C).filter((o) => o != null), h = [], j = /* @__PURE__ */ new Set();
69
+ for (const o of [...b, ...g.map(k)]) {
70
+ const l = F(o);
71
+ j.has(l) || (j.add(l), h.push(o));
57
72
  }
58
- d[u.field] = {
59
- ...k.length ? { create: k } : {},
60
- ...h.length ? { delete: h } : {}
61
- }, r.add(f);
73
+ u[f.field] = {
74
+ ...h.length ? { create: h } : {},
75
+ ...p.length ? { delete: p } : {}
76
+ }, _.add(m);
62
77
  }
63
- return r;
78
+ return _;
64
79
  }
65
- const O = 500;
66
- function E(n) {
67
- var s;
68
- const l = /* @__PURE__ */ new Set();
69
- for (const [e, c] of n.rows) {
70
- if (c.length === 0 || (s = n.skip) != null && s.has(e)) continue;
71
- const d = e.indexOf(".");
72
- if (d < 0) continue;
73
- const _ = e.slice(0, d), r = e.slice(d + 1), o = n.relations.find(
74
- (i) => i.one_collection === n.collection && i.many_collection === _ && i.many_field === r && !!i.one_field && i.junction_field == null
80
+ const z = 500;
81
+ function W(n) {
82
+ var a;
83
+ const r = /* @__PURE__ */ new Set();
84
+ for (const [e, s] of n.rows) {
85
+ if (s.length === 0 || (a = n.skip) != null && a.has(e)) continue;
86
+ const u = e.indexOf(".");
87
+ if (u < 0) continue;
88
+ const d = e.slice(0, u), _ = e.slice(u + 1), t = n.relations.find(
89
+ (i) => i.one_collection === n.collection && i.many_collection === d && i.many_field === _ && !!i.one_field && i.junction_field == null
75
90
  );
76
- o != null && o.one_field && (c.length > O || c.some((i) => Object.keys(i).some((f) => f.startsWith("__o2m_"))) || (n.payload[o.one_field] = c.map((i) => {
77
- const { id: f, ...g } = i;
91
+ t != null && t.one_field && (s.length > z || s.some((i) => Object.keys(i).some((m) => m.startsWith("__o2m_"))) || (n.payload[t.one_field] = s.map((i) => {
92
+ const { id: m, ...g } = i;
78
93
  return g;
79
- }), l.add(e)));
94
+ }), r.add(e)));
80
95
  }
81
- return l;
96
+ return r;
82
97
  }
83
98
  export {
84
- A as foldStagedLinks,
85
- E as foldStagedRows
99
+ M as foldStagedLinks,
100
+ W as foldStagedRows
86
101
  };