Skip to content

Commit

Permalink
Merge pull request #31 from aleph-im/Feat/Logout
Browse files Browse the repository at this point in the history
Fix: logout and auto-recconect using SessionStorage
  • Loading branch information
amalcaraz authored Jul 10, 2023
2 parents 57f8573 + e94228d commit 77fb8a8
Show file tree
Hide file tree
Showing 4 changed files with 39 additions and 9 deletions.
20 changes: 20 additions & 0 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@
"react-dom": "18.2.0",
"styled-components": "^5.3.6",
"typescript": "4.9.5",
"usehooks-ts": "^2.9.1",
"uuid": "^9.0.0"
},
"devDependencies": {
Expand Down
9 changes: 2 additions & 7 deletions src/components/common/Main/cmp.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -3,16 +3,11 @@ import { MainProps } from './types'
import { useConnect } from '@/hooks/common/useConnect'

export const Main = ({ children }: MainProps) => {
const { isConnected, connect } = useConnect()
const { tryReconnect } = useConnect()

useEffect(() => {
async function tryReconnect() {
if (isConnected) return
await connect()
}

tryReconnect()
}, [connect, isConnected])
}, [tryReconnect])

return <main>{children}</main>
}
Expand Down
18 changes: 16 additions & 2 deletions src/hooks/common/useConnect.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,17 +5,23 @@ import { useNotification } from '@aleph-front/aleph-core'
import { Account } from 'aleph-sdk-ts/dist/accounts/account'
import { Chain } from 'aleph-sdk-ts/dist/messages/types'
import { useCallback } from 'react'
import { useSessionStorage } from 'usehooks-ts'

export type UseConnectReturn = {
connect: () => Promise<Account | undefined>
disconnect: () => Promise<void>
isConnected: boolean
account: Account | undefined
tryReconnect: () => Promise<void>
}

export function useConnect(): UseConnectReturn {
const [state, dispatch] = useAppState()
const noti = useNotification()
const [keepAccountAlive, setKeepAccountAlive] = useSessionStorage(
'keepAccountAlive',
false,
)

const onError = useCallback(
(error: string) => {
Expand Down Expand Up @@ -46,29 +52,37 @@ export function useConnect(): UseConnectReturn {
}

if (!account) return
setKeepAccountAlive(true)

await Promise.all([getBalance(account)]).catch((err) => {
onError(err.message)
})

dispatch({ type: ActionTypes.connect, payload: { account } })
return account
}, [getBalance, dispatch, onError])
}, [setKeepAccountAlive, getBalance, dispatch, onError])

// @todo: Think if it is necessary preload all on connect
// useAccountProducts()

const disconnect = useCallback(async () => {
setKeepAccountAlive(false)
dispatch({ type: ActionTypes.disconnect, payload: null })
}, [dispatch])
}, [dispatch, setKeepAccountAlive])

const { account } = state
const isConnected = !!account?.address

const tryReconnect = useCallback(async () => {
if (isConnected || !keepAccountAlive) return
await connect()
}, [isConnected, keepAccountAlive, connect])

return {
connect,
disconnect,
isConnected,
account,
tryReconnect,
}
}

0 comments on commit 77fb8a8

Please sign in to comment.