chore: Adding missing field to store #101
Open
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.
Prerequisites checklist
What is the purpose of this pull request?
What changes did you make? (Give an overview)
Use the
onRehydrateStorage
hook to add any new field if it is missing in the store.The previous implementation - #97, where we checked for missing language code, works fine in cases where a user manually selects a new language from the dropdown. In this scenario, if the corresponding code entry was missing, we could dynamically patch it on the fly.
However, this approach breaks when a user opens a shared link that points directly to a newly added language. Since the state is rehydrated from the URL hash and doesn’t include the new language (because it wasn’t present in the original persisted state), the app can break.
We’re now using the onRehydrateStorage hook provided by Zustand. This hook ensures that immediately after the state is loaded from storage, any missing fields (like code for a new language) are automatically added using defaultCode. This centralises the patching logic inside the store (use-explorer file), ensures the state is always complete, and prevents issues with incomplete or outdated persisted data.
Also, all code logic related to state hydration and defaults stays cleanly inside the Zustand store.
Related Issues
Is there anything you'd like reviewers to focus on?