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

Fix changing account bugs #69

Merged
merged 15 commits into from
Aug 30, 2024
2 changes: 1 addition & 1 deletion src/components/LocksCard.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ export const LocksCard = () => {
const [isUnlockingLoading, setIsUnlockingLoading] = useState(false)

useEffect(() => {
if (!currentBlock || !locks.length) return
if (!currentBlock) return

const tempOngoingLocks: VoteLock[] = []
const tempFree: VoteLock[] = []
Expand Down
14 changes: 9 additions & 5 deletions src/contexts/AccountsContext.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -28,17 +28,21 @@ const AccountContextProvider = ({ children }: AccountContextProps) => {
const [selectedAccount, setSelected] = useState<
InjectedPolkadotAccount | undefined
>()
const [localStorageAccount, setLocalStorageAccount] = useLocalStorage(
SELECTED_ACCOUNT_KEY,
'',
)
const [
localStorageAccount,
setLocalStorageAccount,
removeLocalStorageAccount,
] = useLocalStorage(SELECTED_ACCOUNT_KEY, '')

const selectAccount = useCallback(
(account: InjectedPolkadotAccount | undefined) => {
if (!account) {
removeLocalStorageAccount()
}
account?.address && setLocalStorageAccount(account.address)
setSelected(account)
},
[setLocalStorageAccount],
[removeLocalStorageAccount, setLocalStorageAccount],
)

useEffect(() => {
Expand Down
26 changes: 15 additions & 11 deletions src/contexts/LocksContext.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,10 @@ const LocksContextProvider = ({ children }: LocksContextProps) => {

// retrieve the tracks with locks for the selected account
useEffect(() => {
if (!selectedAccount || !api) return
if (!selectedAccount || !api) {
setLockTracks([])
return
}

const sub = api.query.ConvictionVoting.ClassLocksFor.watchValue(
selectedAccount.address,
Expand All @@ -78,7 +81,10 @@ const LocksContextProvider = ({ children }: LocksContextProps) => {
// retrieve all the votes for the selected account
// they can be directly casted or delegated
useEffect(() => {
if (!selectedAccount || !api || !lockTracks.length) return
if (!selectedAccount || !api || !lockTracks.length) {
setCurrentVoteLocks([])
return
}

api.query.ConvictionVoting.VotingFor.getEntries(
selectedAccount.address,
Expand All @@ -93,7 +99,7 @@ const LocksContextProvider = ({ children }: LocksContextProps) => {

// get the ref for which we have a vote casted directly
const refsVotedOn = useMemo(() => {
if (!selectedAccount || !currentVoteLocks.length) return
if (!selectedAccount || !currentVoteLocks.length) return {}

const res: Record<
number,
Expand Down Expand Up @@ -128,9 +134,10 @@ const LocksContextProvider = ({ children }: LocksContextProps) => {
!api ||
!refsVotedOn ||
!Object.entries(refsVotedOn).length
)
) {
setRefRecap({})
return

}
const refParams = Object.keys(refsVotedOn).map((id) => [Number(id)]) as [
number,
][]
Expand All @@ -153,7 +160,8 @@ const LocksContextProvider = ({ children }: LocksContextProps) => {
}, [api, refsVotedOn, selectedAccount])

const getLocks = useCallback(async () => {
if (!api || !refRecap) return
if (!api || !Object.entries(refRecap).length) return []

const locks: VoteLock[] = []
const lockTimes = await getLockTimes(api)
const blockTimeMs = await getExpectedBlockTimeMs(api)
Expand Down Expand Up @@ -263,11 +271,7 @@ const LocksContextProvider = ({ children }: LocksContextProps) => {
}, [api, refRecap])

useEffect(() => {
getLocks()
.then((l) => {
!!l && setLocks(l)
})
.catch(console.error)
getLocks().then(setLocks).catch(console.error)
}, [getLocks])

return (
Expand Down