-
Notifications
You must be signed in to change notification settings - Fork 5
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(wallet): use atomWithStorage for walletStarknetkitLatestAtom
- Loading branch information
1 parent
7116c53
commit beb5ede
Showing
1 changed file
with
42 additions
and
6 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,7 +1,43 @@ | ||
import { atom } from "jotai"; | ||
import { atomWithReset } from "jotai/utils"; | ||
import { StarknetWindowObject } from "starknetkit"; | ||
import { ARGENT_WEBWALLET_URL, CHAIN_ID, provider } from "@/constants"; | ||
import { atomWithStorage } from "jotai/utils"; | ||
import type { StarknetWindowObject } from "starknetkit"; | ||
import { connect } from "starknetkit"; | ||
|
||
export const walletStarknetkitLatestAtom = atomWithReset< | ||
StarknetWindowObject | null | undefined | ||
>(undefined); | ||
export const walletStarknetkitLatestAtom = atomWithStorage< | ||
undefined | null | StarknetWindowObject | ||
>( | ||
"walletStarknetkitLatest", | ||
undefined, | ||
{ | ||
getItem: async (key: string) => { | ||
const value = localStorage.getItem(key); | ||
const jsonWallet = | ||
value && value !== "undefined" ? JSON.parse(value) : undefined; | ||
if (!jsonWallet) return undefined; | ||
const { wallet } = await connect({ | ||
provider: provider, | ||
modalMode: "neverAsk", | ||
webWalletUrl: ARGENT_WEBWALLET_URL, | ||
argentMobileOptions: { | ||
dappName: "Starknetkit example dapp", | ||
url: window.location.hostname, | ||
chainId: CHAIN_ID, | ||
icons: [], | ||
}, | ||
}); | ||
return wallet?.isConnected ? wallet : undefined; | ||
}, | ||
setItem: async ( | ||
key: string, | ||
value: StarknetWindowObject | null | undefined, | ||
) => { | ||
localStorage.setItem(key, JSON.stringify(value)); | ||
}, | ||
removeItem: async (key: string) => { | ||
localStorage.removeItem(key); | ||
}, | ||
}, | ||
{ | ||
getOnInit: true, | ||
}, | ||
); |