Add navigation-aware undo/redo with onNavigate callback#21
Merged
Conversation
The boolean flag was never read by any code — onNavigate is the implemented API. Keeping it alongside onNavigate with a "supersedes" comment implied live behavior that didn't exist. https://claude.ai/code/session_01RhU9ifeoZ8Pb74HipfodF7
…shots updateState and collection updateState used getMergedData() as the base for constructing newLocalState. getMergedData() substitutes display-override Timestamps at serverTimestamp() sentinel paths; a second update() would clone those Timestamps into newLocalState, silently erasing the sentinel from state.localState. Subsequent sync() would then ship a client Timestamp to Firestore, re-introducing the C1 regression for any chained mutation. The same problem affected setData and deleteDocument: their undo restore snapshots called deepClone(getMergedData()), capturing the frozen Timestamp instead of the sentinel; undo replay would call setDoc with a client Timestamp rather than serverTimestamp. Fix: use state.localState ?? state.syncState (raw state) as the mutation base and undo snapshot source. getMergedData() is retained for display (the null-check in updateState) and the undo diff comparisons now use rawBase so sentinel-carrying diffs round-trip correctly through undo/redo. Two regression tests added to firestate.integration.test.ts to pin both paths. https://claude.ai/code/session_01RhU9ifeoZ8Pb74HipfodF7
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Summary
Adds support for navigation-aware undo/redo operations. When an undo action is tagged with a
path, the store can invoke a navigation callback before reverting the change, returning users to the route where the change occurred.Changes
setOnNavigate()method toFirestateStoreto allow runtime updates to the navigation handler without recreating the storeonNavigatecallback toFirestateConfigandFirestateProviderPropsonNavigatecallback tocreateUndoManagerso it can invoke navigation before undo/redo operationsFirestateProviderto accept and manage theonNavigatecallback, similar to existingonErrorpatternsetOnNavigateImplementation Details
The
onNavigatecallback is stored as a mutable reference in the store (likeonError) to allow the provider to update it without recreating the store. This prevents subscription loss when consumers pass inline callbacks that change reference on every render. A stable wrapper delegates to this mutable reference, so the undo manager doesn't need recreation when the callback changes.https://claude.ai/code/session_01DPzuA9BMs4erECovPMXbMW