fix: restore row status on undo and improve change detection

This commit is contained in:
Christian Vidal Wolf
2026-04-21 09:05:14 +02:00
parent 2b994e3c86
commit 77819f5d3a
+32 -4
View File
@@ -321,8 +321,23 @@ export default function App() {
const currentPending = pendingRows[articleNo]; const currentPending = pendingRows[articleNo];
const originalData = currentPending?.originalData ?? appState.data[rowIndex]; const originalData = currentPending?.originalData ?? appState.data[rowIndex];
// Check if the new state is actually different from the original (not the current) const isRowDifferent = (rowA: any[], rowB: any[]) => {
const isActuallyModified = JSON.stringify(updatedRow) !== JSON.stringify(originalData); if (!rowA || !rowB) return rowA !== rowB;
const length = Math.max(rowA.length, rowB.length);
for (let i = 0; i < length; i++) {
const a = rowA[i];
const b = rowB[i];
if (a === b) continue;
const normA = (a === null || a === undefined || a === '' || a === false) ? null : a;
const normB = (b === null || b === undefined || b === '' || b === false) ? null : b;
if (normA === normB) continue;
if (String(a) === String(b)) continue;
return true;
}
return false;
};
const isActuallyModified = isRowDifferent(updatedRow, originalData);
if (isActuallyModified) { if (isActuallyModified) {
setAppState(prev => { setAppState(prev => {
@@ -355,7 +370,11 @@ export default function App() {
setAppState(prev => { setAppState(prev => {
const newData = [...prev.data]; const newData = [...prev.data];
newData[rowIndex] = updatedRow; newData[rowIndex] = updatedRow;
const stillHasChanges = Object.keys(pendingRows).filter(k => k !== articleNo).length > 0;
// Use a more reliable way to check if there are still other pending rows
const otherPendingCount = Object.keys(pendingRows).filter(k => k !== articleNo).length;
const stillHasChanges = otherPendingCount > 0;
return { ...prev, data: newData, hasUnsavedChanges: stillHasChanges }; return { ...prev, data: newData, hasUnsavedChanges: stillHasChanges };
}); });
} }
@@ -435,6 +454,8 @@ export default function App() {
setUndoHistory(prev => { setUndoHistory(prev => {
const newState = { const newState = {
data: JSON.parse(JSON.stringify(appState.data)), // Deep copy data: JSON.parse(JSON.stringify(appState.data)), // Deep copy
rowStatuses: { ...rowStatuses },
pendingRows: { ...pendingRows },
message message
}; };
// Keep last 50 steps // Keep last 50 steps
@@ -447,11 +468,18 @@ export default function App() {
if (undoHistory.length === 0) return; if (undoHistory.length === 0) return;
const [lastAction, ...remainingHistory] = undoHistory; const [lastAction, ...remainingHistory] = undoHistory;
// Restore data
setAppState(prev => ({ setAppState(prev => ({
...prev, ...prev,
data: lastAction.data, data: lastAction.data,
hasUnsavedChanges: true hasUnsavedChanges: Object.keys(lastAction.pendingRows || {}).length > 0
})); }));
// Restore statuses and pending state
if (lastAction.rowStatuses) setRowStatuses(lastAction.rowStatuses);
if (lastAction.pendingRows) setPendingRows(lastAction.pendingRows);
setUndoHistory(remainingHistory); setUndoHistory(remainingHistory);
}; };