Cleanup context

This commit is contained in:
Ilja 2022-02-04 12:01:08 +02:00
parent 1f71001175
commit 81bddf0c5d
4 changed files with 34 additions and 20 deletions

View File

@ -1,9 +1,15 @@
import { Text } from '@nextui-org/react' import { Text } from '@nextui-org/react'
/**
* Types
*/
interface Props { interface Props {
children: string children: string
} }
/**
* Component
*/
export default function PageHeader({ children }: Props) { export default function PageHeader({ children }: Props) {
return ( return (
<Text <Text

View File

@ -2,27 +2,31 @@ import { WalletContext } from '@/contexts/WalletContext'
import { Card, Container, Divider, Loading } from '@nextui-org/react' import { Card, Container, Divider, Loading } from '@nextui-org/react'
import { Fragment, ReactNode, useCallback, useContext, useEffect } from 'react' import { Fragment, ReactNode, useCallback, useContext, useEffect } from 'react'
/**
* Types
*/
interface Props { interface Props {
children: ReactNode | ReactNode[] children: ReactNode | ReactNode[]
} }
/**
* Container
*/
export default function GlobalLayout({ children }: Props) { export default function GlobalLayout({ children }: Props) {
const { const { walletState, walletActions } = useContext(WalletContext)
state: { initialized }, const waletReady = walletState.ready
actions
} = useContext(WalletContext)
const onInitialize = useCallback(async () => { const onInitialize = useCallback(async () => {
actions.createWallet() walletActions.createWallet()
await actions.createWalletConnectClient() await walletActions.createWalletConnectClient()
actions.setInitialized() walletActions.setInitialized()
}, [actions]) }, [walletActions])
useEffect(() => { useEffect(() => {
if (!initialized) { if (!waletReady) {
onInitialize() onInitialize()
} }
}, [initialized, onInitialize]) }, [waletReady, onInitialize])
return ( return (
<Container <Container
@ -38,11 +42,11 @@ export default function GlobalLayout({ children }: Props) {
height: '92vh', height: '92vh',
maxWidth: '600px', maxWidth: '600px',
width: '100%', width: '100%',
justifyContent: initialized ? 'normal' : 'center', justifyContent: waletReady ? 'normal' : 'center',
alignItems: initialized ? 'normal' : 'center' alignItems: waletReady ? 'normal' : 'center'
}} }}
> >
{initialized ? ( {waletReady ? (
<Fragment> <Fragment>
<Card.Header>Header</Card.Header> <Card.Header>Header</Card.Header>
<Divider /> <Divider />

View File

@ -7,7 +7,7 @@ import { createContext, ReactNode, useMemo, useState } from 'react'
* Types * Types
*/ */
interface State { interface State {
initialized: boolean ready: boolean
wallet: Wallet wallet: Wallet
walletConnectClient: WalletConnectClient walletConnectClient: WalletConnectClient
} }
@ -19,8 +19,8 @@ interface Actions {
} }
interface Context { interface Context {
state: State walletState: State
actions: Actions walletActions: Actions
} }
interface Props { interface Props {
@ -33,13 +33,13 @@ interface Props {
export const WalletContext = createContext<Context>({} as Context) export const WalletContext = createContext<Context>({} as Context)
export function WalletContextProvider({ children }: Props) { export function WalletContextProvider({ children }: Props) {
const [state, setState] = useState<State>({ const [walletState, setState] = useState<State>({
initialized: false, ready: false,
wallet: undefined, wallet: undefined,
walletConnectClient: undefined walletConnectClient: undefined
}) })
const actions = useMemo( const walletActions = useMemo(
() => ({ () => ({
setInitialized() { setInitialized() {
setState(s => ({ ...s, initialized: true })) setState(s => ({ ...s, initialized: true }))
@ -70,5 +70,9 @@ export function WalletContextProvider({ children }: Props) {
[] []
) )
return <WalletContext.Provider value={{ state, actions }}>{children}</WalletContext.Provider> return (
<WalletContext.Provider value={{ walletState, walletActions }}>
{children}
</WalletContext.Provider>
)
} }