-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add support for setting the storage dynamically (#8)
* add support for setting the storage dynamically
- Loading branch information
1 parent
327406c
commit 7bda9b5
Showing
9 changed files
with
125 additions
and
45 deletions.
There are no files selected for viewing
This file contains 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
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,52 +1,52 @@ | ||
import { LoginMethodsEnum } from 'types/enums.types'; | ||
import { store } from '../../store'; | ||
import { TokenLoginType } from 'types/login.types'; | ||
import { | ||
LedgerLoginType, | ||
LoginInfoType, | ||
WalletConnectLoginType | ||
} from 'store/slices/loginInfo/loginInfo.types'; | ||
import { getStore } from 'store/store'; | ||
|
||
export const setLoginMethod = (loginMethod: LoginMethodsEnum) => | ||
store.setState(({ loginInfo: state }) => { | ||
getStore().setState(({ loginInfo: state }) => { | ||
state.loginMethod = loginMethod; | ||
}); | ||
|
||
export const setTokenLogin = (tokenLogin: TokenLoginType) => | ||
store.setState(({ loginInfo: state }) => { | ||
getStore().setState(({ loginInfo: state }) => { | ||
state.tokenLogin = tokenLogin; | ||
}); | ||
|
||
export const setTokenLoginSignature = (signature: string) => | ||
store.setState(({ loginInfo: state }) => { | ||
getStore().setState(({ loginInfo: state }) => { | ||
if (state?.tokenLogin != null) { | ||
state.tokenLogin.signature = signature; | ||
} | ||
}); | ||
|
||
export const setWalletLogin = (walletLogin: LoginInfoType | null) => | ||
store.setState(({ loginInfo: state }) => { | ||
getStore().setState(({ loginInfo: state }) => { | ||
state.walletLogin = walletLogin; | ||
}); | ||
|
||
export const setWalletConnectLogin = ( | ||
walletConnectLogin: WalletConnectLoginType | null | ||
) => | ||
store.setState(({ loginInfo: state }) => { | ||
getStore().setState(({ loginInfo: state }) => { | ||
state.walletConnectLogin = walletConnectLogin; | ||
}); | ||
|
||
export const setLedgerLogin = (ledgerLogin: LedgerLoginType | null) => | ||
store.setState(({ loginInfo: state }) => { | ||
getStore().setState(({ loginInfo: state }) => { | ||
state.ledgerLogin = ledgerLogin; | ||
}); | ||
|
||
export const setLogoutRoute = (logoutRoute: string | undefined) => | ||
store.setState(({ loginInfo: state }) => { | ||
getStore().setState(({ loginInfo: state }) => { | ||
state.logoutRoute = logoutRoute; | ||
}); | ||
|
||
export const setIsWalletConnectV2Initialized = (isInitialized: boolean) => | ||
store.setState(({ loginInfo: state }) => { | ||
getStore().setState(({ loginInfo: state }) => { | ||
state.isWalletConnectV2Initialized = isInitialized; | ||
}); |
This file contains 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
This file contains 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
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,10 +1,11 @@ | ||
import { StoreType } from 'store/store.types'; | ||
import { useStore } from '../../store'; | ||
import { getStoreHook } from '../../store'; | ||
|
||
type ExtractState<S> = S extends { getState: () => infer X } ? X : StoreType; | ||
type ExtractState<S> = S extends { getState: () => infer T } ? T : StoreType; | ||
|
||
export const useSelector = <T>( | ||
export function useSelector<T>( | ||
selector: (state: ExtractState<StoreType>) => T | ||
) => { | ||
) { | ||
const useStore = getStoreHook(); | ||
return useStore(selector); | ||
}; | ||
} |
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,32 @@ | ||
interface InMemoryStorageType { | ||
[key: string]: string; | ||
} | ||
|
||
export class InMemoryStorage { | ||
private storage: InMemoryStorageType = {}; | ||
|
||
setItem(key: string, value: string) { | ||
this.storage[key] = value; | ||
} | ||
|
||
getItem(key: string): string | null { | ||
return this.storage.hasOwnProperty(key) ? this.storage[key] : null; | ||
} | ||
|
||
removeItem(key: string) { | ||
delete this.storage[key]; | ||
} | ||
|
||
clear() { | ||
this.storage = {} as Storage; | ||
} | ||
|
||
get length() { | ||
return Object.keys(this.storage).length; | ||
} | ||
|
||
key(index: number): string | null { | ||
const keys = Object.keys(this.storage); | ||
return keys[index] || null; | ||
} | ||
} |
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,2 @@ | ||
export * from "./storageCallback"; | ||
export * from "./inMemoryStorage"; |
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
import { StateStorage } from 'zustand/middleware'; | ||
|
||
export type StorageCallback = () => StateStorage; | ||
|
||
export const defaultStorageCallback: StorageCallback = () => localStorage; |
This file contains 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