-
Notifications
You must be signed in to change notification settings - Fork 363
Enhancement: cache owned Safes in localStorage #3066
Conversation
CLA Assistant Lite All Contributors have signed the CLA. |
ESLint Summary View Full Report
Report generated by eslint-plus-action |
Pull Request Test Coverage Report for Build 1526576110
💛 - Coveralls |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Looking good! I think there is some duplicate logic of what we already have though.
src/utils/storage/__tests__/local.ts
Outdated
@@ -0,0 +1,31 @@ | |||
import { getItem, setItem } from '../local' |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I'm surprised this passes. Normally I have to mock the localStorage
.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Somehow it's already mocked.
src/utils/storage/local.ts
Outdated
@@ -0,0 +1,20 @@ | |||
const PREFIX = 'SAFE__' | |||
|
|||
const prefixKey = (key: string): string => `${PREFIX}${key}` |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
We already have a function for getting the prefix?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Can you point me to it?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
getStoragePrefix()
at src/utils/storage/index.ts
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
That one is for immortal.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Do we want to follow a versioning convention though, just in case we make any drastic changes?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
We'll have to live with two keys for now: the legacy immortal ones and the new SAFE
ones.
The first ones, we'll need to eventually migrate (not a high prio I think).
As for versioning, We can always slap a version on top of the new key, e.g. SAFE_v2
. Once there's a need.
src/utils/storage/local.ts
Outdated
|
||
const prefixKey = (key: string): string => `${PREFIX}${key}` | ||
|
||
export const getItem = <T>(key: string): T | undefined => { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Why are you not using the functions we already have for storage interaction?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Which ones?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
loadFromStorage()
and saveToStorage()
at src/utils/storage/index.ts
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I created some for the sessionStorage
at src/utils/storage/session.ts
that don't rely on ImmortalDB. They might be of help.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I'll merge session and local, they share 99% of the code.
a55e877
to
4270e44
Compare
Deployment links
|
E2E Tests Failed Failed tests:
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Looking good. Just a few things
@@ -43,7 +43,7 @@ const isNetworkId = (id: unknown): id is ETHEREUM_NETWORK => { | |||
return Object.values(ETHEREUM_NETWORK).some((network) => network === id) | |||
} | |||
|
|||
export const NETWORK_ID_KEY = 'SAFE__networkId' | |||
export const NETWORK_ID_KEY = 'networkId' |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
In the chains migration, I am trying to move all networkId
references to chainId
. Do you think it's worthwhile including this here too?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Out of scope, I'm just removing the prefix here.
@@ -30,9 +30,6 @@ enum ErrorCodes { | |||
_701 = '701: Failed to save a localStorage item', | |||
_702 = '702: Failed to remove a localStorage item', | |||
_703 = '703: Error migrating localStorage', | |||
_704 = '704: Failed to load a sessionStorage item', |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Why have you removed these? The /load
and /open
routes use the sessionStorage
to retrieve the current chainId
.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I know but there's little value in having two sets of error messages depending on which storage this is. Besides, we're not even tracking them. I can change localStorage
-> storage
.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Alright, I would suggest generalising 701-703 then.
src/utils/storage/Storage.ts
Outdated
} | ||
} | ||
|
||
return data as unknown as T | undefined |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Why not just return on line 32/35? You won't need to cast to both types, only line 32 to T
.
// We need this to update on run time depending on selected network name | ||
export const getStoragePrefix = (networkName = getNetworkName()): string => `v2_${networkName}` | ||
// We need this to update the key in runtime depending on selected network name | ||
export const getStoragePrefix = (networkName = getNetworkName()): string => `_immortal|v2_${networkName}__` | ||
|
||
export const loadFromStorage = async <T = unknown>( |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Need this still be async?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The code that uses it still expects a promise.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Do you not think it's worthwhile changing it? Or create a new issue to do that?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Yes, we should eventually un-async all that code. 👍
Created #3073
import { useState, useEffect } from 'react' | ||
import local from './local' | ||
|
||
const useStoredState = <T>(key: string): [T | undefined, React.Dispatch<React.SetStateAction<T>>] => { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Maybe this should be name something like useLocalState
just to be clear that it isn't involving the sessionStorage
.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
"Local" state, to me, doesn't imply that it's stored in a permanent place. I would keep this name.
Looks good to me. Checked: |
@francovenica thanks! They are stored under |
What it solves
Owned safes on other networks are gone when you refresh the page.
How this PR fixes it
Keeps them in localStorage.
I've added a wrapper around localStorage that does that. Plus a hook.
How to test it