Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Ej/fix persisted store #746

Merged
merged 11 commits into from
Aug 22, 2023
9 changes: 7 additions & 2 deletions src/renderer/hooks/store.js
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

based on the typescript typings, it looks like get item has to return a string, and I assume createJSONStorage then just turns it back to an object...its seems like it is working.

playing around with it, I also realize that getItem, setItem, and removeItem don't need jsdoc typings, those are encapasulated it typing of the 'storage'

Original file line number Diff line number Diff line change
Expand Up @@ -12,15 +12,20 @@ const storage = {
* @returns {string | null}
*/
getItem: key => {
return /** @type {string | null} */ (store.get(key)) || null
const item = store.get(key)
return item ? JSON.stringify(item) : null
},
/**
* @param {string} key - A string key to reference the stored item by.
* @param {string} state - The state to be persisted - stringified JSON.
* @returns {void}
*/
setItem: (key, state) => {
store.set(key, JSON.parse(state))
if (typeof state === 'string') {
store.set(key, JSON.parse(state))
} else {
store.set(key, state)
}
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

with some more testing, it seems like zustand will automatically create your object into a string (so you don't need to do this typecheck)

},
/**
* @param {string} key - A string key to reference the stored item to be removed.
Expand Down
Loading