Skip to content

Commit

Permalink
fix: don't set clipboard + move validation to hook
Browse files Browse the repository at this point in the history
  • Loading branch information
iamacook committed Oct 17, 2023
1 parent a518831 commit f0f25b6
Show file tree
Hide file tree
Showing 7 changed files with 15 additions and 28 deletions.
1 change: 1 addition & 0 deletions src/components/walletconnect/HeaderWidget/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ const usePrepopulatedUri = (): [string, () => void] => {

const clearUri = useCallback(() => {
setSearchParamWcUri(null)
// This does not clear the system clipboard, just state
setClipboardWcUri('')
}, [setClipboardWcUri, setSearchParamWcUri])

Expand Down
4 changes: 1 addition & 3 deletions src/components/walletconnect/WcInput/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -43,9 +43,7 @@ const WcInput = ({ uri }: { uri: string }): ReactElement => {
)

useEffect(() => {
if (uri.startsWith('wc:')) {
onInput(uri)
}
onInput(uri)
}, [onInput, uri])

const onPaste = useCallback(async () => {
Expand Down
1 change: 0 additions & 1 deletion src/services/exceptions/ErrorCodes.ts
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,6 @@ enum ErrorCodes {
_706 = '706: Failed to write to IndexedDB',
_707 = '707: Error requesting clipboard permissions',
_708 = '708: Failed to read clipboard',
_709 = '709: Failed to write to clipboard',

_800 = '800: Safe creation tx failed',
_801 = '801: Failed to send a tx with a spending limit',
Expand Down
20 changes: 5 additions & 15 deletions src/services/walletconnect/useWalletConnectClipboardUri.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,10 @@
import { useState, useEffect } from 'react'
import type { Dispatch, SetStateAction } from 'react'

import { getClipboard, isClipboardGranted } from '@/utils/clipboard'
import { Errors, logError } from '../exceptions'
import { isPairingUri } from './utils'

export const useWalletConnectClipboardUri = (): [string, (data: string) => Promise<void>] => {
export const useWalletConnectClipboardUri = (): [string, Dispatch<SetStateAction<string>>] => {
const [state, setState] = useState('')

useEffect(() => {
Expand All @@ -20,7 +21,7 @@ export const useWalletConnectClipboardUri = (): [string, (data: string) => Promi

const clipboard = await getClipboard()

if (clipboard.startsWith('wc:')) {
if (isPairingUri(clipboard)) {
setState(clipboard)
}
}
Expand All @@ -34,16 +35,5 @@ export const useWalletConnectClipboardUri = (): [string, (data: string) => Promi
}
}, [])

const setClipboard = async (data: string) => {
await navigator.clipboard
.writeText(data)
.then(() => {
setState(data)
})
.catch((e) => {
logError(Errors._709, e)
})
}

return [state, setClipboard]
return [state, setState]
}
5 changes: 4 additions & 1 deletion src/services/walletconnect/useWalletConnectSearchParamUri.ts
Original file line number Diff line number Diff line change
@@ -1,13 +1,16 @@
import { useRouter } from 'next/router'
import { useCallback } from 'react'

import { isPairingUri } from './utils'

const WC_URI_SEARCH_PARAM = 'wc'

export function useWalletConnectSearchParamUri(): [string | null, (wcUri: string | null) => void] {
const router = useRouter()

const wcUriQuery = router.query[WC_URI_SEARCH_PARAM]
const wcUri = wcUriQuery ? (Array.isArray(wcUriQuery) ? wcUriQuery[0] : wcUriQuery) : null
const value = wcUriQuery ? (Array.isArray(wcUriQuery) ? wcUriQuery[0] : wcUriQuery) : null
const wcUri = value && isPairingUri(value) ? value : null

const setWcUri = useCallback(
(wcUri: string | null) => {
Expand Down
4 changes: 4 additions & 0 deletions src/services/walletconnect/utils.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,9 @@
import { EIP155 } from './constants'

export const isPairingUri = (uri: string): boolean => {
return uri.startsWith('wc:')
}

export const getEip155ChainId = (chainId: string): string => {
return `${EIP155}:${chainId}`
}
Expand Down
8 changes: 0 additions & 8 deletions src/utils/clipboard.ts
Original file line number Diff line number Diff line change
Expand Up @@ -32,11 +32,3 @@ export const getClipboard = async (): Promise<string> => {

return clipboard
}

export const setClipboard = async (data: string): Promise<void> => {
try {
await navigator.clipboard.writeText(data)
} catch (e) {
logError(Errors._709, e)
}
}

0 comments on commit f0f25b6

Please sign in to comment.