@ai-matrx/records 0.81.0 → 0.81.1

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,12 @@
1
1
  # Changelog — @ai-matrx/records
2
2
 
3
+ ## 0.81.1
4
+ ### lane SORT-REACTS
5
+
6
+ - A live page no longer patches a changed row in place when the change decides its place: `useRecordPage` / `useRecords` read the page again when a realtime notice or this page's own write changed a value the page is sorted by (the group key included), or any value on a filtered or searched page. A date edited to the top now moves to the top, and a row edited out of the filter leaves the page, without a reload. An edit that places nothing still patches in place and reads no page (`react/pageOrder.ts`).
7
+
8
+ **Consumer action:** none.
9
+
3
10
  ## 0.81.0
4
11
  ### SSR-ROWS: a page's first answers, asked on the server, drawn in the server's HTML
5
12
 
@@ -8175,6 +8175,31 @@ function useHoldKey() {
8175
8175
  return (0, import_react3.useContext)(HoldContext);
8176
8176
  }
8177
8177
 
8178
+ // src/react/pageOrder.ts
8179
+ function pageOrderOf(sort, narrowed) {
8180
+ return { sortFields: (sort ?? []).map((key) => key.field), narrowed };
8181
+ }
8182
+ var same = (a, b) => {
8183
+ if (a === b) return true;
8184
+ if ((a === null || a === void 0 || a === "") && (b === null || b === void 0 || b === "")) return true;
8185
+ return JSON.stringify(a) === JSON.stringify(b);
8186
+ };
8187
+ function patchMovesRows(held, fresh, order) {
8188
+ if (order.sortFields.length === 0 && !order.narrowed) return false;
8189
+ for (const row of held) {
8190
+ const next = fresh.get(row.id);
8191
+ if (!next) continue;
8192
+ const was = row.document ?? {};
8193
+ const now = next.document ?? {};
8194
+ if (order.narrowed) {
8195
+ if (!same(was, now)) return true;
8196
+ continue;
8197
+ }
8198
+ for (const field2 of order.sortFields) if (!same(was[field2], now[field2])) return true;
8199
+ }
8200
+ return false;
8201
+ }
8202
+
8178
8203
  // src/react/hooks.ts
8179
8204
  function useAsync(run, key, live) {
8180
8205
  const client = useRecordsClient();
@@ -8358,7 +8383,7 @@ function useReverseLinks(tableId, columns, recordIds, limit = 3, offset = 0) {
8358
8383
  tableId ? `reverse-links:${tableId}:${columnsKey}:${limit}:${offset}:${idsKey}:${writes}` : "reverse-links:none"
8359
8384
  );
8360
8385
  }
8361
- function liveRowsFor(tableId) {
8386
+ function liveRowsFor(tableId, order = { sortFields: [], narrowed: false }) {
8362
8387
  return (entry) => {
8363
8388
  const client = entry.client;
8364
8389
  const realtime = client.config.realtime;
@@ -8377,6 +8402,10 @@ function liveRowsFor(tableId) {
8377
8402
  entry.refetch();
8378
8403
  return;
8379
8404
  }
8405
+ if (patchMovesRows(held, fresh, order)) {
8406
+ entry.refetch();
8407
+ return;
8408
+ }
8380
8409
  entry.setData(
8381
8410
  (current) => current === null ? current : {
8382
8411
  ...current,
@@ -8484,7 +8513,7 @@ function useRecords(tableId, options = {}) {
8484
8513
  // on every render must not re-read the page on every render, and a parent
8485
8514
  // that changes the question must re-read on the first one.
8486
8515
  `records:${tableId ?? "none"}:${pageSize}:${page2}:${filterKey}:s${shape}`,
8487
- liveRowsFor(tableId)
8516
+ liveRowsFor(tableId, pageOrderOf(void 0, filterKey !== "{}"))
8488
8517
  );
8489
8518
  const { live, liveReason } = liveStatus(useRecordsClient(), tableId);
8490
8519
  return { ...useSharedRows(useKeptPage(tableId, base)), live, liveReason };
@@ -8512,7 +8541,7 @@ function useRecordPage(tableId, options = {}) {
8512
8541
  return answered.ok ? { ok: true, data: { rows: answered.data.rows, total: answered.data.total, sortIgnored: answered.data.sort_ignored ?? [] } } : answered;
8513
8542
  },
8514
8543
  `page:${tableId ?? "none"}:${pageSize}:${page2}:${filterKey}:${search ?? ""}:${sortKey}:${viewId ?? ""}:s${shape}`,
8515
- liveRowsFor(tableId)
8544
+ liveRowsFor(tableId, pageOrderOf(options.sort, filterKey !== "{}" || search !== null))
8516
8545
  );
8517
8546
  const { live, liveReason } = liveStatus(useRecordsClient(), tableId);
8518
8547
  return { ...useSharedRows(useKeptPage(tableId, base)), live, liveReason };