Establish WalletConnectManager concept
This commit is contained in:
parent
27905179a1
commit
5640db7dff
@ -1,33 +1,18 @@
|
|||||||
import WalletConnectStore from '@/store/WalletConnectStore'
|
|
||||||
import WalletStore from '@/store/WalletStore'
|
|
||||||
import { Card, Container, Divider, Loading } from '@nextui-org/react'
|
import { Card, Container, Divider, Loading } from '@nextui-org/react'
|
||||||
import { Fragment, ReactNode, useCallback, useEffect, useState } from 'react'
|
import { Fragment, ReactNode } from 'react'
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Types
|
* Types
|
||||||
*/
|
*/
|
||||||
interface Props {
|
interface Props {
|
||||||
|
initialized: boolean
|
||||||
children: ReactNode | ReactNode[]
|
children: ReactNode | ReactNode[]
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Container
|
* Container
|
||||||
*/
|
*/
|
||||||
export default function GlobalLayout({ children }: Props) {
|
export default function GlobalLayout({ children, initialized }: Props) {
|
||||||
const [initialized, setInitialized] = useState(false)
|
|
||||||
|
|
||||||
const onInitialize = useCallback(async () => {
|
|
||||||
WalletStore.createWallet()
|
|
||||||
await WalletConnectStore.createWalletConnectClient()
|
|
||||||
setInitialized(true)
|
|
||||||
}, [])
|
|
||||||
|
|
||||||
useEffect(() => {
|
|
||||||
if (!initialized) {
|
|
||||||
onInitialize()
|
|
||||||
}
|
|
||||||
}, [initialized, onInitialize])
|
|
||||||
|
|
||||||
return (
|
return (
|
||||||
<Container
|
<Container
|
||||||
display="flex"
|
display="flex"
|
@ -0,0 +1,26 @@
|
|||||||
|
import WalletConnectStore from '@/store/WalletConnectStore'
|
||||||
|
import { Modal } from '@nextui-org/react'
|
||||||
|
import { CLIENT_EVENTS } from '@walletconnect/client'
|
||||||
|
import { SessionTypes } from '@walletconnect/types'
|
||||||
|
import { useCallback, useEffect, useState } from 'react'
|
||||||
|
|
||||||
|
export default function WalletConnectManager() {
|
||||||
|
const [open, setOpen] = useState(false)
|
||||||
|
const { client } = WalletConnectStore.state
|
||||||
|
|
||||||
|
const onSessionProposal = useCallback((proposal: SessionTypes.Proposal) => {}, [])
|
||||||
|
|
||||||
|
useEffect(() => {
|
||||||
|
client.on(CLIENT_EVENTS.session.proposal, onSessionProposal)
|
||||||
|
|
||||||
|
return () => client.disconnect()
|
||||||
|
}, [client, onSessionProposal])
|
||||||
|
|
||||||
|
return (
|
||||||
|
<Modal closeButton open={open} onClose={() => setOpen(false)}>
|
||||||
|
<Modal.Header></Modal.Header>
|
||||||
|
<Modal.Body></Modal.Body>
|
||||||
|
<Modal.Footer></Modal.Footer>
|
||||||
|
</Modal>
|
||||||
|
)
|
||||||
|
}
|
21
wallets/react-wallet-v2/src/hooks/useInitialization.ts
Normal file
21
wallets/react-wallet-v2/src/hooks/useInitialization.ts
Normal file
@ -0,0 +1,21 @@
|
|||||||
|
import WalletConnectStore from '@/store/WalletConnectStore'
|
||||||
|
import WalletStore from '@/store/WalletStore'
|
||||||
|
import { useCallback, useEffect, useState } from 'react'
|
||||||
|
|
||||||
|
export default function useInitialization() {
|
||||||
|
const [initialized, setInitialized] = useState(false)
|
||||||
|
|
||||||
|
const onInitialize = useCallback(async () => {
|
||||||
|
WalletStore.createWallet()
|
||||||
|
await WalletConnectStore.createWalletConnectClient()
|
||||||
|
setInitialized(true)
|
||||||
|
}, [])
|
||||||
|
|
||||||
|
useEffect(() => {
|
||||||
|
if (!initialized) {
|
||||||
|
onInitialize()
|
||||||
|
}
|
||||||
|
}, [initialized, onInitialize])
|
||||||
|
|
||||||
|
return initialized
|
||||||
|
}
|
@ -1,10 +1,14 @@
|
|||||||
import GlobalLayout from '@/containers/GlobalLayout'
|
import Layout from '@/components/Layout'
|
||||||
|
import WalletConnectManager from '@/components/WalletConnectManager'
|
||||||
|
import useInitialization from '@/hooks/useInitialization'
|
||||||
import { darkTheme, lightTheme } from '@/utils/ThemeUtil'
|
import { darkTheme, lightTheme } from '@/utils/ThemeUtil'
|
||||||
import { NextUIProvider } from '@nextui-org/react'
|
import { NextUIProvider } from '@nextui-org/react'
|
||||||
import { ThemeProvider } from 'next-themes'
|
import { ThemeProvider } from 'next-themes'
|
||||||
import { AppProps } from 'next/app'
|
import { AppProps } from 'next/app'
|
||||||
|
|
||||||
export default function App({ Component, pageProps }: AppProps) {
|
export default function App({ Component, pageProps }: AppProps) {
|
||||||
|
const initialized = useInitialization()
|
||||||
|
|
||||||
return (
|
return (
|
||||||
<ThemeProvider
|
<ThemeProvider
|
||||||
defaultTheme="system"
|
defaultTheme="system"
|
||||||
@ -15,9 +19,11 @@ export default function App({ Component, pageProps }: AppProps) {
|
|||||||
}}
|
}}
|
||||||
>
|
>
|
||||||
<NextUIProvider>
|
<NextUIProvider>
|
||||||
<GlobalLayout>
|
<Layout initialized={initialized}>
|
||||||
<Component {...pageProps} />
|
<Component {...pageProps} />
|
||||||
</GlobalLayout>
|
</Layout>
|
||||||
|
|
||||||
|
{initialized && <WalletConnectManager />}
|
||||||
</NextUIProvider>
|
</NextUIProvider>
|
||||||
</ThemeProvider>
|
</ThemeProvider>
|
||||||
)
|
)
|
||||||
|
Loading…
Reference in New Issue
Block a user