Skip to content

Commit

Permalink
Disabled when undo stack is empty
Browse files Browse the repository at this point in the history
  • Loading branch information
leegee committed Oct 17, 2024
1 parent 0382c20 commit 1f20ea3
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 11 deletions.
4 changes: 2 additions & 2 deletions src/components/UndoButton.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -2,14 +2,14 @@ import React from 'react';
import useMusicStore from '../store';

const UndoButton: React.FC = () => {
const { undo } = useMusicStore();
const { undo, undoStack } = useMusicStore();

const handleUndo = () => {
undo();
};

return (
<button onClick={handleUndo} >
<button onClick={handleUndo} disabled={!undoStack.length}>
Undo
</button>
);
Expand Down
25 changes: 16 additions & 9 deletions src/store.ts
Original file line number Diff line number Diff line change
Expand Up @@ -256,17 +256,24 @@ function pushToUndoStack() {
);

useMusicStore.setState((state) => {
const newUndoStack = [
...state.undoStack.slice(-MAX_HISTORY_ENTRIES + 1),
JSON.parse(JSON.stringify(stateToStore)),
];
const isIdentical = state.undoStack.length > 0 &&
JSON.stringify(state.undoStack[state.undoStack.length - 1]) === JSON.stringify(stateToStore);

// Log the new state after setState
console.log("Updated undo stack:", newUndoStack);
if (!isIdentical) {
const newUndoStack = [
...state.undoStack.slice(-MAX_HISTORY_ENTRIES + 1),
JSON.parse(JSON.stringify(stateToStore)),
];

return {
undoStack: newUndoStack,
};
console.log("Updated undo stack:", newUndoStack);

return {
undoStack: newUndoStack,
};
}

// No change
return state;
});
}

Expand Down

0 comments on commit 1f20ea3

Please sign in to comment.