Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 16 additions & 0 deletions .changeset/35689-group-refresh.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
---
'@object-ui/plugin-grid': patch
---

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

Clause-②: no
Metadata-protocol: none

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:

- **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.
- **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.
- **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.

A grid that is not select-first still reads everything again after each save, as before.
6 changes: 6 additions & 0 deletions content/docs/frontend/plugins/plugin-grid.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -463,6 +463,12 @@ value a server hook rewrote shows as stored); the list is not read again, so a
row whose new value would sort it elsewhere stays in place until the next load
(a refresh, a sort, a filter or a page change).

- **Groups and totals follow the save** (objectstack#35689). A save that
changes the grouping field leaves the row in its group while the user is 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 server-grouped grid
reads its group headers and open groups' rows again then). Any other save is
in its group's subtotals at once, and in the footer total of a flat grid.
- **A refused save** keeps the typed value and shows the server's reason on the
cell. Escape puts the stored value back. A later edit on the same row is sent
together with the refused one, as one update. When the reason names a field
Expand Down
65 changes: 60 additions & 5 deletions ui/packages/plugin-grid/src/ObjectGrid.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -103,7 +103,8 @@ import { useGridTranslation } from './objectGridTranslations';
import { inlineEditRuleRefusals, inlineRuleRefusalError } from './objectGridInlineRules';
import { DEFAULT_DISPLAY_PAGE_SIZE, DEFAULT_SELECTION_TYPE, type RowHeightMode, resolvePageSize, resolveRowHeightMode } from './objectGridDefaults';
import { type BatchSaveOutcome, type RowConflictChoice, type WrittenRowVersion, rowVersionOf, rowVersionToSend } from './objectGridRowVersion';
import { type RowPatch, mergeSavedRow } from './objectGridRowPatch';
import { type RowPatch, mergeSavedRow, rowsAsSaved } from './objectGridRowPatch';
import { usableGroupingFields } from './useGroupedData';
import { type CellSaveUndo, createCellUndoStack, isCellUndoKey, undoKeyCellOf } from './objectGridCellUndo';
import { type ObjectGridExternalPaginationProps, type ObjectGridFooterSummary, type ObjectGridRowGroup } from './objectGridProps';
import { FooterSummaryReport } from './objectGridFooterSummaryReport';
Expand Down Expand Up @@ -852,6 +853,9 @@ function cellEditorWidthClass(type: string): string {
}
}

/** objectstack#35689 — no record waits in its group. */
const NO_HELD_RECORDS: ReadonlySet<string> = new Set();

export const ObjectGrid: React.FC<ObjectGridComponentProps> = ({
schema,
dataSource,
Expand Down Expand Up @@ -959,6 +963,25 @@ export const ObjectGrid: React.FC<ObjectGridComponentProps> = ({
// which `writeRowInPlace` sets together with the state it renders from.
const cellUndoRef = React.useRef(createCellUndoStack());
const rowPatchesRef = React.useRef(rowPatches);
// objectstack#35689 — the records a select-first save moved to another
// group while the reader is still on their row: each stays in the group it
// was read in, counted and summed there, until a cell of another row is
// selected. Then the groups are read again (`regroupNonce`), and a grid
// that buckets in the browser buckets the saved row. Any other save is in
// the figures at once (`savedFigureNonce` re-asks the server's headers).
const [heldInGroup, setHeldInGroup] = useState<ReadonlySet<string>>(NO_HELD_RECORDS);
const heldInGroupRef = React.useRef(heldInGroup);
const [regroupNonce, setRegroupNonce] = useState(0);
const [savedFigureNonce, setSavedFigureNonce] = useState(0);
const leaveHeldRows = useCallback((selectedRecordId: unknown) => {
const held = heldInGroupRef.current;
const stay = selectedRecordId === undefined || selectedRecordId === null ? undefined : String(selectedRecordId);
if (held.size === 0 || (held.size === 1 && stay !== undefined && held.has(stay))) return;
const next = stay !== undefined && held.has(stay) ? new Set([stay]) : NO_HELD_RECORDS;
heldInGroupRef.current = next;
setHeldInGroup(next);
setRegroupNonce((n) => n + 1);
}, []);
const [showExport, setShowExport] = useState(false);
const [exportBusy, setExportBusy] = useState(false);
const [exportError, setExportError] = useState<string | null>(null);
Expand Down Expand Up @@ -1443,6 +1466,9 @@ export const ObjectGrid: React.FC<ObjectGridComponentProps> = ({
};
},
onCellSelect: (row) => {
// objectstack#35689 — a row held in its group is left. Keyed as
// the save keys it (`resolveRecordId`).
leaveHeldRows(row?._id ?? row?.id);
recordVerdict.requestVerdicts();
// objectstack#35795 — one lock read for the page, not one per row selected.
approvalLocks.probe([rowRecordId(row), ...pageRecordIds]);
Expand All @@ -1468,7 +1494,7 @@ export const ObjectGrid: React.FC<ObjectGridComponentProps> = ({
}
: null), [
hostCellSelection, opensReferencedRecords, objectFields, objectSchema, objectName, perms,
gridEditable, objectInlineEditable, approvalLocks, recordVerdict, pageRecordIds, t,
gridEditable, objectInlineEditable, approvalLocks, recordVerdict, pageRecordIds, t, leaveHeldRows,
]);
// objectstack#35685 — the state-machine confirm, the grid's next question
// when the required-fields prompt has nothing to ask.
Expand Down Expand Up @@ -1846,13 +1872,18 @@ export const ObjectGrid: React.FC<ObjectGridComponentProps> = ({

// The grouping: the group value formatter and date faces, the server group
// headers and rows, and the grouped data, in hook order (`./objectGridGrouping`).
// objectstack#35689 — the rows the figures read: each saved row in place of
// the row read (the footer), and, for the groups, a row still held in its
// group (`heldInGroup`) as it was read.
const savedData = useMemo(() => rowsAsSaved(data, rowPatches), [data, rowPatches]);
const groupedData = useMemo(() => rowsAsSaved(data, rowPatches, heldInGroup), [data, rowPatches, heldInGroup]);
const {
temporalGroupFaces, groupWhere, groupHeaders, groups, isGrouped, toggleGroup, groupRows,
} = useObjectGridGrouping({
schema, dataSource, objectName, objectFields, objectTypesPending, data, setData,
schema, dataSource, objectName, objectFields, objectTypesPending, data: groupedData, setData,
setTotalMatching, t, translateOptions, groupedPage, setGroupedPage, groupedPageSize, refreshKey,
serverPageSize, groupRowQuery, groupingKeyReadable, groupingRefusesNothing,
unmaskedGroupingFields, serverGroupedFetch, invalidationNonce,
unmaskedGroupingFields, serverGroupedFetch, invalidationNonce, savedFigureNonce, regroupNonce,
});
// objectstack#35685 — the rows the grid read, as an undo finds its record
// among them when it runs: the page's rows, and a server-grouped group's.
Expand All @@ -1869,7 +1900,7 @@ export const ObjectGrid: React.FC<ObjectGridComponentProps> = ({
const {
bulkFanoutParams, resolvedTotalMatching, summaries, hasSummary, summaryTypes,
} = useObjectGridSummaries({
schema, dataSource, objectName, objectSchema, objectFields, objectTypesPending, data, hasInlineData,
schema, dataSource, objectName, objectSchema, objectFields, objectTypesPending, data: savedData, hasInlineData,
externalManualPagination, hostFindParams, hostRowCount, lastFindParamsRef, totalMatching, refreshKey, serverPage,
fetchWindow, serverGroupedFetch, isGrouped, summaryPicks,
});
Expand Down Expand Up @@ -3053,12 +3084,36 @@ export const ObjectGrid: React.FC<ObjectGridComponentProps> = ({
next.set(id, { base: held && held.row === row ? held.base : row, row: mergeSavedRow(row, shown, answer) });
rowPatchesRef.current = next;
setRowPatches(next);
noteSavedPlace(id, Object.keys(changes));
return { outcome: 'saved', result: answer };
} finally {
releaseOwnWrite();
}
};

/**
* objectstack#35689 — where a landed select-first save of `fields` on
* record `id` leaves it in a grouped grid: a save that wrote a grouping
* field, or any save on a row already held, keeps the row in the group it
* was read in until the reader leaves the row (`leaveHeldRows`); any other
* save is in its group's figures at once, which a server-grouped grid asks
* its headers for again.
*/
const noteSavedPlace = (id: string, fields: readonly string[]): void => {
if (!isGrouped) return;
const groupingFields = usableGroupingFields(schema.grouping?.fields).map((gf) => gf.field);
const held = heldInGroupRef.current;
if (held.has(id)) return;
if (fields.some((field) => groupingFields.includes(field))) {
const next = new Set(held);
next.add(id);
heldInGroupRef.current = next;
setHeldInGroup(next);
return;
}
if (serverGroupedFetch) setSavedFigureNonce((n) => n + 1);
};

/**
* objectstack#35685 — the row an undo of record `id` is drawn over: the row
* the grid draws for it now (a later save's patch over the row it read, or
Expand Down
Loading
Loading