fix: Adding missing field to store#101
Merged
Merged
Conversation
✅ Deploy Preview for eslint-code-explorer ready!
To edit notification comments on pull requests, go to your Netlify site configuration. |
nzakas
reviewed
May 13, 2025
Comment on lines
-107
to
+121
| const storedValue = searchParams.get(key) ?? ""; | ||
| const storedValue = getHashParams().get(key) ?? ""; | ||
| return storedValue ? JSON.parse(atob(storedValue)) : ""; | ||
| }, | ||
| setItem: (key, newValue): void => { | ||
| const searchParams = new URLSearchParams(location.hash.slice(1)); | ||
| const searchParams = getHashParams(); | ||
| const encodedValue = btoa(JSON.stringify(newValue)); | ||
| searchParams.set(key, encodedValue); | ||
| location.hash = searchParams.toString(); | ||
| }, | ||
| removeItem: (key): void => { | ||
| const searchParams = new URLSearchParams(location.hash.slice(1)); | ||
| const searchParams = getHashParams(); |
Member
There was a problem hiding this comment.
This looks like we're creating a new URLSearchParams for each method. Is that necessary or can the same one be reused?
Member
Author
There was a problem hiding this comment.
Yes, it is necessary to create a new URLSearchParams. If we reused a single URLSearchParams instance, it would contain stale data that doesn't reflect the current state of location.hash.
nzakas
approved these changes
May 14, 2025
Member
nzakas
left a comment
There was a problem hiding this comment.
LGTM. Would like another review before merging.
Member
|
@amareshsm should we remove the dropdown change case then? I can send a PR for that? |
1 task
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.
Prerequisites checklist
What is the purpose of this pull request?
What changes did you make? (Give an overview)
Use the
onRehydrateStoragehook 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?