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

feat: added LoadingModal #416

Merged
merged 1 commit into from
Jan 16, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions advanced/wallets/react-wallet-v2/src/components/Modal.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ import { Modal as NextModal } from '@nextui-org/react'
import { useSnapshot } from 'valtio'
import { useCallback, useMemo } from 'react'
import AuthRequestModal from '@/views/AuthRequestModal'
import LoadingModal from '@/views/LoadingModal'

export default function Modal() {
const { open, view } = useSnapshot(ModalStore.state)
Expand Down Expand Up @@ -56,6 +57,8 @@ export default function Modal() {
return <SessionSignKadenaModal />
case 'AuthRequestModal':
return <AuthRequestModal />
case 'LoadingModal':
return <LoadingModal />
default:
return null
}
Expand Down
58 changes: 29 additions & 29 deletions advanced/wallets/react-wallet-v2/src/pages/walletconnect.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import PageHeader from '@/components/PageHeader'
import QrReader from '@/components/QrReader'
import { web3wallet } from '@/utils/WalletConnectUtil'
import { Button, Input, Loading, Text } from '@nextui-org/react'
import { Fragment, useMemo, useState } from 'react'
import { Fragment, useEffect, useState } from 'react'
import { styledToast } from '@/utils/HelperUtil'
import ModalStore from '@/store/ModalStore'

Expand All @@ -27,7 +27,6 @@ export default function WalletConnectPage(params: { deepLink?: string }) {
})
try {
setLoading(true)
ModalStore.open('SessionProposalModal', {})
web3wallet.core.pairing.events.on('pairing_expire', pairingExpiredListener)
await web3wallet.pair({ uri })
} catch (error) {
Expand All @@ -39,7 +38,7 @@ export default function WalletConnectPage(params: { deepLink?: string }) {
}
}

useMemo(() => {
useEffect(() => {
if (deepLink) {
onConnect(deepLink)
}
Expand All @@ -48,34 +47,35 @@ export default function WalletConnectPage(params: { deepLink?: string }) {
return (
<Fragment>
<PageHeader title="WalletConnect" />
<>
<QrReader onConnect={onConnect} />

<QrReader onConnect={onConnect} />
<Text size={13} css={{ textAlign: 'center', marginTop: '$10', marginBottom: '$10' }}>
or use walletconnect uri
</Text>

<Text size={13} css={{ textAlign: 'center', marginTop: '$10', marginBottom: '$10' }}>
or use walletconnect uri
</Text>

<Input
css={{ width: '100%' }}
bordered
aria-label="wc url connect input"
placeholder="e.g. wc:a281567bb3e4..."
onChange={e => setUri(e.target.value)}
value={uri}
data-testid="uri-input"
contentRight={
<Button
size="xs"
disabled={!uri}
css={{ marginLeft: -60 }}
onClick={() => onConnect(uri)}
color="gradient"
data-testid="uri-connect-button"
>
{loading ? <Loading size="md" type="points" color={'white'} /> : 'Connect'}
</Button>
}
/>
<Input
css={{ width: '100%' }}
bordered
aria-label="wc url connect input"
placeholder="e.g. wc:a281567bb3e4..."
onChange={e => setUri(e.target.value)}
value={uri}
data-testid="uri-input"
contentRight={
<Button
size="xs"
disabled={!uri}
css={{ marginLeft: -60 }}
onClick={() => onConnect(uri)}
color="gradient"
data-testid="uri-connect-button"
>
{loading ? <Loading size="md" type="points" color={'white'} /> : 'Connect'}
</Button>
}
/>
</>
</Fragment>
)
}
34 changes: 33 additions & 1 deletion advanced/wallets/react-wallet-v2/src/pages/wc.tsx
Original file line number Diff line number Diff line change
@@ -1,14 +1,46 @@
import { Text } from '@nextui-org/react'
import { Fragment } from 'react'
import { Fragment, useCallback, useEffect, useState } from 'react'
import { useRouter } from 'next/router'
import WalletConnectPage from './walletconnect'
import ModalStore from '@/store/ModalStore'
import { useSnapshot } from 'valtio'

export default function DeepLinkPairingPage() {
const state = useSnapshot(ModalStore.state)
const router = useRouter()
const [loadingMessage, setLoadingMessage] = useState<string>('')
const [requestTimeout, setRequestTimeout] = useState<NodeJS.Timeout | null>(null)

const uri = router.query.uri as string
const requestId = router.query.requestId as string

const removeTimeout = useCallback(() => {
if (requestTimeout) {
clearTimeout(requestTimeout)
}
}, [requestTimeout])

useEffect(() => {
if (state.view == 'LoadingModal') {
const timeout = setTimeout(() => {
setLoadingMessage('Your request is taking longer than usual. Feel free to try again.')
}, 15_000)
setRequestTimeout(timeout)
} else if (state.view) {
removeTimeout()
}
}, [state.view])

useEffect(() => {
if (requestId) {
ModalStore.open('LoadingModal', { loadingMessage })
}

if (uri) {
ModalStore.open('LoadingModal', { loadingMessage })
}
}, [uri, requestId, loadingMessage])

if (!uri && !requestId) {
return (
<Fragment>
Expand Down
2 changes: 2 additions & 0 deletions advanced/wallets/react-wallet-v2/src/store/ModalStore.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ interface ModalData {
requestEvent?: SignClientTypes.EventArguments['session_request']
requestSession?: SessionTypes.Struct
request?: Web3WalletTypes.AuthRequest
loadingMessage?: string
}

interface State {
Expand All @@ -29,6 +30,7 @@ interface State {
| 'SessionSignTezosModal'
| 'SessionSignKadenaModal'
| 'AuthRequestModal'
| 'LoadingModal'
data?: ModalData
}

Expand Down
34 changes: 34 additions & 0 deletions advanced/wallets/react-wallet-v2/src/views/LoadingModal.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
import { Col, Divider, Link, Loading, Row, Text, styled } from '@nextui-org/react'
import { CoreTypes } from '@walletconnect/types'
import NewReleasesIcon from '@mui/icons-material/NewReleases'
import RequestModalContainer from '@/components/RequestModalContainer'
import { useSnapshot } from 'valtio'
import ModalStore from '@/store/ModalStore'

export default function LoadingModal() {
const state = useSnapshot(ModalStore.state)
const message = state.data?.loadingMessage

return (
<RequestModalContainer title="">
<div style={{ textAlign: 'center', padding: '20px' }}>
<Row>
<Col>
<Loading size="lg" />
</Col>
</Row>
<Row align="center">
<Col>
<Text h3>Loading your request...</Text>
</Col>
</Row>
{message ? (
<div style={{ textAlign: 'center' }}>
<Divider y={1} />
<Text>{message}</Text>
</div>
) : null}
</div>
</RequestModalContainer>
)
}
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
/* eslint-disable react-hooks/rules-of-hooks */
import { Col, Grid, Loading, Row, Text, styled } from '@nextui-org/react'
import { useCallback, useMemo, useState } from 'react'
import { buildApprovedNamespaces, getSdkError } from '@walletconnect/utils'
Expand Down Expand Up @@ -265,14 +264,7 @@ export default function SessionProposalModal() {
ModalStore.close()
}, [proposal])

return !proposal ? (
<>
<br />
<Loading size="lg" color="primary" type="default" />
<Text>Attempting to pair...</Text>
<br />
</>
) : (
return (
<RequestModal
metadata={proposal.params.proposer.metadata}
onApprove={onApprove}
Expand Down
Loading