Skip to content

Commit

Permalink
feat: enter pairing URI from clipboard on focus
Browse files Browse the repository at this point in the history
  • Loading branch information
iamacook committed Oct 13, 2023
1 parent 847a4e0 commit f793009
Showing 1 changed file with 47 additions and 4 deletions.
51 changes: 47 additions & 4 deletions src/components/walletconnect/WcInput/index.tsx
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { useCallback, useContext, useState } from 'react'
import { useCallback, useContext, useEffect, useState } from 'react'
import { Button, InputAdornment, TextField } from '@mui/material'
import type { ReactElement } from 'react'

Expand Down Expand Up @@ -41,7 +41,7 @@ const WcInput = (): ReactElement => {
[walletConnect],
)

const onPaste = useCallback(async () => {
const getClipboard = useCallback(async () => {
let clipboard: string | null = null

try {
Expand All @@ -51,8 +51,51 @@ const WcInput = (): ReactElement => {
return
}

onInput(clipboard)
}, [onInput])
return clipboard
}, [])

const onPaste = useCallback(async () => {
const clipboard = await getClipboard()

if (clipboard) {
onInput(clipboard)
}
}, [getClipboard, onInput])

useEffect(() => {
if (!navigator || !window) {
return
}

const autofillUri = async () => {
let isGranted = false

try {
// @ts-expect-error navigator permissions types don't include clipboard
const permission = await navigator.permissions.query({ name: 'clipboard-read' })
isGranted = permission.state === 'granted'
} catch (e) {
setError(asError(e))
return
}

if (!isGranted) {
return
}

const clipboard = await getClipboard()

if (clipboard?.startsWith('wc:')) {
onInput(clipboard)
}
}

window.addEventListener('focus', autofillUri)

return () => {
window.removeEventListener('focus', autofillUri)
}
}, [getClipboard, onInput])

return (
<TextField
Expand Down

0 comments on commit f793009

Please sign in to comment.