@nivaro/react 0.3.135 → 0.3.136

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,43 @@
1
+ import { useRef as w, useEffect as p, useSyncExternalStore as m } from "react";
2
+ const u = "nvr:open-integrations", g = (e, n) => `${e}\0${String(n)}`, s = /* @__PURE__ */ new Map(), c = /* @__PURE__ */ new Set(), E = () => {
3
+ for (const e of c) e();
4
+ }, v = (e) => (c.add(e), () => {
5
+ c.delete(e);
6
+ });
7
+ function h(e, n) {
8
+ const o = g(e, n);
9
+ return m(
10
+ v,
11
+ () => (s.get(o) ?? 0) > 0,
12
+ () => !1
13
+ );
14
+ }
15
+ function I(e, n, o) {
16
+ if (typeof window > "u") return !1;
17
+ const t = { collection: e, item: String(n), submissionId: o };
18
+ return window.dispatchEvent(new CustomEvent(u, { detail: t })), t.handled || (t.force = !0, window.dispatchEvent(new CustomEvent(u, { detail: t }))), t.handled === !0;
19
+ }
20
+ function y(e, n, o, t) {
21
+ const f = w(t);
22
+ f.current = t;
23
+ const a = w(o);
24
+ a.current = o, p(() => {
25
+ const i = g(e, n);
26
+ s.set(i, (s.get(i) ?? 0) + 1), E();
27
+ const l = (d) => {
28
+ const r = d.detail;
29
+ !r || r.handled || r.collection !== e || r.item !== String(n) || !r.force && !a.current() || (r.handled = !0, f.current(r.submissionId ?? null));
30
+ };
31
+ return window.addEventListener(u, l), () => {
32
+ window.removeEventListener(u, l);
33
+ const d = (s.get(i) ?? 1) - 1;
34
+ d > 0 ? s.set(i, d) : s.delete(i), E();
35
+ };
36
+ }, [e, n]);
37
+ }
38
+ export {
39
+ u as OPEN_INTEGRATIONS_EVENT,
40
+ I as openRecordIntegrations,
41
+ h as useIntegrationsOpenable,
42
+ y as useIntegrationsOpener
43
+ };
@@ -0,0 +1,137 @@
1
+ import { withoutMoveLabel as k } from "../../lib/partner-failure.js";
2
+ import { sendReason as F, atClock as P, sendStatusWords as q } from "../../lib/send-status.js";
3
+ import { serverNow as L } from "../../lib/server-clock.js";
4
+ const N = 15 * 6e4, p = (e) => {
5
+ const t = e ? new Date(e).getTime() : Number.NaN;
6
+ return Number.isFinite(t) ? t : Number.NaN;
7
+ }, D = (e, t) => !!e && !!t && e.toUpperCase() === t.toUpperCase(), H = /* @__PURE__ */ new Set(["failed", "rejected"]);
8
+ function W(e) {
9
+ return p(e.finished_at ?? e.started_at);
10
+ }
11
+ function j(e) {
12
+ return p(e.finished_at ?? e.started_at);
13
+ }
14
+ function A(e, t) {
15
+ const o = F(k(e, t));
16
+ return o.quoted ? o.text.toLowerCase() : "";
17
+ }
18
+ function B(e, t) {
19
+ if (!D(e.transition_id, t.transition_id)) return !1;
20
+ const o = W(t), h = p(e.first_failed_at), s = p(e.last_failed_at);
21
+ if (!Number.isFinite(o)) return !0;
22
+ const n = Number.isFinite(h) ? h : s, r = Number.isFinite(s) ? s : h;
23
+ return Number.isFinite(n) ? o >= n - N && o <= r + N : !0;
24
+ }
25
+ function U(e, t) {
26
+ var f, _, d, g;
27
+ if (!H.has((e.status ?? "").toLowerCase())) return null;
28
+ const o = ((f = t.problem) == null ? void 0 : f.transition_label) ?? ((_ = t.held) == null ? void 0 : _.transition_label) ?? null, h = [(d = t.problem) == null ? void 0 : d.reason, (g = t.held) == null ? void 0 : g.error].filter((b) => !!b), s = [
29
+ t.problem ? W(t.problem) : Number.NaN,
30
+ t.held ? p(t.held.last_failed_at) : Number.NaN
31
+ ].filter(Number.isFinite), n = j(e);
32
+ if (!Number.isFinite(n) || s.length === 0) return null;
33
+ const r = Math.min(...s.map((b) => Math.abs(b - n)));
34
+ if (r > N) return null;
35
+ const m = (e.api ?? "").trim().toLowerCase(), c = A(e.reason, null), u = [o ?? "", ...h].join(" ").toLowerCase(), a = m.length >= 2 && u.includes(m), i = !!c && h.some((b) => A(b, o) === c);
36
+ return a || i ? r : null;
37
+ }
38
+ function K(e) {
39
+ var c, u;
40
+ const t = e.problem ?? null, o = Array.isArray(e.held) ? e.held : [], h = Array.isArray(e.sends) ? e.sends : [], s = /* @__PURE__ */ new Set(), n = ((c = t == null ? void 0 : t.unanswered) == null ? void 0 : c.submission_id) ?? null;
41
+ n != null && s.add(n);
42
+ const r = [], m = /* @__PURE__ */ new Set();
43
+ if (t) {
44
+ const a = t.unanswered ? null : o.find((i) => B(i, t)) ?? null;
45
+ a && m.add(a), r.push({ problem: t, held: a, send: null });
46
+ }
47
+ for (const a of o) m.has(a) || r.push({ problem: null, held: a, send: null });
48
+ for (const a of r) {
49
+ if ((u = a.problem) != null && u.unanswered) continue;
50
+ let i = null;
51
+ for (const f of h) {
52
+ if (s.has(f.id)) continue;
53
+ const _ = U(f, a);
54
+ _ != null && (!i || _ < i.gap) && (i = { f, gap: _ });
55
+ }
56
+ i && (a.send = i.f, s.add(i.f.id));
57
+ }
58
+ return { events: r, spokenSendIds: [...s] };
59
+ }
60
+ function Q(e) {
61
+ return !!e.problem && !e.held && !e.send;
62
+ }
63
+ const y = {
64
+ validation: "The other system refused what was sent",
65
+ not_found: "The other system could not find what was referred to",
66
+ auth: "The other system did not accept this app’s sign-in",
67
+ transient: "The other system could not be reached",
68
+ rate_limited: "The other system asked us to slow down",
69
+ unknown: "The other system refused it"
70
+ }, V = /* @__PURE__ */ new Set(["transient", "rate_limited", "auth"]);
71
+ function M(e, t = L()) {
72
+ const o = Math.ceil((new Date(e).getTime() - t) / 6e4);
73
+ return !Number.isFinite(o) || o <= 0 ? "on the next check" : o < 60 ? `in ${o} min` : `in ${Math.round(o / 60)}h`;
74
+ }
75
+ const X = {
76
+ refused: "did not go through. The record was not moved.",
77
+ interrupted: "stopped before it finished.",
78
+ failed: "ran into a problem."
79
+ };
80
+ function Y(e, t = {}) {
81
+ var S, x, C, I, E, R;
82
+ const o = t.now ?? L(), h = t.time ?? ((l) => P(l, o)), { problem: s, held: n, send: r } = e, m = ((S = s == null ? void 0 : s.transition_label) == null ? void 0 : S.trim()) || ((x = n == null ? void 0 : n.transition_label) == null ? void 0 : x.trim()) || "An automatic move", c = ((s == null ? void 0 : s.reason) ?? (n == null ? void 0 : n.error) ?? (r == null ? void 0 : r.reason) ?? "").trim() || null, u = ((C = (r == null ? void 0 : r.error_class) ?? (n == null ? void 0 : n.error_class) ?? null) == null ? void 0 : C.toLowerCase()) ?? null, a = r ? q(r, { now: o }) : null;
83
+ let i = "", f = !1;
84
+ if ((I = t.hostWords) != null && I.trim()) i = t.hostWords.trim();
85
+ else if (a != null && a.lead)
86
+ i = a.lead, f = /\b(at|on) .*\d{1,2}:\d{2}/.test(a.lead);
87
+ else if (c) {
88
+ const l = F(k(c, m)), v = !l.quoted && /^The other system refused the request\.?$/.test(l.text);
89
+ l.quoted ? i = `${y[u ?? ""] ?? "The other system refused it"}: “${l.text}.”` : l.text && !v ? i = /[.!?…]$/.test(l.text) ? l.text : `${l.text}.` : u && y[u] && (i = `${y[u]}.`);
90
+ }
91
+ const _ = s ? X[s.result] : "did not go through. The record was not moved.", d = [], g = (E = r == null ? void 0 : r.retry) != null && E.at ? r.retry : null;
92
+ if (g)
93
+ d.push(
94
+ `It is tried again ${M(g.at, o)} (attempt ${g.attempt} of ${g.of}).`
95
+ );
96
+ else if (n != null && n.retry_at)
97
+ d.push(`It tries the same push again ${M(n.retry_at, o)}.`);
98
+ else if (u === "auth")
99
+ d.push("An administrator needs to check the connection to the other system.");
100
+ else if (u && V.has(u))
101
+ d.push(
102
+ t.canRetry ? "Nothing on the record needs to change: try again." : t.canOpenIntegrations ? "Nothing on the record needs to change: send it again from Integrations." : "Nothing on the record needs to change: ask an administrator to send it again."
103
+ );
104
+ else {
105
+ const l = i ? "Change that on the record" : "Check the record";
106
+ n ? d.push(
107
+ `It won’t send the same request again. ${l} and it sends again by itself${t.canRetry ? ", or try again" : ""}.`
108
+ ) : t.canRetry ? d.push(`${l}, then try again.`) : t.canOpenIntegrations ? d.push(`${l}, then send it again from Integrations.`) : d.push(`${l}, then ask for the move to be made again.`);
109
+ }
110
+ t.canMoveByHand && d.push("Or move the record by hand.");
111
+ const b = r ? r.finished_at ?? r.started_at : (s == null ? void 0 : s.finished_at) ?? (s == null ? void 0 : s.started_at) ?? (n == null ? void 0 : n.last_failed_at) ?? null, O = [
112
+ t.startedBy ?? (n && !s ? "Started automatically" : null),
113
+ !f && b ? h(b) : null,
114
+ n && n.attempts > 1 ? `${n.attempts} attempts` : null
115
+ ].filter(Boolean).join(" · "), T = c ? /\bHTTP (\d{3})\b/.exec(c) : null, w = [];
116
+ s && w.push({ label: "Run", value: `#${s.id}` }), r && w.push({ label: "Send", value: `#${r.id} (${r.api})` });
117
+ const $ = (s == null ? void 0 : s.transition_id) ?? (n == null ? void 0 : n.transition_id) ?? null;
118
+ return $ && w.push({ label: "Transition", value: $ }), {
119
+ label: m,
120
+ headline: _,
121
+ reason: i,
122
+ reasonFromHost: !!((R = t.hostWords) != null && R.trim()),
123
+ next: d.join(" "),
124
+ meta: O,
125
+ raw: c,
126
+ status: T ? Number(T[1]) : null,
127
+ errorClass: u,
128
+ ids: w
129
+ };
130
+ }
131
+ export {
132
+ N as SAME_EVENT_WINDOW_MS,
133
+ K as composePipelineNotices,
134
+ Q as isPlainProblem,
135
+ Y as pipelineEventWords,
136
+ M as retryWhen
137
+ };
@@ -1,11 +1,11 @@
1
1
  import { describeError as R } from "./error-message.js";
2
- import { partnerWords as k, partnerOutcome as I, partnerSaid as U } from "./partner-failure.js";
3
- import { getDisplayTimezone as v } from "./utils.js";
4
- const T = "Integrations at the top of the record";
5
- function A(t, e = Date.now()) {
2
+ import { partnerWords as A, partnerOutcome as I, partnerSaid as v } from "./partner-failure.js";
3
+ import { getDisplayTimezone as U } from "./utils.js";
4
+ const k = "Integrations at the top of the record";
5
+ function F(t, e = Date.now()) {
6
6
  const n = t instanceof Date ? t : new Date(t);
7
7
  if (Number.isNaN(n.getTime())) return "";
8
- const r = v(), o = r ? { timeZone: r } : {}, i = (c) => new Intl.DateTimeFormat("en-US", {
8
+ const r = U(), o = r ? { timeZone: r } : {}, i = (c) => new Intl.DateTimeFormat("en-US", {
9
9
  ...o,
10
10
  year: "numeric",
11
11
  month: "numeric",
@@ -20,12 +20,12 @@ function A(t, e = Date.now()) {
20
20
  )}, ${u}`;
21
21
  }
22
22
  function L(t, e = Date.now()) {
23
- const n = A(t, e);
23
+ const n = F(t, e);
24
24
  if (!n) return "";
25
25
  const r = /^(.+), (\d{1,2}:\d{2}\s?[AP]M)$/.exec(n);
26
26
  return r ? `on ${r[1]} at ${r[2]}` : `at ${n}`;
27
27
  }
28
- function f(t) {
28
+ function y(t) {
29
29
  if (!Number.isFinite(t) || t < 6e4) return "less than a minute";
30
30
  const e = Math.round(t / 6e4);
31
31
  if (e < 60) return `${e} minute${e === 1 ? "" : "s"}`;
@@ -37,26 +37,26 @@ function f(t) {
37
37
  function z(t) {
38
38
  const e = (t ?? "").trim();
39
39
  if (!e) return { text: "", quoted: !1 };
40
- const n = /\bHTTP \d{3}: /.exec(e), r = k(n ? e.slice(n.index) : e);
41
- return r ? { text: F(r).replace(/[\s.,;:]+$/, ""), quoted: !0 } : { text: S(e), quoted: !1 };
40
+ const n = /\bHTTP \d{3}: /.exec(e), r = A(n ? e.slice(n.index) : e);
41
+ return r ? { text: S(r).replace(/[\s.,;:]+$/, ""), quoted: !0 } : { text: C(e), quoted: !1 };
42
42
  }
43
- function F(t) {
43
+ function S(t) {
44
44
  const e = t.replace(
45
- /(^|[.!]\s*)(please\s+)?contact\s+(support|(the\s+)?help\s*desk|your\s+(system\s+)?administrator|an?\s+administrator)[^.!]*[.!]?\s*$/i,
45
+ /(^|[.!…]\s*)(please\s+)?contact\s+(?:(?:the|your)\s+)?(?:[\w&'-]+\s+){0,2}?(?:customer\s+|technical\s+)?(support|help\s*desk|service\s*desk|(system\s+)?administrator)\b[^.!]*[.!]?\s*$/i,
46
46
  "$1"
47
47
  );
48
48
  return e.trim() ? e.trim() : t;
49
49
  }
50
- function S(t) {
50
+ function C(t) {
51
51
  const e = (t ?? "").trim();
52
52
  if (!e) return "";
53
- const n = /\bHTTP \d{3}: /.exec(e), r = k(n ? e.slice(n.index) : e) ?? I(e) ?? U(e);
54
- return r ? F(r).replace(/[\s.,;:]+$/, "") : R({ message: e }, "").title.replace(/[\s.,;:]+$/, "");
53
+ const n = /\bHTTP \d{3}: /.exec(e), r = A(n ? e.slice(n.index) : e) ?? I(e) ?? v(e);
54
+ return r ? S(r).replace(/[\s.,;:]+$/, "") : R({ message: e }, "").title.replace(/[\s.,;:]+$/, "");
55
55
  }
56
56
  const E = /* @__PURE__ */ new Set(["transient", "rate_limited"]);
57
57
  function j(t, e = {}) {
58
- var _, p, x, N;
59
- const n = e.now ?? Date.now(), r = e.time ?? ((s) => A(s, n)), o = (s) => e.time ? `at ${e.time(s)}` : L(s, n);
58
+ var _, x, N, D;
59
+ const n = e.now ?? Date.now(), r = e.time ?? ((s) => F(s, n)), o = (s) => e.time ? `at ${e.time(s)}` : L(s, n);
60
60
  e.reason;
61
61
  const i = ((_ = t.api) == null ? void 0 : _.trim()) || "the other system", u = (t.status ?? "").toLowerCase(), d = t.finished_at ?? t.started_at;
62
62
  if (u === "sending") {
@@ -64,7 +64,7 @@ function j(t, e = {}) {
64
64
  return Number.isFinite(s) && n > s ? {
65
65
  state: "no_answer",
66
66
  tone: "held",
67
- text: `No answer from ${i} after ${f(n - new Date(t.started_at).getTime())} — the send is held and will not be repeated automatically. An administrator can mark it received or not received.`
67
+ text: `No answer from ${i} after ${y(n - new Date(t.started_at).getTime())} — the send is held and will not be repeated automatically. An administrator can mark it received or not received.`
68
68
  } : {
69
69
  state: "sending",
70
70
  tone: "busy",
@@ -72,7 +72,7 @@ function j(t, e = {}) {
72
72
  };
73
73
  }
74
74
  if (u === "pending") {
75
- const s = t.usual_answer_ms != null && t.usual_answer_ms > 0 ? ` · usually answers within ${f(t.usual_answer_ms)}` : "";
75
+ const s = t.usual_answer_ms != null && t.usual_answer_ms > 0 ? ` · usually answers within ${y(t.usual_answer_ms)}` : "";
76
76
  return {
77
77
  state: "waiting",
78
78
  tone: "waiting",
@@ -80,39 +80,42 @@ function j(t, e = {}) {
80
80
  };
81
81
  }
82
82
  if (u === "accepted") {
83
- const s = (p = t.external_ref) == null ? void 0 : p.trim(), h = ((x = t.ref_label) == null ? void 0 : x.trim()) || "reference";
83
+ const s = (x = t.external_ref) == null ? void 0 : x.trim(), w = ((N = t.ref_label) == null ? void 0 : N.trim()) || "reference";
84
84
  return {
85
85
  state: "accepted",
86
86
  tone: "ok",
87
- text: `${i} accepted it ${o(d)}${s ? ` — ${h} ${s}` : ""}`
87
+ text: `${i} accepted it ${o(d)}${s ? ` — ${w} ${s}` : ""}`
88
88
  };
89
89
  }
90
- const c = (t.error_class ?? "").toLowerCase(), m = e.reason ? { text: e.reason(t.reason), quoted: !1 } : z(t.reason), a = t.retry && t.retry.at ? t.retry : null, y = a ? new Date(a.at).getTime() : Number.NaN, $ = a ? `${Number.isFinite(y) && y > n ? `Retrying at ${r(a.at)}` : "Retrying now"} (attempt ${a.attempt} of ${a.of})` : "", g = (N = t.no_retry) == null ? void 0 : N.trim(), w = (s) => a ? { retry_at: a.at } : s && g ? { detail: `It won't be sent again automatically — ${g}.` } : {};
90
+ const c = (t.error_class ?? "").toLowerCase(), m = e.reason ? { text: e.reason(t.reason), quoted: !1 } : z(t.reason), a = t.retry && t.retry.at ? t.retry : null, g = a ? new Date(a.at).getTime() : Number.NaN, h = a ? `${Number.isFinite(g) && g > n ? `Retrying at ${r(a.at)}` : "Retrying now"} (attempt ${a.attempt} of ${a.of})` : "", b = (D = t.no_retry) == null ? void 0 : D.trim(), f = (s) => a ? { retry_at: a.at } : s && b ? { detail: `It won't be sent again automatically — ${b}.` } : {};
91
91
  if (E.has(c)) {
92
- const h = c === "rate_limited" ? `${i} was too busy to take it ${o(d)} — nothing was sent.` : `Couldn't reach ${i} — nothing was sent.`, D = !a && t.can_resend === !0, M = a ? ` ${$}` : D ? "" : " Ask an admin to send it again.";
92
+ const w = c === "rate_limited" ? `${i} was too busy to take it ${o(d)} — nothing was sent.` : `Couldn't reach ${i} — nothing was sent.`, T = !a && t.can_resend === !0, M = a ? ` ${h}` : T ? "" : " Ask an admin to send it again.";
93
93
  return {
94
94
  state: "not_taken",
95
95
  tone: a ? "waiting" : "problem",
96
- text: `${h}${M}`,
97
- ...D ? { resend: !0 } : {},
98
- ...w(!0)
96
+ text: `${w}${M}`,
97
+ lead: w,
98
+ ...T ? { resend: !0 } : {},
99
+ ...f(!0)
99
100
  };
100
101
  }
101
- const b = `${i} refused it ${o(d)}`;
102
+ const l = `${i} refused it ${o(d)}`;
102
103
  if (c === "auth")
103
104
  return {
104
105
  state: "refused",
105
106
  tone: a ? "waiting" : "problem",
106
- text: `${b}: it did not accept this app's sign-in. ${a ? $ : `An administrator needs to check the connection to ${i}.`}`,
107
- ...w(!0)
107
+ text: `${l}: it did not accept this app's sign-in. ${a ? h : `An administrator needs to check the connection to ${i}.`}`,
108
+ lead: `${l}: it did not accept this app's sign-in.`,
109
+ ...f(!0)
108
110
  };
109
- const l = !m.quoted && /^The other system refused the request\.?$/.test(m.text) ? "" : m.text, C = l ? m.quoted ? `: “${l}.”` : `: ${l}.` : " without giving a reason.", q = u === "rejected" || ["validation", "not_found"].includes(c);
111
+ const $ = !m.quoted && /^The other system refused the request\.?$/.test(m.text) ? "" : m.text, p = $ ? m.quoted ? `: “${$}.”` : `: ${$}.` : " without giving a reason.", q = u === "rejected" || ["validation", "not_found"].includes(c);
110
112
  return {
111
113
  state: "refused",
112
114
  tone: a ? "waiting" : "problem",
113
- text: `${b}${C} ${a ? $ : l ? `Change that on the record, then send it again from ${T}.` : `Check the record, then send it again from ${T}.`}`,
115
+ text: `${l}${p} ${a ? h : $ ? `Change that on the record, then send it again from ${k}.` : `Check the record, then send it again from ${k}.`}`,
116
+ lead: `${l}${p}`,
114
117
  // "Repeating it cannot fix it" is already what the next step says.
115
- ...w(!q)
118
+ ...f(!q)
116
119
  };
117
120
  }
118
121
  function G(t, e = Date.now()) {
@@ -120,7 +123,7 @@ function G(t, e = Date.now()) {
120
123
  const n = new Date(t).getTime();
121
124
  if (!Number.isFinite(n) || n <= e) return null;
122
125
  const r = Math.ceil((n - e) / 6e4);
123
- return r < 60 ? `in ${r} minute${r === 1 ? "" : "s"}` : `in ${f(n - e)}`;
126
+ return r < 60 ? `in ${r} minute${r === 1 ? "" : "s"}` : `in ${y(n - e)}`;
124
127
  }
125
128
  function K(t, e = Date.now()) {
126
129
  var o;
@@ -135,14 +138,14 @@ function Z(t, e = Date.now()) {
135
138
  return !Number.isFinite(n) || n < e ? null : n - e + 500;
136
139
  }
137
140
  export {
138
- A as atClock,
141
+ F as atClock,
139
142
  L as atWhen,
140
143
  Z as msUntilNoAnswer,
141
144
  K as msUntilRetryTick,
142
- f as plainDuration,
143
- S as plainSendReason,
145
+ y as plainDuration,
146
+ C as plainSendReason,
144
147
  G as retryCountdown,
145
148
  z as sendReason,
146
149
  j as sendStatusWords,
147
- F as withoutContactLine
150
+ S as withoutContactLine
148
151
  };
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@nivaro/react",
3
- "version": "0.3.135",
3
+ "version": "0.3.136",
4
4
  "description": "React hooks and components for Nivaro CMS forms",
5
5
  "type": "module",
6
6
  "main": "./dist/index.js",