Skip to content

Commit

Permalink
Merge pull request #480 from cosmology-tech/fix/connected-state-not-w…
Browse files Browse the repository at this point in the history
…orking-on-initial-render

connected state not working on initial render
  • Loading branch information
pyramation authored Jun 21, 2024
2 parents 25fcec7 + 5f59b5d commit 5ce9a16
Show file tree
Hide file tree
Showing 3 changed files with 19 additions and 8 deletions.
19 changes: 15 additions & 4 deletions packages/react/src/context/useSelectedWalletContext.tsx
Original file line number Diff line number Diff line change
@@ -1,15 +1,26 @@
import { createContext, useContext, useState } from "react";
import { createContext, useContext, useEffect, useState } from "react";


export const SelectedWalletRepoContext = createContext(null)
type SelectedWalletRepoContextType = {
selectedWalletRepoName: string, selectWalletRepoName: (name: string) => void
}

export const SelectedWalletRepoContext = createContext<SelectedWalletRepoContextType>(null)

export const SelectedWalletRepoProvider = ({ children }) => {

const [selectedWalletRepo, selectWalletRepo] = useState(null)
const [selectedWalletRepoName, selectWalletRepoName] = useState<string>('')

useEffect(() => {
const selectedWalletName = localStorage.getItem('cosmos-kit@2:core//current-wallet')
if (selectedWalletName) {
selectWalletRepoName(selectedWalletName)
}
}, [])


return <SelectedWalletRepoContext.Provider value={{
selectedWalletRepo, selectWalletRepo
selectedWalletRepoName, selectWalletRepoName
}}>
{children}
</SelectedWalletRepoContext.Provider>
Expand Down
4 changes: 2 additions & 2 deletions packages/react/src/modal/components/views/WalletList.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -13,10 +13,10 @@ interface DynamicWalletListProps {
function DynamicWalletList({ wallets, onClose }: DynamicWalletListProps) {
const [isLargeScreen, setIsLargeScreen] = useState(true);

const { selectWalletRepo } = useSelectedWalletRepoContext()
const { selectWalletRepoName } = useSelectedWalletRepoContext()

const onWalletClicked = useCallback(async (wallet: ChainWalletBase) => {
selectWalletRepo(wallet)
selectWalletRepoName(wallet.walletName)
await wallet.connect(wallet.walletStatus !== 'NotExist');
if (!['Rejected', 'NotExist'].includes(wallet.walletStatus)) {
onClose();
Expand Down
4 changes: 2 additions & 2 deletions packages/react/src/modal/modal.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -81,8 +81,8 @@ export function WalletModal({
beforeConnect: { disconnect: disconnectOptions },
});

const { selectedWalletRepo } = useSelectedWalletRepoContext()
const current = walletRepo?.wallets.find(w => w.walletName === selectedWalletRepo?.walletName);
const { selectedWalletRepoName } = useSelectedWalletRepoContext()
const current = walletRepo?.wallets.find(w => w.walletName === selectedWalletRepoName);

(current?.client as any)?.setActions?.({
qrUrl: {
Expand Down

0 comments on commit 5ce9a16

Please sign in to comment.