Skip to content

Commit

Permalink
feat(wallet): use atomWithStorage for walletStarknetkitLatestAtom
Browse files Browse the repository at this point in the history
  • Loading branch information
Ruben Abarca authored and espaciofuturoio committed Jan 24, 2025
1 parent 7116c53 commit beb5ede
Showing 1 changed file with 42 additions and 6 deletions.
48 changes: 42 additions & 6 deletions state/connectedWallet.ts
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,
},
);

0 comments on commit beb5ede

Please sign in to comment.