Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Change wallet #109

Open
wants to merge 13 commits into
base: main
Choose a base branch
from
5 changes: 1 addition & 4 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@
"@polkadot-api/descriptors": "file:.papi/descriptors",
"@polkadot-labs/hdkd": "^0.0.8",
"@polkadot-labs/hdkd-helpers": "^0.0.8",
"@polkadot-ui/react": "0.0.1-alpha.31",
"@polkadot-ui/react": "0.0.1-alpha.35",
"@polkadot-ui/utils": "0.0.3",
"@radix-ui/react-dialog": "^1.1.1",
"@radix-ui/react-dropdown-menu": "^2.1.1",
Expand All @@ -33,13 +33,10 @@
"@radix-ui/react-toggle": "^1.1.0",
"@radix-ui/react-toggle-group": "^1.1.0",
"@radix-ui/react-tooltip": "^1.1.2",
"@reactive-dot/core": "^0.10.0",
"@reactive-dot/react": "^0.10.0",
"@tanstack/react-table": "^8.20.5",
"class-variance-authority": "^0.7.0",
"clsx": "^2.1.1",
"copy-to-clipboard": "^3.3.3",
"dot-connect": "^0.6.0",
"lucide-react": "^0.437.0",
"next-themes": "^0.3.0",
"polkadot-api": "^1.2.0",
Expand Down
285 changes: 6 additions & 279 deletions pnpm-lock.yaml

Large diffs are not rendered by default.

44 changes: 17 additions & 27 deletions src/App.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -6,10 +6,6 @@ import { useLocalStorage } from 'usehooks-ts'
import { Toaster } from '@/components/ui/sonner'
import './index.css'
import { Content } from './Content'
import 'dot-connect/font.css'
import { config } from './walletConfigs'
import { ReDotProvider, ReDotChainProvider } from '@reactive-dot/react'
import { Suspense } from 'react'
import { AccountContextProvider } from './contexts/AccountsContext'
import { LocksContextProvider } from './contexts/LocksContext'
import { DelegateContextProvider } from '@/contexts/DelegatesContext'
Expand All @@ -25,29 +21,23 @@ const App = () => {
return (
<ErrorBoundary>
<ThemeProvider defaultTheme={settings?.themeMode as Theme}>
<ReDotProvider config={config}>
<ReDotChainProvider chainId="polkadot">
<Suspense fallback={<div>Loading...</div>}>
<NetworkContextProvider>
<DelegateContextProvider>
<AccountContextProvider>
<LocksContextProvider>
<TooltipProvider>
<div className="flex min-h-screen w-full flex-col bg-muted/40">
<Navigation />
<div className="flex flex-col sm:gap-4 sm:py-4 sm:pl-14">
<Header />
<Content />
</div>
</div>
</TooltipProvider>
</LocksContextProvider>
</AccountContextProvider>
</DelegateContextProvider>
</NetworkContextProvider>
</Suspense>
</ReDotChainProvider>
</ReDotProvider>
<NetworkContextProvider>
<DelegateContextProvider>
<AccountContextProvider>
<LocksContextProvider>
<TooltipProvider>
<div className="flex min-h-screen w-full flex-col bg-muted/40">
<Navigation />
<div className="flex flex-col sm:gap-4 sm:py-4 sm:pl-14">
<Header />
<Content />
</div>
</div>
</TooltipProvider>
</LocksContextProvider>
</AccountContextProvider>
</DelegateContextProvider>
</NetworkContextProvider>
</ThemeProvider>
<Toaster />
</ErrorBoundary>
Expand Down
2 changes: 1 addition & 1 deletion src/components/MyDelegations.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -86,7 +86,7 @@ export const MyDelegations = () => {
{delegationsByDelegateConvictionAmount === undefined ? (
<Skeleton className="h-[116px] rounded-xl" />
) : noDelegations ? (
<Card className="col-span-2 mb-5 bg-accent p-4">
<Card className="col-span-2 mb-5 bg-accent p-4 md:col-span-3">
wirednkod marked this conversation as resolved.
Show resolved Hide resolved
<div className="flex w-full flex-col justify-center">
<div className="flex h-full items-center justify-center">
<TreePalm className="h-12 w-12" />
Expand Down
36 changes: 17 additions & 19 deletions src/contexts/AccountsContext.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,8 @@ import React, {
useEffect,
} from 'react'
import { InjectedPolkadotAccount } from 'polkadot-api/pjs-signer'
import { useAccounts as useRedotAccounts } from '@reactive-dot/react'
import { useLocalStorage } from 'usehooks-ts'
import { SELECTED_ACCOUNT_KEY } from '@/lib/constants'

import { useAccountLocalStorage, useConnect } from '@polkadot-ui/react'

type AccountContextProps = {
children: React.ReactNode | React.ReactNode[]
Expand All @@ -19,44 +18,42 @@ export interface IAccountContext {
selectedAccount?: InjectedPolkadotAccount
accounts: InjectedPolkadotAccount[]
selectAccount: (account: InjectedPolkadotAccount | undefined) => void
setAccounts: (accounts: InjectedPolkadotAccount[]) => void
}

const AccountContext = createContext<IAccountContext | undefined>(undefined)

const AccountContextProvider = ({ children }: AccountContextProps) => {
const accounts = useRedotAccounts()
const [localStorageAccount, setLocalStorageAccount] = useAccountLocalStorage()
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

this is a weird DX that I've never seen before. I don't understand why we'd need it.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

For the wallet the localstorage has a specific key. This hook is allowing you to get/set that key without the need to know the key

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

do I really need this though, as a dev? I'm still not sure.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Not sure what you mean "as a dev"... how will you setup the localstorage?

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Look at Tien's interface for the dot-connect, there's no mention of local storage ever. If we do anything with local storage, it's on our side. This useAccountLocalStorage comes form polkadot-ui and I don't get why. I think these things should be hidden if needed.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Noted!


const { connectedAccounts } = useConnect()

const [accounts, setAccounts] =
useState<InjectedPolkadotAccount[]>(connectedAccounts)

const [selectedAccount, setSelected] = useState<
InjectedPolkadotAccount | undefined
>()
const [
localStorageAccount,
setLocalStorageAccount,
removeLocalStorageAccount,
] = useLocalStorage(SELECTED_ACCOUNT_KEY, '')

const selectAccount = useCallback(
(account: InjectedPolkadotAccount | undefined) => {
if (!account) {
removeLocalStorageAccount()
setLocalStorageAccount('')
}

if (account?.address) setLocalStorageAccount(account.address)
if (account?.address) setLocalStorageAccount(account)

setSelected(account)
},
[removeLocalStorageAccount, setLocalStorageAccount],
[setLocalStorageAccount],
)

useEffect(() => {
if (localStorageAccount) {
if (accounts?.length !== 0) {
const account = accounts.find(
(account) => account.address === localStorageAccount,
(account) => account.address === localStorageAccount?.address,
)
if (account) {
selectAccount(account)
}
} else {
selectAccount(accounts[0])
selectAccount(account?.address ? account : accounts[0])
}
}, [accounts, localStorageAccount, selectAccount])

Expand All @@ -66,6 +63,7 @@ const AccountContextProvider = ({ children }: AccountContextProps) => {
accounts,
selectedAccount,
selectAccount,
setAccounts,
}}
>
{children}
Expand Down
Loading