From 77819f5d3add4a60b111c851468be3b790d67cd1 Mon Sep 17 00:00:00 2001 From: Christian Vidal Wolf Date: Tue, 21 Apr 2026 09:05:14 +0200 Subject: [PATCH] fix: restore row status on undo and improve change detection --- src/App.tsx | 36 ++++++++++++++++++++++++++++++++---- 1 file changed, 32 insertions(+), 4 deletions(-) diff --git a/src/App.tsx b/src/App.tsx index 62c11e4..fd9addd 100644 --- a/src/App.tsx +++ b/src/App.tsx @@ -321,8 +321,23 @@ export default function App() { const currentPending = pendingRows[articleNo]; const originalData = currentPending?.originalData ?? appState.data[rowIndex]; - // Check if the new state is actually different from the original (not the current) - const isActuallyModified = JSON.stringify(updatedRow) !== JSON.stringify(originalData); + const isRowDifferent = (rowA: any[], rowB: any[]) => { + 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) { setAppState(prev => { @@ -355,7 +370,11 @@ export default function App() { setAppState(prev => { const newData = [...prev.data]; 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 }; }); } @@ -435,6 +454,8 @@ export default function App() { setUndoHistory(prev => { const newState = { data: JSON.parse(JSON.stringify(appState.data)), // Deep copy + rowStatuses: { ...rowStatuses }, + pendingRows: { ...pendingRows }, message }; // Keep last 50 steps @@ -447,11 +468,18 @@ export default function App() { if (undoHistory.length === 0) return; const [lastAction, ...remainingHistory] = undoHistory; + + // Restore data setAppState(prev => ({ ...prev, 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); };