Skip to content

Commit

Permalink
Rm onPaste
Browse files Browse the repository at this point in the history
  • Loading branch information
katspaugh committed Sep 20, 2023
1 parent 5eb5938 commit 8cc2924
Show file tree
Hide file tree
Showing 2 changed files with 7 additions and 49 deletions.
12 changes: 3 additions & 9 deletions src/components/wallet-connect/hooks/useWalletConnect.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,13 +5,7 @@ import { useCurrentChain } from '@/hooks/useChains'
import useSafeInfo from '@/hooks/useSafeInfo'
import useSafeWalletProvider from '@/safe-wallet-provider/useSafeWalletProvider'
import { WalletConnect } from './WalletConnect'

const EVMBasedNamespaces: string = 'eip155'

// see https://docs.walletconnect.com/2.0/specs/sign/error-codes
const UNSUPPORTED_CHAIN_ERROR_CODE = 5100
const INVALID_METHOD_ERROR_CODE = 1001
const USER_REJECTED_REQUEST_CODE = 4001
import { WC_ERRORS, EVMBasedNamespaces } from './constants'

export const errorLabel =
'We were unable to create a connection due to compatibility issues with the latest WalletConnect v2 upgrade. We are actively working with the WalletConnect team and the dApps to get these issues resolved. Use Safe Apps instead wherever possible.'
Expand Down Expand Up @@ -93,7 +87,7 @@ const useWalletConnect = (): useWalletConnectType => {
setError(errorMessage)
await walletConnect.sendSessionResponse({
topic,
response: rejectResponse(id, UNSUPPORTED_CHAIN_ERROR_CODE, errorMessage),
response: rejectResponse(id, WC_ERRORS.UNSUPPORTED_CHAIN_ERROR_CODE, errorMessage),
})
return
}
Expand All @@ -115,7 +109,7 @@ const useWalletConnect = (): useWalletConnectType => {
} catch (error: any) {
setError(error?.message)
const isUserRejection = error?.message?.includes?.('Transaction was rejected')
const code = isUserRejection ? USER_REJECTED_REQUEST_CODE : INVALID_METHOD_ERROR_CODE
const code = isUserRejection ? WC_ERRORS.USER_REJECTED_REQUEST_CODE : WC_ERRORS.INVALID_METHOD_ERROR_CODE

try {
await walletConnect.sendSessionResponse({
Expand Down
44 changes: 4 additions & 40 deletions src/components/wallet-connect/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ import {
Typography,
} from '@mui/material'
import WcIcon from '@/public/images/apps/wallet-connect.svg'
import { type ChangeEvent, useCallback, useState, useRef } from 'react'
import { type ChangeEvent, useState, useRef } from 'react'
import useWalletConnect, { WC_CONNECT_STATE } from './hooks/useWalletConnect'
import css from './styles.module.css'
import type { Web3WalletTypes } from '@walletconnect/web3wallet'
Expand All @@ -38,7 +38,6 @@ const extractInformationFromProposal = (sessionProposal: Web3WalletTypes.Session

export const ConnectWC = () => {
const [openModal, setOpenModal] = useState(false)
const [wcConnectUrl, setWcConnectUrl] = useState('')
const { wcConnect, wcDisconnect, wcClientData, wcApproveSession, acceptInvalidSession, wcState, sessionProposal } =
useWalletConnect()
const anchorElem = useRef<HTMLButtonElement | null>(null)
Expand All @@ -55,45 +54,11 @@ export const ConnectWC = () => {
setOpenModal((prev) => !prev)
}

const onConnect = useCallback(
async (uri: string) => {
await wcConnect(uri)
},
[wcConnect],
)

const onChangeWcUrl = (event: ChangeEvent<HTMLInputElement | HTMLTextAreaElement>) => {
const newValue = event.target.value
setWcConnectUrl(newValue)
const onChangeWcUrl = async (event: ChangeEvent<HTMLInputElement | HTMLTextAreaElement>) => {
const uri = event.target.value
await wcConnect(uri)
}

const onPaste = useCallback(
(event: React.ClipboardEvent) => {
const connectWithUri = (data: string) => {
if (data.startsWith('wc')) {
onConnect(data)
}
}

setWcConnectUrl('')

if (wcClientData) {
return
}

const items = event.clipboardData.items

for (const index in items) {
const item = items[index]

if (item.kind === 'string' && item.type === 'text/plain') {
connectWithUri(event.clipboardData.getData('Text'))
}
}
},
[wcClientData, onConnect],
)

return (
<>
<IconButton onClick={handleWidgetIconClick} ref={anchorElem}>
Expand Down Expand Up @@ -219,7 +184,6 @@ export const ConnectWC = () => {
<TextField
placeholder="wc:"
label="Wallet Connect URI"
onPaste={onPaste}
onChange={onChangeWcUrl}
fullWidth
sx={{ mt: 2 }}
Expand Down

0 comments on commit 8cc2924

Please sign in to comment.