Skip to content

Commit d8b15b5

Browse files
fix(grid): a grouped row moves to its new group once its row is left, and group subtotals and the footer total follow a cell save (#35689 slice B) (#36476)
Fixes #35689 — slice B (groups and totals after a save). Slice A (touch, accessibility, checked rows) landed earlier on its own PR, so nothing else on the card is left. Clause-②: no Metadata-protocol: none ## 维护者速读 - 列表页按阶段分组时,把一条的阶段从甲改成乙:停在这一行时它不动,点到别的行后它出现在乙组,两组的条数和金额小计都对;改金额后分组小计和表尾合计马上更新。 - 列表行内编辑重做专题的最后一块(分组与合计随保存更新),这张卡至此做完。 - 出错时最坏是分组小计或表尾合计显示旧数字、或这一行要刷新后才换组,保存的数据本身不受影响;可以直接撤回。 - 无 ## What changes Measured first on `origin/main` 285ad8d, in a list that selects a cell first (the list page's grid), with the card-named pins below run against the unchanged source: | Card item | Server grouping | Browser grouping | Premise | |:---|:---|:---|:---| | A row whose stage changed moves to its new group after the user leaves the row | did not move | did not move | holds: broken | | Group counts and amount subtotals after the save | not refreshed | not refreshed | holds: broken | | Footer total, flat grid whose rows are the whole set | not refreshed | n/a | holds: broken | | Footer total, grid paged on the server (whole-set figure) | already re-read after a write | n/a | false: works on main, pinned only | Why: a select-first save redraws only its own row (the row patches of #35685) and the list lets its own write go by on the bus, so nothing is read again. The groups, the group headers' numbers and the footer's row figures read the rows as loaded, not as saved. The whole-set footer listens to the bus itself, so it already re-asked. What this PR does, all in `@object-ui/plugin-grid`: - **The figures read the saved rows.** `rowsAsSaved` (in `objectGridRowPatch.ts`) gives the rows with each saved row in place of the one read, while that read is the one held. The footer reads it, so a flat grid's total includes the saved value at once. A grid that groups the rows it was handed buckets it, so its counts and subtotals follow the save. - **A row whose save changed a grouping field is held in its group until the row is left.** `writeRowInPlace` notes such a record in `heldInGroup`. The browser bucketing reads that record as it was loaded (it stays in its group, counted and summed there). `onCellSelect` on a cell of another row lets it go: the browser bucketing then moves it, and a server-grouped grid reads its group headers and open groups' rows again (`regroupNonce`). A cell of the same row does not let it go. Any later save on a held row waits with it. - **Any other save in a server-grouped grid re-asks only the headers** (`savedFigureNonce`), so subtotals update at once and no row is read again. `objectGridGrouping.ts` now keys the header query on that nonce and the row pages on `regroupNonce` alone. - A grid without the select-first model still reads everything again after each save, as before (control pin). Docs: `content/docs/frontend/plugins/plugin-grid.mdx` gains one bullet under the per-cell save section. Changeset: `@object-ui/plugin-grid` patch. ## Tests One new top-level describe in the component's existing select-first save harness, `ObjectGrid.autosave-35685.test.tsx`: "ObjectGrid — groups, subtotals and the footer total after a cell save (objectstack#35689)". It covers both grouping paths: the server, where the grid asks its own group headers and group pages, and the browser, where it buckets rows a host hands it. - Card line 「离开这一行后它出现在乙组,两个组的条数和金额小计正确」, on each path: a stage pasted from Prospect to Won keeps the row in Prospect, with both groups at their old figures. A click on another cell of the same row keeps it there too. A click on a cell of another row moves it to Won, and the groups read Prospect 1 · 100 and Won 2 · 80. - Card text 「分组小计…随保存更新」, on each path: an amount saved is in its group's subtotal at once. No row is read again. A server-grouped grid asks its headers exactly once more, and a browser-grouped one asks nothing. - Card line 「表尾合计在改了金额后更新」: in a flat grid whose rows are the set, the footer sum goes from 180 to 200. In a grid paged on the server, the whole-set footer is read again and reads 200 (this holds on main and is pinned only). - Control: without `cellSelection`, a write on the bus re-reads the grouped grid at once. Ablations: one run per leg through `scripts/ablation-replace.mjs`. Every anchor hit x1 and went to x0, and every restore came back to the HEAD blob `f5aebf14` with `git diff HEAD` empty. The suites import the grid by relative path (source), so no dist leg was owed: | Leg | Mutation | Red | |:---|:---|:---| | A1 | `onCellSelect` no longer lets a held row go | 2: the move pin on both paths | | A2 | no header re-ask on a save | 1: server subtotal | | A3 | the grouping reads the loaded rows | 2: browser move and browser subtotal | | A4 | the footer reads the loaded rows | 1: flat footer | | A5 | a held row is bucketed as saved | 1: browser hold before leaving | <details><summary>Readings (all through scripts/pm/os-verify-lock.sh)</summary> - On `origin/main` 285ad8d before any source edit, the new describe gave 5 failed and 2 passed (the paged footer and the control). The failures: Won expected Bolt and Cord, got Cord only (both paths); the subtotal stayed 2 · 150 (both paths); the footer stayed Sum: 180. - At e96aa67 (after merging origin/main with #36332 and #36396): `pnpm turbo run build --concurrency=2 --filter='@object-ui/plugin-grid^...' --filter='@objectstack/client-react...'`: 54 successful. Then `pnpm --filter @object-ui/plugin-grid run type-check` (script echoed): exit 0. Then `pnpm --dir ui exec vitest run --maxWorkers=2 packages/plugin-grid/`: Test Files 304 passed (304), Tests 2706 passed (2706). And `pnpm --dir ui exec vitest run --maxWorkers=2 scripts/`: Test Files 172 passed, 2 skipped; Tests 5127 passed, 2 skipped; exit 0. - At 1285fe4 (the final head; it only moves the new describe above the state-machine block, so it does not collide with an open PR's append at the end of the file): the harness gave Test Files 1 passed (1), Tests 29 passed (29), VERDICT command-exit 0. `pnpm check:nul-bytes` exit 0. </details> <details><summary>Gates</summary> - `node scripts/pm/dispatch-gates.mjs --commands --repo objectstack-ai/objectstack` at e96aa67 derived 72 families (6 paths vs merge base d8628ac). All ran with exit 0. Three needed a second attempt: `check:skill-examples` first exited 3 because client-react had no dist (built, then exit 0); `check:type-check-debt` first exited 3 because the lock was busy (run under the lock, exit 0, 0 raw tsc errors); the ui `scripts/` suite first got no turn on the lock (exit 99), and its reading is above. - `--ran`: 72 derived, 72 run, 0 NOT-MEASURED, 0 UNRUN, with every exit code recorded. - `node scripts/pm/overlap-check.mjs` at 1285fe4: VERDICT clean exit 0, against origin/main 7ae5e07 and 4 open heads (#36453, #36438, #36357, #36316). </details> ## Acceptance notes - "Leaving the row" means selecting a cell of another row (a click, the arrow keys, Tab across the row's end). Escape, or a click outside the grid, keeps the row selected, so it stays in its group until another row is selected or the list loads again. - When a server-grouped grid regroups on leaving a row, it reads the open groups' rows again, so a group pager turned past page 1 starts over at page 1, as on any refresh. - A held record stays noted after a later full load. The next time a row is left, the groups are read one extra time. That read is harmless. ## Verification Files to read first, by risk: 1. `ui/packages/plugin-grid/src/ObjectGrid.tsx`: `noteSavedPlace`, `leaveHeldRows`, and the `savedData` and `groupedData` memos. 2. `ui/packages/plugin-grid/src/objectGridGrouping.ts`: the header and row reload keys. 3. `ui/packages/plugin-grid/src/objectGridRowPatch.ts`: `rowsAsSaved`. Two-minute check: `pnpm --dir ui exec vitest run packages/plugin-grid/src/__tests__/ObjectGrid.autosave-35685.test.tsx`. Expect 29 passed, including the seven under "(objectstack#35689)". --- _Generated by [Claude Code](https://claude.ai/code/session_01QsR2jUc3qp6bHKSWijbjds)_ --------- Co-authored-by: Claude <noreply@anthropic.com>
1 parent 595844b commit d8b15b5

6 files changed

Lines changed: 356 additions & 8 deletions

File tree

‎.changeset/35689-group-refresh.md‎

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
---
2+
'@object-ui/plugin-grid': patch
3+
---
4+
5+
fix(grid): after a cell save in a list that selects a cell first, a grouped row moves to its new group once the user leaves the row, and group subtotals and the footer total follow the save
6+
7+
Clause-②: no
8+
Metadata-protocol: none
9+
10+
A list that selects a cell first (the list page) saves each cell and redraws only that row. Until now the groups and the footer did not follow that save:
11+
12+
- **A changed group field.** The row stays where it is while the user is still on it. Once a cell of another row is selected, the row shows in its new group, and both groups' counts and subtotals are correct. A grid grouped on the server reads its group headers and the open groups' rows again at that moment. A grid that groups the rows it was handed puts the saved row in its new group.
13+
- **Any other field.** The group subtotals include the saved value straight away. A grid grouped on the server reads its group headers again and reads no row. A grid that groups the rows it was handed recounts them.
14+
- **The footer total.** A flat grid whose rows are the whole set totals the saved value straight away. A grid paged on the server already read its whole-set figures again after a write, and still does.
15+
16+
A grid that is not select-first still reads everything again after each save, as before.

‎content/docs/frontend/plugins/plugin-grid.mdx‎

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -463,6 +463,12 @@ value a server hook rewrote shows as stored); the list is not read again, so a
463463
row whose new value would sort it elsewhere stays in place until the next load
464464
(a refresh, a sort, a filter or a page change).
465465

466+
- **Groups and totals follow the save** (objectstack#35689). A save that
467+
changes the grouping field leaves the row in its group while the user is on
468+
it; once a cell of another row is selected, the row shows in its new group
469+
and both groups' counts and subtotals are correct (a server-grouped grid
470+
reads its group headers and open groups' rows again then). Any other save is
471+
in its group's subtotals at once, and in the footer total of a flat grid.
466472
- **A refused save** keeps the typed value and shows the server's reason on the
467473
cell. Escape puts the stored value back. A later edit on the same row is sent
468474
together with the refused one, as one update. When the reason names a field

‎ui/packages/plugin-grid/src/ObjectGrid.tsx‎

Lines changed: 60 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -105,7 +105,8 @@ import { useGridTranslation } from './objectGridTranslations';
105105
import { inlineEditRuleRefusals, inlineRuleRefusalError } from './objectGridInlineRules';
106106
import { DEFAULT_DISPLAY_PAGE_SIZE, DEFAULT_SELECTION_TYPE, type RowHeightMode, resolvePageSize, resolveRowHeightMode } from './objectGridDefaults';
107107
import { type BatchSaveOutcome, type RowConflictChoice, type WrittenRowVersion, rowVersionOf, rowVersionToSend } from './objectGridRowVersion';
108-
import { type RowPatch, mergeSavedRow } from './objectGridRowPatch';
108+
import { type RowPatch, mergeSavedRow, rowsAsSaved } from './objectGridRowPatch';
109+
import { usableGroupingFields } from './useGroupedData';
109110
import { type CellSaveUndo, createCellUndoStack, isCellUndoKey, undoKeyCellOf } from './objectGridCellUndo';
110111
import { type ObjectGridExternalPaginationProps, type ObjectGridFooterSummary, type ObjectGridRowGroup } from './objectGridProps';
111112
import { FooterSummaryReport } from './objectGridFooterSummaryReport';
@@ -874,6 +875,9 @@ function cellEditorWidthClass(type: string): string {
874875
}
875876
}
876877

878+
/** objectstack#35689 — no record waits in its group. */
879+
const NO_HELD_RECORDS: ReadonlySet<string> = new Set();
880+
877881
export const ObjectGrid: React.FC<ObjectGridComponentProps> = ({
878882
schema,
879883
dataSource,
@@ -986,6 +990,25 @@ export const ObjectGrid: React.FC<ObjectGridComponentProps> = ({
986990
// which `writeRowInPlace` sets together with the state it renders from.
987991
const cellUndoRef = React.useRef(createCellUndoStack());
988992
const rowPatchesRef = React.useRef(rowPatches);
993+
// objectstack#35689 — the records a select-first save moved to another
994+
// group while the reader is still on their row: each stays in the group it
995+
// was read in, counted and summed there, until a cell of another row is
996+
// selected. Then the groups are read again (`regroupNonce`), and a grid
997+
// that buckets in the browser buckets the saved row. Any other save is in
998+
// the figures at once (`savedFigureNonce` re-asks the server's headers).
999+
const [heldInGroup, setHeldInGroup] = useState<ReadonlySet<string>>(NO_HELD_RECORDS);
1000+
const heldInGroupRef = React.useRef(heldInGroup);
1001+
const [regroupNonce, setRegroupNonce] = useState(0);
1002+
const [savedFigureNonce, setSavedFigureNonce] = useState(0);
1003+
const leaveHeldRows = useCallback((selectedRecordId: unknown) => {
1004+
const held = heldInGroupRef.current;
1005+
const stay = selectedRecordId === undefined || selectedRecordId === null ? undefined : String(selectedRecordId);
1006+
if (held.size === 0 || (held.size === 1 && stay !== undefined && held.has(stay))) return;
1007+
const next = stay !== undefined && held.has(stay) ? new Set([stay]) : NO_HELD_RECORDS;
1008+
heldInGroupRef.current = next;
1009+
setHeldInGroup(next);
1010+
setRegroupNonce((n) => n + 1);
1011+
}, []);
9891012
const [showExport, setShowExport] = useState(false);
9901013
const [exportBusy, setExportBusy] = useState(false);
9911014
const [exportError, setExportError] = useState<string | null>(null);
@@ -1470,6 +1493,9 @@ export const ObjectGrid: React.FC<ObjectGridComponentProps> = ({
14701493
};
14711494
},
14721495
onCellSelect: (row) => {
1496+
// objectstack#35689 — a row held in its group is left. Keyed as
1497+
// the save keys it (`resolveRecordId`).
1498+
leaveHeldRows(row?._id ?? row?.id);
14731499
recordVerdict.requestVerdicts();
14741500
// objectstack#35795 — one lock read for the page, not one per row selected.
14751501
approvalLocks.probe([rowRecordId(row), ...pageRecordIds]);
@@ -1495,7 +1521,7 @@ export const ObjectGrid: React.FC<ObjectGridComponentProps> = ({
14951521
}
14961522
: null), [
14971523
hostCellSelection, opensReferencedRecords, objectFields, objectSchema, objectName, perms,
1498-
gridEditable, objectInlineEditable, approvalLocks, recordVerdict, pageRecordIds, t,
1524+
gridEditable, objectInlineEditable, approvalLocks, recordVerdict, pageRecordIds, t, leaveHeldRows,
14991525
]);
15001526
// objectstack#35685 — the state-machine confirm, the grid's next question
15011527
// when the required-fields prompt has nothing to ask.
@@ -1885,13 +1911,18 @@ export const ObjectGrid: React.FC<ObjectGridComponentProps> = ({
18851911

18861912
// The grouping: the group value formatter and date faces, the server group
18871913
// headers and rows, and the grouped data, in hook order (`./objectGridGrouping`).
1914+
// objectstack#35689 — the rows the figures read: each saved row in place of
1915+
// the row read (the footer), and, for the groups, a row still held in its
1916+
// group (`heldInGroup`) as it was read.
1917+
const savedData = useMemo(() => rowsAsSaved(data, rowPatches), [data, rowPatches]);
1918+
const groupedData = useMemo(() => rowsAsSaved(data, rowPatches, heldInGroup), [data, rowPatches, heldInGroup]);
18881919
const {
18891920
temporalGroupFaces, groupWhere, groupHeaders, groups, isGrouped, toggleGroup, groupRows,
18901921
} = useObjectGridGrouping({
1891-
schema, dataSource, objectName, objectFields, objectTypesPending, data, setData,
1922+
schema, dataSource, objectName, objectFields, objectTypesPending, data: groupedData, setData,
18921923
setTotalMatching, t, translateOptions, groupedPage, setGroupedPage, groupedPageSize, refreshKey,
18931924
serverPageSize, groupRowQuery, groupingKeyReadable, groupingRefusesNothing,
1894-
unmaskedGroupingFields, serverGroupedFetch, invalidationNonce,
1925+
unmaskedGroupingFields, serverGroupedFetch, invalidationNonce, savedFigureNonce, regroupNonce,
18951926
});
18961927
// objectstack#35685 — the rows the grid read, as an undo finds its record
18971928
// among them when it runs: the page's rows, and a server-grouped group's.
@@ -1908,7 +1939,7 @@ export const ObjectGrid: React.FC<ObjectGridComponentProps> = ({
19081939
const {
19091940
bulkFanoutParams, resolvedTotalMatching, summaries, hasSummary, summaryTypes,
19101941
} = useObjectGridSummaries({
1911-
schema, dataSource, objectName, objectSchema, objectFields, objectTypesPending, data, hasInlineData,
1942+
schema, dataSource, objectName, objectSchema, objectFields, objectTypesPending, data: savedData, hasInlineData,
19121943
externalManualPagination, hostFindParams, hostRowCount, lastFindParamsRef, totalMatching, refreshKey, serverPage,
19131944
fetchWindow, serverGroupedFetch, isGrouped, summaryPicks,
19141945
});
@@ -3100,12 +3131,36 @@ export const ObjectGrid: React.FC<ObjectGridComponentProps> = ({
31003131
next.set(id, { base: held && held.row === row ? held.base : row, row: mergeSavedRow(row, shown, answer) });
31013132
rowPatchesRef.current = next;
31023133
setRowPatches(next);
3134+
noteSavedPlace(id, Object.keys(changes));
31033135
return { outcome: 'saved', result: answer };
31043136
} finally {
31053137
releaseOwnWrite();
31063138
}
31073139
};
31083140

3141+
/**
3142+
* objectstack#35689 — where a landed select-first save of `fields` on
3143+
* record `id` leaves it in a grouped grid: a save that wrote a grouping
3144+
* field, or any save on a row already held, keeps the row in the group it
3145+
* was read in until the reader leaves the row (`leaveHeldRows`); any other
3146+
* save is in its group's figures at once, which a server-grouped grid asks
3147+
* its headers for again.
3148+
*/
3149+
const noteSavedPlace = (id: string, fields: readonly string[]): void => {
3150+
if (!isGrouped) return;
3151+
const groupingFields = usableGroupingFields(schema.grouping?.fields).map((gf) => gf.field);
3152+
const held = heldInGroupRef.current;
3153+
if (held.has(id)) return;
3154+
if (fields.some((field) => groupingFields.includes(field))) {
3155+
const next = new Set(held);
3156+
next.add(id);
3157+
heldInGroupRef.current = next;
3158+
setHeldInGroup(next);
3159+
return;
3160+
}
3161+
if (serverGroupedFetch) setSavedFigureNonce((n) => n + 1);
3162+
};
3163+
31093164
/**
31103165
* objectstack#35685 — the row an undo of record `id` is drawn over: the row
31113166
* the grid draws for it now (a later save's patch over the row it read, or

0 commit comments

Comments
 (0)