@ai-matrx/records-ui 0.115.47 → 0.115.49

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.
package/CHANGELOG.md CHANGED
@@ -1,5 +1,17 @@
1
1
  # Changelog — @ai-matrx/records-ui
2
2
 
3
+ ## 0.115.49
4
+
5
+ Automatic changed-only republish (docs/metadata drift since the last tag — see
6
+ `git diff npm/records-ui/v0.115.48..npm/records-ui/v0.115.49 -- apps/shared/records-ui`).
7
+ No source changes intended and no consumer action required.
8
+
9
+ ## 0.115.48
10
+
11
+ Automatic changed-only republish (docs/metadata drift since the last tag — see
12
+ `git diff npm/records-ui/v0.115.47..npm/records-ui/v0.115.48 -- apps/shared/records-ui`).
13
+ No source changes intended and no consumer action required.
14
+
3
15
  ## 0.115.47
4
16
 
5
17
  Automatic changed-only republish (docs/metadata drift since the last tag — see
package/dist/index.cjs CHANGED
@@ -1617,29 +1617,29 @@ function clauseFor(f, value, now = /* @__PURE__ */ new Date()) {
1617
1617
  }
1618
1618
  case "select": {
1619
1619
  const ticked = value.values ?? (value.value === "" ? [] : [value.value]);
1620
- const blank = ticked.includes(BLANK_VALUE);
1620
+ const blank2 = ticked.includes(BLANK_VALUE);
1621
1621
  const words2 = ticked.filter((w) => w !== BLANK_VALUE);
1622
- if (!blank && words2.length === 0) return value.negated ? null : { rule: constant(false) };
1622
+ if (!blank2 && words2.length === 0) return value.negated ? null : { rule: constant(false) };
1623
1623
  const present = op("present", field(f));
1624
1624
  const kind = String(editorKindFor(f));
1625
1625
  if (kind === "datetime" || kind === "date") {
1626
1626
  if (words2.length === 0) return { rule: value.negated ? present : not(present) };
1627
1627
  const holdsDays = String((f.config ?? {})["kind"] ?? "") === "date";
1628
- if (words2.length === 1 && !blank && !value.negated) return { flat: { [f.key]: dayWindow(words2[0], holdsDays) } };
1628
+ if (words2.length === 1 && !blank2 && !value.negated) return { flat: { [f.key]: dayWindow(words2[0], holdsDays) } };
1629
1629
  const leaves = words2.map((day2) => withinWindow(f, dayWindow(day2, holdsDays)));
1630
1630
  const anyDay = leaves.length === 1 ? leaves[0] : op("or", ...leaves);
1631
- if (!value.negated) return { rule: blank ? op("or", not(present), anyDay) : anyDay };
1632
- return { rule: blank ? op("and", present, not(anyDay)) : not(anyDay) };
1631
+ if (!value.negated) return { rule: blank2 ? op("or", not(present), anyDay) : anyDay };
1632
+ return { rule: blank2 ? op("and", present, not(anyDay)) : not(anyDay) };
1633
1633
  }
1634
1634
  const asStored = (w) => (0, import_first_page.holdsNumbers)(f) || kind === "rating" ? Number.isFinite(Number(w)) ? Number(w) : w : kind === "checkbox" && (w === "true" || w === "false") ? w === "true" : w;
1635
1635
  const stored = words2.map(asStored);
1636
1636
  const anyWord = stored.length === 0 ? null : stored.length === 1 ? op("eq", field(f), constant(stored[0])) : op("or", ...stored.map((w) => op("eq", field(f), constant(w))));
1637
1637
  if (!value.negated) {
1638
- if (!blank && stored.length === 1) return { flat: { [f.key]: stored[0] } };
1639
- if (!blank) return { rule: anyWord };
1638
+ if (!blank2 && stored.length === 1) return { flat: { [f.key]: stored[0] } };
1639
+ if (!blank2) return { rule: anyWord };
1640
1640
  return { rule: anyWord ? op("or", not(present), anyWord) : not(present) };
1641
1641
  }
1642
- if (blank) return { rule: anyWord ? op("and", present, not(anyWord)) : present };
1642
+ if (blank2) return { rule: anyWord ? op("and", present, not(anyWord)) : present };
1643
1643
  return { rule: op("or", not(present), not(anyWord)) };
1644
1644
  }
1645
1645
  case "boolean":
@@ -18093,6 +18093,7 @@ function useMergedGridControls(args) {
18093
18093
  /* @__PURE__ */ (0, import_jsx_runtime63.jsx)(
18094
18094
  import_design_system55.Dialog,
18095
18095
  {
18096
+ modal: true,
18096
18097
  open: dialog?.kind === "retype",
18097
18098
  onOpenChange: (open) => {
18098
18099
  if (!open) close();
@@ -21757,7 +21758,10 @@ var IMPORT_KINDS = [
21757
21758
  { kind: "date", label: "Date" },
21758
21759
  { kind: "number", label: "Number" },
21759
21760
  { kind: "currency", label: "Money" },
21760
- { kind: "checkbox", label: "Checkbox" }
21761
+ { kind: "checkbox", label: "Checkbox" },
21762
+ { kind: "email", label: "Email" },
21763
+ { kind: "phone", label: "Phone" },
21764
+ { kind: "url", label: "Link" }
21761
21765
  ];
21762
21766
  function importKindOfPlan(type) {
21763
21767
  if (type === "text" || type === "long_text") return "text";
@@ -21765,6 +21769,7 @@ function importKindOfPlan(type) {
21765
21769
  if (type === "number" || type === "range" || type === "integer" || type === "decimal") return "number";
21766
21770
  if (type === "currency") return "currency";
21767
21771
  if (type === "checkbox" || type === "boolean") return "checkbox";
21772
+ if (type === "email" || type === "phone" || type === "url") return type;
21768
21773
  return null;
21769
21774
  }
21770
21775
  function declarationFor(kind) {
@@ -21802,6 +21807,19 @@ function exactDuplicateRows(rows, headers) {
21802
21807
  });
21803
21808
  return out;
21804
21809
  }
21810
+ var EMAIL = /^[^\s@]+@[^\s@]+\.[^\s@]{2,}$/;
21811
+ var PHONE = /^\+?[\d\s().-]{7,20}$/;
21812
+ var URLISH = /^(https?:\/\/|www\.)\S+$/i;
21813
+ function looksLike(rows, header) {
21814
+ const cells = rows.map((r) => String(r[header] ?? "").trim()).filter((c) => c !== "");
21815
+ if (cells.length < 2) return null;
21816
+ const share = (test) => cells.filter(test).length / cells.length;
21817
+ if (share((c) => EMAIL.test(c)) >= 0.6) return "email";
21818
+ if (share((c) => URLISH.test(c)) >= 0.6) return "url";
21819
+ const isPhone = (c) => PHONE.test(c) && /[\d]/.test(c) && c.replace(/\D/g, "").length >= 7 && c.replace(/\D/g, "").length <= 15 && /[()\s.+-]/.test(c);
21820
+ if (share(isPhone) >= 0.6) return "phone";
21821
+ return null;
21822
+ }
21805
21823
 
21806
21824
  // src/importCombine.ts
21807
21825
  var import_records42 = require("@ai-matrx/records");
@@ -22406,6 +22424,7 @@ function ImportWizard({ tableId, source, onDone, onWrote, autoRun, className })
22406
22424
  for (const c of plan?.columns ?? []) {
22407
22425
  const k = importKindOfPlan(c.type);
22408
22426
  out[c.header] = (k === "text" || c.type === "select") && looksLikeDates(effectiveRows, c.header) ? "date" : k;
22427
+ if (out[c.header] === "text") out[c.header] = looksLike(effectiveRows, c.header) ?? "text";
22409
22428
  if (combined?.multiValued.has(c.header) && (k === null || c.type === "select")) out[c.header] = "text";
22410
22429
  }
22411
22430
  return out;
@@ -22586,6 +22605,25 @@ function ImportWizard({ tableId, source, onDone, onWrote, autoRun, className })
22586
22605
  }
22587
22606
  setColumnsAdded(added);
22588
22607
  }
22608
+ if (makesColumns && plan) {
22609
+ const wantedKeys = plan.columns.map((c) => c.header).filter((h) => !leftOut.has(h)).map((h) => sendMapping[h]).filter((k) => Boolean(k));
22610
+ const now = await client.fields({ table_id: tableId });
22611
+ if (now.ok) {
22612
+ const order = new Map(wantedKeys.map((k, i) => [k, i]));
22613
+ const current = now.data.filter((f) => order.has(f.key)).sort((a, b) => (a.sort ?? 0) - (b.sort ?? 0)).map((f) => f.key);
22614
+ const inFileOrder = current.every((k, i) => k === wantedKeys.filter((w) => current.includes(w))[i]);
22615
+ if (!inFileOrder) {
22616
+ const slots = now.data.filter((f) => order.has(f.key)).map((f) => f.sort ?? 0).sort((a, b) => a - b);
22617
+ const byKey = new Map(now.data.map((f) => [f.key, f]));
22618
+ const present = wantedKeys.filter((k) => byKey.has(k));
22619
+ for (let i = 0; i < present.length; i += 1) {
22620
+ const f = byKey.get(present[i]);
22621
+ const to = slots[i] ?? (i + 1) * 10;
22622
+ if ((f.sort ?? 0) !== to) await client.fieldUpdate({ field_id: f.id, patch: { sort: to } });
22623
+ }
22624
+ }
22625
+ }
22626
+ }
22589
22627
  setPhase("writing");
22590
22628
  const opened = await client.importBegin({
22591
22629
  table_id: tableId,
@@ -23806,9 +23844,9 @@ var PERCENT = /^[+-]?\d+(?:\.\d+)?\s?%$/;
23806
23844
  var ISO_DATE = /^\d{4}-\d{2}-\d{2}(?:[T ]\d{2}:\d{2}(?::\d{2}(?:\.\d+)?)?(?:Z|[+-]\d{2}:?\d{2})?)?$/;
23807
23845
  var SLASH_DATE = /^\d{1,2}\/\d{1,2}\/\d{2,4}$/;
23808
23846
  var WORD_DATE = /^(?:\d{1,2}\s+)?(?:jan|feb|mar|apr|may|jun|jul|aug|sep|sept|oct|nov|dec)[a-z]*\.?\s+\d{1,2}(?:st|nd|rd|th)?,?\s+\d{4}$|^\d{1,2}\s+(?:jan|feb|mar|apr|may|jun|jul|aug|sep|sept|oct|nov|dec)[a-z]*\.?\s+\d{4}$/i;
23809
- var EMAIL = /^[^\s@]+@[^\s@]+\.[^\s@]{2,}$/;
23847
+ var EMAIL2 = /^[^\s@]+@[^\s@]+\.[^\s@]{2,}$/;
23810
23848
  var URL_WORD = /^https?:\/\/\S+$/i;
23811
- var PHONE = /^\+?[\d\s().-]{7,20}$/;
23849
+ var PHONE2 = /^\+?[\d\s().-]{7,20}$/;
23812
23850
  var YES = /* @__PURE__ */ new Set(["yes", "true", "y", "\u2713", "\u2714", "checked", "done"]);
23813
23851
  var NO = /* @__PURE__ */ new Set(["no", "false", "n", "\u2717", "\u2718", "unchecked", ""]);
23814
23852
  function isDate(v) {
@@ -23828,9 +23866,9 @@ function guessColumn(values) {
23828
23866
  }
23829
23867
  if (every((v) => PERCENT.test(v))) return { type: "percent" };
23830
23868
  if (every(isDate)) return { type: "datetime", kind: every((v) => !/\d:\d/.test(v)) ? "date" : "datetime" };
23831
- if (every((v) => EMAIL.test(v))) return { type: "email" };
23869
+ if (every((v) => EMAIL2.test(v))) return { type: "email" };
23832
23870
  if (every((v) => URL_WORD.test(v))) return { type: "url" };
23833
- if (every((v) => PHONE.test(v) && (v.match(/\d/g)?.length ?? 0) >= 7)) return { type: "phone" };
23871
+ if (every((v) => PHONE2.test(v) && (v.match(/\d/g)?.length ?? 0) >= 7)) return { type: "phone" };
23834
23872
  if (filled.some((v) => v.length > 120 || v.includes("\n"))) return { type: "long_text" };
23835
23873
  const distinct = [];
23836
23874
  for (const v of filled) if (!distinct.some((d) => d.toLowerCase() === v.toLowerCase())) distinct.push(v);
@@ -24176,7 +24214,7 @@ function noValueList(columnId2) {
24176
24214
  }
24177
24215
  function facetsFromGroups(columnId2, key, rows, limit, distinctCount, order = "count") {
24178
24216
  const counts = /* @__PURE__ */ new Map();
24179
- let blank = 0;
24217
+ let blank2 = 0;
24180
24218
  let total = 0;
24181
24219
  let maxLength = 0;
24182
24220
  let unlistable = 0;
@@ -24187,7 +24225,7 @@ function facetsFromGroups(columnId2, key, rows, limit, distinctCount, order = "c
24187
24225
  const said2 = raw === null || raw === void 0 ? "" : String(raw).trim();
24188
24226
  const text = order === "newest" && /^\d{4}-\d{2}-\d{2}/.test(said2) ? said2.slice(0, 10) : said2;
24189
24227
  if (text === "") {
24190
- blank += n;
24228
+ blank2 += n;
24191
24229
  continue;
24192
24230
  }
24193
24231
  maxLength = Math.max(maxLength, text.length);
@@ -24205,8 +24243,8 @@ function facetsFromGroups(columnId2, key, rows, limit, distinctCount, order = "c
24205
24243
  return {
24206
24244
  columnId: columnId2,
24207
24245
  totalRows: total,
24208
- filled: total - blank,
24209
- blank,
24246
+ filled: total - blank2,
24247
+ blank: blank2,
24210
24248
  distinctCount: known,
24211
24249
  maxLength,
24212
24250
  unlistable,
@@ -28970,6 +29008,10 @@ function NewTableStart({
28970
29008
  onMade?.();
28971
29009
  setStep({ at: "wizard", tableId: result.data, source, made: true });
28972
29010
  }, [busy, client, fileName, importName, importReady, onMade, parsed, target]);
29011
+ const openImported = (tableId) => {
29012
+ (0, import_react155.tableShapeChanged)(tableId);
29013
+ onOpenTable?.(tableId);
29014
+ };
28973
29015
  const back = () => {
28974
29016
  setError(null);
28975
29017
  setStep({ at: "choose" });
@@ -29165,10 +29207,10 @@ function NewTableStart({
29165
29207
  /* @__PURE__ */ (0, import_jsx_runtime92.jsx)(import_design_system76.Button, { disabled: !importReady || busy, onClick: () => void continueImport(), "data-new-table-continue": "", children: busy ? "Creating\u2026" : "Next: columns" })
29166
29208
  ] });
29167
29209
  } else {
29168
- body = /* @__PURE__ */ (0, import_jsx_runtime92.jsx)("div", { "data-new-table-wizard": "", children: /* @__PURE__ */ (0, import_jsx_runtime92.jsx)(ImportWizard, { tableId: step2.tableId, source: step2.source, autoRun: step2.made, onDone: () => onOpenTable?.(step2.tableId) }) });
29210
+ body = /* @__PURE__ */ (0, import_jsx_runtime92.jsx)("div", { "data-new-table-wizard": "", children: /* @__PURE__ */ (0, import_jsx_runtime92.jsx)(ImportWizard, { tableId: step2.tableId, source: step2.source, autoRun: step2.made, onWrote: () => (0, import_react155.tableShapeChanged)(step2.tableId), onDone: () => openImported(step2.tableId) }) });
29169
29211
  footer = /* @__PURE__ */ (0, import_jsx_runtime92.jsxs)(import_jsx_runtime92.Fragment, { children: [
29170
29212
  !step2.made ? /* @__PURE__ */ (0, import_jsx_runtime92.jsx)(import_design_system76.Button, { variant: "ghost", className: "mr-auto", onClick: () => setStep({ at: "import" }), children: "Back" }) : null,
29171
- /* @__PURE__ */ (0, import_jsx_runtime92.jsx)(import_design_system76.Button, { variant: "outline", onClick: () => onOpenTable?.(step2.tableId), "data-new-table-open": "", children: "Open the table" })
29213
+ /* @__PURE__ */ (0, import_jsx_runtime92.jsx)(import_design_system76.Button, { variant: "outline", onClick: () => openImported(step2.tableId), "data-new-table-open": "", children: "Open the table" })
29172
29214
  ] });
29173
29215
  }
29174
29216
  return /* @__PURE__ */ (0, import_jsx_runtime92.jsxs)(
@@ -32013,10 +32055,10 @@ function ColumnsAtOnce({
32013
32055
  const orderMoved = rows.some((f, i) => f.id !== draft[i]?.id);
32014
32056
  const hiddenChanged = look ? JSON.stringify([...hidden].sort()) !== JSON.stringify([...look.hidden].sort()) : false;
32015
32057
  const lookChanged = Boolean(look) && (hiddenChanged || orderMoved && look.order !== null);
32016
- const blank = draft.find((d) => d.label.trim() === "");
32017
- const dirty = batch.length > 0 || lookChanged || Boolean(blank);
32058
+ const blank2 = draft.find((d) => d.label.trim() === "");
32059
+ const dirty = batch.length > 0 || lookChanged || Boolean(blank2);
32018
32060
  const save = async () => {
32019
- if (blank) return;
32061
+ if (blank2) return;
32020
32062
  setSaving(true);
32021
32063
  try {
32022
32064
  for (const one of batch) {
@@ -32129,7 +32171,7 @@ function ColumnsAtOnce({
32129
32171
  draft.length === 0 ? /* @__PURE__ */ (0, import_jsx_runtime107.jsx)("li", { className: "px-2 py-2 text-muted-foreground", children: "This table has no fields yet." }) : null
32130
32172
  ] }),
32131
32173
  dirty ? /* @__PURE__ */ (0, import_jsx_runtime107.jsxs)("div", { className: "flex flex-wrap items-center gap-2", "data-records-columns-save": "", children: [
32132
- /* @__PURE__ */ (0, import_jsx_runtime107.jsx)(import_design_system92.Button, { size: "sm", disabled: saving || Boolean(blank), onClick: () => void save(), children: saving ? "Saving\u2026" : "Save columns" }),
32174
+ /* @__PURE__ */ (0, import_jsx_runtime107.jsx)(import_design_system92.Button, { size: "sm", disabled: saving || Boolean(blank2), onClick: () => void save(), children: saving ? "Saving\u2026" : "Save columns" }),
32133
32175
  /* @__PURE__ */ (0, import_jsx_runtime107.jsx)(
32134
32176
  import_design_system92.Button,
32135
32177
  {
@@ -32143,7 +32185,7 @@ function ColumnsAtOnce({
32143
32185
  children: "Discard"
32144
32186
  }
32145
32187
  ),
32146
- blank ? /* @__PURE__ */ (0, import_jsx_runtime107.jsx)("span", { className: "text-muted-foreground", children: "Every column needs a name." }) : null
32188
+ blank2 ? /* @__PURE__ */ (0, import_jsx_runtime107.jsx)("span", { className: "text-muted-foreground", children: "Every column needs a name." }) : null
32147
32189
  ] }) : null
32148
32190
  ] });
32149
32191
  }
@@ -41868,6 +41910,41 @@ function ActionInbox({
41868
41910
 
41869
41911
  // src/HistoryPanel.tsx
41870
41912
  var import_react244 = require("react");
41913
+
41914
+ // src/undoChange.ts
41915
+ function blank(value) {
41916
+ return value === null || value === void 0 || value === "";
41917
+ }
41918
+ function sameValue2(a, b) {
41919
+ if (blank(a) || blank(b)) return blank(a) && blank(b);
41920
+ if (a === b) return true;
41921
+ if (typeof a !== "object" || typeof b !== "object") return false;
41922
+ if (Array.isArray(a) !== Array.isArray(b)) return false;
41923
+ const left = a;
41924
+ const right = b;
41925
+ const keys = /* @__PURE__ */ new Set([...Object.keys(left), ...Object.keys(right)]);
41926
+ for (const key of keys) if (!sameValue2(left[key], right[key])) return false;
41927
+ return true;
41928
+ }
41929
+ function planUndo(changes, current, only) {
41930
+ const doc = current ?? {};
41931
+ return changes.filter((change) => !only || change.key === only).map((change) => {
41932
+ const now = doc[change.key];
41933
+ const state = sameValue2(now, change.before) ? "already" : sameValue2(now, change.after) ? "clean" : "conflict";
41934
+ return { key: change.key, label: change.label, current: now, after: change.after, before: change.before, state };
41935
+ });
41936
+ }
41937
+ function undoPatch(lines, replaceLaterEdits) {
41938
+ const patch = {};
41939
+ for (const line of lines) {
41940
+ if (line.state === "already") continue;
41941
+ if (line.state === "conflict" && !replaceLaterEdits) continue;
41942
+ patch[line.key] = blank(line.before) ? null : line.before;
41943
+ }
41944
+ return patch;
41945
+ }
41946
+
41947
+ // src/HistoryPanel.tsx
41871
41948
  var import_react245 = require("@ai-matrx/records/react");
41872
41949
  var import_design_system118 = require("@ai-matrx/design-system");
41873
41950
  var import_format12 = require("@ai-matrx/kit/format");
@@ -42140,6 +42217,7 @@ function HistoryPanel({ tableId, recordId, onAskAboutField, className }) {
42140
42217
  const [error, setError] = (0, import_react244.useState)(null);
42141
42218
  const [open, setOpen] = (0, import_react244.useState)(null);
42142
42219
  const [pending, setPending] = (0, import_react244.useState)({ phase: "idle" });
42220
+ const [undo, setUndo] = (0, import_react244.useState)({ phase: "idle" });
42143
42221
  const seenAtPreview = (0, import_react244.useRef)(null);
42144
42222
  const [said2, setSaid] = (0, import_react244.useState)(null);
42145
42223
  const listRef = (0, import_react244.useRef)(null);
@@ -42189,6 +42267,37 @@ function HistoryPanel({ tableId, recordId, onAskAboutField, className }) {
42189
42267
  }
42190
42268
  setPending({ phase: "confirm", version, fieldKey, preview: asked3.data });
42191
42269
  }
42270
+ async function startUndo(entry, only) {
42271
+ setSaid(null);
42272
+ setPending({ phase: "idle" });
42273
+ setUndo({ phase: "asking", version: entry.version, only });
42274
+ const read = await client.recordRead({ record_id: recordId, withVersion: true });
42275
+ if (!read.ok) {
42276
+ setUndo({ phase: "idle" });
42277
+ setError(read.error);
42278
+ return;
42279
+ }
42280
+ const lines = planUndo(entry.changes, read.data.document, only);
42281
+ setUndo({ phase: "confirm", version: entry.version, only, lines, seen: read.data.version ?? await (0, recordVersions_exports.readVersionNow)(client, recordId) });
42282
+ }
42283
+ async function doUndo(replaceLaterEdits) {
42284
+ if (undo.phase !== "confirm") return;
42285
+ const { version, only, lines, seen } = undo;
42286
+ setUndo({ phase: "writing", version, only });
42287
+ const patch = undoPatch(lines, replaceLaterEdits);
42288
+ const written = await (0, recordVersions_exports.updateRecordAt)(client, { record_id: recordId, patch, version: seen });
42289
+ setUndo({ phase: "idle" });
42290
+ if (!written.ok) {
42291
+ setError(written.error);
42292
+ return;
42293
+ }
42294
+ const count = Object.keys(patch).length;
42295
+ const kept = lines.filter((line) => line.state === "conflict" && !replaceLaterEdits).length;
42296
+ setSaid(
42297
+ `Undone as version ${written.data}. ${count} ${count === 1 ? "value" : "values"} put back` + (kept ? `, ${kept} later ${kept === 1 ? "edit" : "edits"} kept` : "") + ". Every other field is as it was."
42298
+ );
42299
+ await load();
42300
+ }
42192
42301
  async function doRestore(version, fieldKey) {
42193
42302
  setPending({ phase: "writing", version, fieldKey });
42194
42303
  const written = await (0, recordVersions_exports.restoreAt)(client, { record_id: recordId, version, field_key: fieldKey, seenVersion: seenAtPreview.current });
@@ -42247,6 +42356,10 @@ function HistoryPanel({ tableId, recordId, onAskAboutField, className }) {
42247
42356
  canRestore: rights.write && entry.version >= newestMakerVersion && !(entry.migration_id && makerIds.has(entry.migration_id)),
42248
42357
  whyNotRestore: entry.migration_id && makerIds.has(entry.migration_id) ? `This version is a ${migrationWords(makers.find((m) => m.id === entry.migration_id)?.verb ?? "split").toLowerCase()}. Undo it in the Migration log below, so no copy is left behind.` : rights.write && entry.version < newestMakerVersion ? "A newer split still holds some of these values. Undo it in the Migration log below first, so no copy is left behind." : rights.why("write"),
42249
42358
  pending,
42359
+ undo,
42360
+ onUndo: (only) => void startUndo(entry, only),
42361
+ onUndoConfirm: (replace) => void doUndo(replace),
42362
+ onUndoCancel: () => setUndo({ phase: "idle" }),
42250
42363
  onRestore: (fieldKey) => void startRestore(entry.version, fieldKey),
42251
42364
  onConfirm: (fieldKey) => void doRestore(entry.version, fieldKey),
42252
42365
  onCancel: () => setPending({ phase: "idle" }),
@@ -42276,6 +42389,10 @@ function VersionRow({
42276
42389
  canRestore,
42277
42390
  whyNotRestore,
42278
42391
  pending,
42392
+ undo,
42393
+ onUndo,
42394
+ onUndoConfirm,
42395
+ onUndoCancel,
42279
42396
  onRestore,
42280
42397
  onConfirm,
42281
42398
  onCancel,
@@ -42283,6 +42400,9 @@ function VersionRow({
42283
42400
  }) {
42284
42401
  const mine = pending.phase !== "idle" && pending.version === entry.version;
42285
42402
  const confirming = mine && pending.phase === "confirm" ? pending : null;
42403
+ const canUndo = canRestore && entry.operation !== "insert" && entry.version > 1 && entry.changes.length > 0;
42404
+ const undoMine = undo.phase !== "idle" && undo.version === entry.version;
42405
+ const undoConfirming = undoMine && undo.phase === "confirm" ? undo : null;
42286
42406
  return /* @__PURE__ */ (0, import_jsx_runtime137.jsxs)("li", { className: "flex flex-col", children: [
42287
42407
  /* @__PURE__ */ (0, import_jsx_runtime137.jsxs)(
42288
42408
  "div",
@@ -42338,13 +42458,24 @@ function VersionRow({
42338
42458
  {
42339
42459
  change,
42340
42460
  field: byKey.get(change.key),
42341
- canRestore,
42342
- onRestore: () => onRestore(change.key),
42461
+ canRestore: canUndo,
42462
+ onRestore: () => onUndo(change.key),
42343
42463
  ...onAskAboutField ? { onAskAboutField } : {}
42344
42464
  },
42345
42465
  change.key
42346
42466
  )) }),
42347
- canRestore ? confirming ? /* @__PURE__ */ (0, import_jsx_runtime137.jsx)(
42467
+ undoConfirming ? /* @__PURE__ */ (0, import_jsx_runtime137.jsx)(ConfirmUndo, { lines: undoConfirming.lines, onConfirm: onUndoConfirm, onCancel: onUndoCancel }) : undoMine && undo.phase === "writing" ? /* @__PURE__ */ (0, import_jsx_runtime137.jsx)("p", { className: "text-[11px] text-muted-foreground", children: "Undoing\u2026" }) : canRestore && !confirming && !(mine && pending.phase === "writing") ? canUndo ? /* @__PURE__ */ (0, import_jsx_runtime137.jsx)("div", { className: "flex", children: /* @__PURE__ */ (0, import_jsx_runtime137.jsx)(
42468
+ import_design_system118.Button,
42469
+ {
42470
+ size: "sm",
42471
+ variant: "secondary",
42472
+ className: "h-6 px-2 text-[11px]",
42473
+ disabled: undoMine,
42474
+ onClick: () => onUndo(null),
42475
+ children: undoMine ? "Working out what would change\u2026" : "Undo this change"
42476
+ }
42477
+ ) }) : null : null,
42478
+ canRestore && !undoMine ? confirming ? /* @__PURE__ */ (0, import_jsx_runtime137.jsx)(
42348
42479
  ConfirmRestore,
42349
42480
  {
42350
42481
  preview: confirming.preview,
@@ -42426,9 +42557,64 @@ function ChangeLine({
42426
42557
  children: "Who changed this?"
42427
42558
  }
42428
42559
  ) : null,
42429
- canRestore ? /* @__PURE__ */ (0, import_jsx_runtime137.jsx)(import_design_system118.Button, { size: "sm", variant: "ghost", className: "h-5 shrink-0 px-1 text-[10px]", onClick: onRestore, children: "Put this one back" }) : null
42560
+ canRestore ? /* @__PURE__ */ (0, import_jsx_runtime137.jsx)(import_design_system118.Button, { size: "sm", variant: "ghost", className: "h-5 shrink-0 px-1 text-[10px]", onClick: onRestore, children: "Undo this one" }) : null
42561
+ ] });
42562
+ }
42563
+ function ConfirmUndo({
42564
+ lines,
42565
+ onConfirm,
42566
+ onCancel
42567
+ }) {
42568
+ const clean = lines.filter((line) => line.state === "clean");
42569
+ const conflicts = lines.filter((line) => line.state === "conflict");
42570
+ const already = lines.filter((line) => line.state === "already");
42571
+ const nothing = clean.length + conflicts.length === 0;
42572
+ return /* @__PURE__ */ (0, import_jsx_runtime137.jsxs)("div", { "data-undo-confirm": "", className: "flex flex-col gap-1 rounded border border-amber-500/40 bg-amber-500/5 p-2", children: [
42573
+ /* @__PURE__ */ (0, import_jsx_runtime137.jsx)("p", { className: "text-[11px] font-medium", children: nothing ? "Nothing to undo \u2014 every value this version changed already says what it said before." : `Undoing this would put back ${clean.length + conflicts.length} ${clean.length + conflicts.length === 1 ? "value" : "values"}:` }),
42574
+ /* @__PURE__ */ (0, import_jsx_runtime137.jsx)("ul", { className: "flex flex-col gap-0.5", children: [...clean, ...conflicts].map((line) => /* @__PURE__ */ (0, import_jsx_runtime137.jsxs)("li", { "data-undo-line": line.key, "data-undo-state": line.state, className: "text-[11px]", children: [
42575
+ /* @__PURE__ */ (0, import_jsx_runtime137.jsx)("span", { className: "font-medium", children: line.label }),
42576
+ " ",
42577
+ /* @__PURE__ */ (0, import_jsx_runtime137.jsx)("span", { className: "text-muted-foreground", children: say(line.current, void 0) }),
42578
+ /* @__PURE__ */ (0, import_jsx_runtime137.jsx)("span", { className: "mx-1 text-muted-foreground", children: "\u2192" }),
42579
+ /* @__PURE__ */ (0, import_jsx_runtime137.jsx)("strong", { className: "font-medium", children: blankSays(line.before) }),
42580
+ line.state === "conflict" ? /* @__PURE__ */ (0, import_jsx_runtime137.jsx)("span", { className: "ml-1 text-amber-700 dark:text-amber-400", children: "edited again since" }) : null
42581
+ ] }, line.key)) }),
42582
+ already.length ? /* @__PURE__ */ (0, import_jsx_runtime137.jsxs)("p", { className: "text-[11px] text-muted-foreground", children: [
42583
+ "Already back: ",
42584
+ already.map((line) => line.label).join(", "),
42585
+ "."
42586
+ ] }) : null,
42587
+ conflicts.length ? /* @__PURE__ */ (0, import_jsx_runtime137.jsxs)("p", { className: "text-[11px] text-muted-foreground", children: [
42588
+ conflicts.map((line) => line.label).join(", "),
42589
+ " ",
42590
+ conflicts.length === 1 ? "was" : "were",
42591
+ " edited again after this change. Keep the newer ",
42592
+ conflicts.length === 1 ? "edit" : "edits",
42593
+ ", or replace ",
42594
+ conflicts.length === 1 ? "it" : "them",
42595
+ "?"
42596
+ ] }) : /* @__PURE__ */ (0, import_jsx_runtime137.jsx)("p", { className: "text-[11px] text-muted-foreground", children: "It is saved as a new version. Nothing in the history is erased, and later edits to other fields stay." }),
42597
+ /* @__PURE__ */ (0, import_jsx_runtime137.jsxs)("div", { className: "flex flex-wrap gap-1", children: [
42598
+ conflicts.length ? /* @__PURE__ */ (0, import_jsx_runtime137.jsxs)(import_jsx_runtime137.Fragment, { children: [
42599
+ clean.length ? /* @__PURE__ */ (0, import_jsx_runtime137.jsx)(import_design_system118.Button, { size: "sm", className: "h-6 px-2 text-[11px]", onClick: () => onConfirm(false), children: "Undo the rest, keep newer edits" }) : null,
42600
+ /* @__PURE__ */ (0, import_jsx_runtime137.jsx)(
42601
+ import_design_system118.Button,
42602
+ {
42603
+ size: "sm",
42604
+ variant: clean.length ? "secondary" : "primary",
42605
+ className: "h-6 px-2 text-[11px]",
42606
+ onClick: () => onConfirm(true),
42607
+ children: "Replace newer edits too"
42608
+ }
42609
+ )
42610
+ ] }) : /* @__PURE__ */ (0, import_jsx_runtime137.jsx)(import_design_system118.Button, { size: "sm", className: "h-6 px-2 text-[11px]", disabled: nothing, onClick: () => onConfirm(false), children: "Yes, undo it" }),
42611
+ /* @__PURE__ */ (0, import_jsx_runtime137.jsx)(import_design_system118.Button, { size: "sm", variant: "ghost", className: "h-6 px-2 text-[11px]", onClick: onCancel, children: "Cancel" })
42612
+ ] })
42430
42613
  ] });
42431
42614
  }
42615
+ function blankSays(value) {
42616
+ return value === null || value === void 0 || value === "" ? "cleared" : say(value, void 0);
42617
+ }
42432
42618
  function ConfirmRestore({
42433
42619
  preview,
42434
42620
  busy,
@@ -42749,11 +42935,12 @@ function FieldHistoryPanel({
42749
42935
  const label = (fields.data ?? []).find((f) => f.key === fieldKey)?.label || (0, import_text_case20.humanizeIdentifier)(fieldKey);
42750
42936
  if (error) return /* @__PURE__ */ (0, import_jsx_runtime139.jsx)(RefusalNotice, { error, className });
42751
42937
  if (!rows || fields.loading && !fields.data || table.loading && !table.data) return /* @__PURE__ */ (0, import_jsx_runtime139.jsx)(import_design_system120.Skeleton, { className: (0, import_design_system120.cn)("h-40 w-full", className) });
42752
- async function start(row) {
42938
+ async function start(row, back = false) {
42753
42939
  setSaid(null);
42754
- setPending({ phase: "asking", recordId: row.record_id, version: row.version });
42940
+ const target = back ? row.version - 1 : row.version;
42941
+ setPending({ phase: "asking", recordId: row.record_id, version: target, back });
42755
42942
  const [asked3, seen] = await Promise.all([
42756
- client.restorePreview({ record_id: row.record_id, version: row.version, field_key: fieldKey }),
42943
+ client.restorePreview({ record_id: row.record_id, version: target, field_key: fieldKey }),
42757
42944
  (0, recordVersions_exports.readVersionNow)(client, row.record_id)
42758
42945
  ]);
42759
42946
  seenAtPreview.current = seen;
@@ -42762,17 +42949,20 @@ function FieldHistoryPanel({
42762
42949
  setError(asked3.error);
42763
42950
  return;
42764
42951
  }
42765
- setPending({ phase: "confirm", recordId: row.record_id, version: row.version, preview: asked3.data });
42952
+ const laterEdit = back && asked3.data.changes.some((c) => c.key === fieldKey && !sameValue2(c.before, row.after));
42953
+ setPending({ phase: "confirm", recordId: row.record_id, version: target, preview: asked3.data, back, laterEdit });
42766
42954
  }
42767
- async function confirm(recordOf2, version) {
42768
- setPending({ phase: "writing", recordId: recordOf2, version });
42955
+ async function confirm(recordOf2, version, back = false) {
42956
+ setPending({ phase: "writing", recordId: recordOf2, version, back });
42769
42957
  const written = await (0, recordVersions_exports.restoreAt)(client, { record_id: recordOf2, version, field_key: fieldKey, seenVersion: seenAtPreview.current });
42770
42958
  setPending({ phase: "idle" });
42771
42959
  if (!written.ok) {
42772
42960
  setError(written.error);
42773
42961
  return;
42774
42962
  }
42775
- setSaid(`Put back as version ${written.data.version}. Every earlier version is still here.`);
42963
+ setSaid(
42964
+ back ? `Change undone as version ${written.data.version}. Other fields keep their later edits.` : `Put back as version ${written.data.version}. Every earlier version is still here.`
42965
+ );
42776
42966
  await load();
42777
42967
  }
42778
42968
  return /* @__PURE__ */ (0, import_jsx_runtime139.jsxs)("section", { className: (0, import_design_system120.cn)("flex min-h-0 flex-col gap-2", className), children: [
@@ -42790,8 +42980,10 @@ function FieldHistoryPanel({
42790
42980
  label,
42791
42981
  " has never been changed on anything you can see."
42792
42982
  ] }) : /* @__PURE__ */ (0, import_jsx_runtime139.jsx)("ol", { className: "flex min-h-0 flex-col divide-y overflow-y-auto rounded border", children: rows.map((row) => {
42793
- const mine = pending.phase !== "idle" && pending.recordId === row.record_id && pending.version === row.version;
42794
- const confirming = mine && pending.phase === "confirm" ? pending : null;
42983
+ const matches2 = (back) => pending.phase !== "idle" && pending.recordId === row.record_id && Boolean(pending.back) === back && pending.version === (back ? row.version - 1 : row.version);
42984
+ const mineValue = matches2(false);
42985
+ const mineBack = matches2(true);
42986
+ const confirming = (mineValue || mineBack) && pending.phase === "confirm" ? pending : null;
42795
42987
  return /* @__PURE__ */ (0, import_jsx_runtime139.jsxs)("li", { className: "flex flex-col", children: [
42796
42988
  /* @__PURE__ */ (0, import_jsx_runtime139.jsxs)("div", { className: "flex items-baseline gap-2 px-2 py-1.5 text-xs", children: [
42797
42989
  onOpenRecord ? /* @__PURE__ */ (0, import_jsx_runtime139.jsx)(
@@ -42830,21 +43022,38 @@ function FieldHistoryPanel({
42830
43022
  size: "sm",
42831
43023
  variant: "ghost",
42832
43024
  className: "h-5 shrink-0 px-1 text-[10px]",
42833
- disabled: mine,
43025
+ disabled: mineValue,
42834
43026
  onClick: () => void start(row),
42835
43027
  children: "Put this back"
42836
43028
  }
43029
+ ) : null,
43030
+ rights.write && row.version > 1 ? /* @__PURE__ */ (0, import_jsx_runtime139.jsx)(
43031
+ import_design_system120.Button,
43032
+ {
43033
+ size: "sm",
43034
+ variant: "ghost",
43035
+ className: "h-5 shrink-0 px-1 text-[10px]",
43036
+ disabled: mineBack,
43037
+ onClick: () => void start(row, true),
43038
+ children: "Undo this change"
43039
+ }
42837
43040
  ) : null
42838
43041
  ] }),
42839
- confirming ? /* @__PURE__ */ (0, import_jsx_runtime139.jsx)("div", { className: "border-t px-2 py-2", children: /* @__PURE__ */ (0, import_jsx_runtime139.jsx)(
42840
- ConfirmRestore,
42841
- {
42842
- preview: confirming.preview,
42843
- busy: false,
42844
- onConfirm: () => void confirm(confirming.recordId, confirming.version),
42845
- onCancel: () => setPending({ phase: "idle" })
42846
- }
42847
- ) }) : null
43042
+ confirming ? /* @__PURE__ */ (0, import_jsx_runtime139.jsxs)("div", { className: "border-t px-2 py-2", children: [
43043
+ confirming.laterEdit ? /* @__PURE__ */ (0, import_jsx_runtime139.jsxs)("p", { "data-later-edit": "", className: "mb-1 text-[11px] font-medium text-amber-700 dark:text-amber-400", children: [
43044
+ label,
43045
+ " was edited again after this change. Undoing replaces that newer value; Cancel keeps it."
43046
+ ] }) : null,
43047
+ /* @__PURE__ */ (0, import_jsx_runtime139.jsx)(
43048
+ ConfirmRestore,
43049
+ {
43050
+ preview: confirming.preview,
43051
+ busy: false,
43052
+ onConfirm: () => void confirm(confirming.recordId, confirming.version, confirming.back === true),
43053
+ onCancel: () => setPending({ phase: "idle" })
43054
+ }
43055
+ )
43056
+ ] }) : null
42848
43057
  ] }, `${row.record_id}-${row.version}`);
42849
43058
  }) }),
42850
43059
  rights.write ? null : /* @__PURE__ */ (0, import_jsx_runtime139.jsx)("p", { className: "text-[11px] text-muted-foreground", children: rights.why("write") })