Skip to content

Commit

Permalink
Fix last used
Browse files Browse the repository at this point in the history
  • Loading branch information
jribbink committed Aug 8, 2024
1 parent ca691f8 commit a0149d8
Show file tree
Hide file tree
Showing 7 changed files with 57 additions and 14 deletions.
8 changes: 8 additions & 0 deletions components/views/ConnectExtension.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@ import { FCL_SERVICE_METHODS } from '../../helpers/constants'
import { useRpc } from '../../contexts/FclContext'
import { FclRequest } from '../../helpers/rpc'
import WalletIcon from '../Icons/WalletIcon'
import { useWalletHistory } from '../../hooks/useWalletHistory'
import { handleCancel } from '../../helpers/window'

type ConnectExtensionProps = {
wallet: Wallet
Expand All @@ -15,13 +17,19 @@ export default function ConnectExtension({ wallet }: ConnectExtensionProps) {
const [isConnecting, setIsConnecting] = useState(false)
const hasAttemptedConnection = useRef(true)
const showSpinner = !rpc || isConnecting
const { setLastUsed } = useWalletHistory()

function connect() {
setIsConnecting(true)
wallet.services.forEach(service => {
if (service.method === FCL_SERVICE_METHODS.EXT) {
rpc
.request(FclRequest.EXEC_SERVICE, { service })
.then(() => {
console.log('Connected to extension')
setLastUsed(wallet)
handleCancel()
})
.catch(e => {
console.error('Failed to connect to extension', e)
})
Expand Down
8 changes: 7 additions & 1 deletion components/views/ScanConnect.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -4,14 +4,20 @@ import QRCode from '../QRCode'
import CopyButton from '../CopyButton'
import HybridButton from '../HybridButton'
import { useWcUri } from '../../hooks/useWcUri'
import { useWalletHistory } from '../../hooks/useWalletHistory'
import { handleCancel } from '../../helpers/window'

interface ScanConnectProps {
wallet: Wallet
onGetWallet: () => void
}

export default function ScanConnect({ wallet, onGetWallet }: ScanConnectProps) {
const { uri, error, isLoading } = useWcUri()
const { setLastUsed } = useWalletHistory()
const { uri, error, isLoading } = useWcUri(() => {
setLastUsed(wallet)
handleCancel()
})

return (
<Stack
Expand Down
1 change: 1 addition & 0 deletions helpers/rpc.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ import { Service } from '../types'

export enum DiscoveryNotification {
NOTIFY_QRCODE_ERROR = 'notifyQRCodeError',
NOTIFY_QRCODE_CONNECTED = 'notifyQRCodeConnected',
}

export enum FclRequest {
Expand Down
File renamed without changes.
6 changes: 3 additions & 3 deletions hooks/useWalletHistory.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,20 +6,20 @@ import { useLocalStorage } from './useLocalStorage'
export function useWalletHistory() {
const [, setLastUsedState] = useLocalStorage(
LOCAL_STORAGE_KEYS.LAST_USED,
null
null,
)
const [lastUsedUid] = useLocalStorage(LOCAL_STORAGE_KEYS.LAST_USED, null)

const setLastUsed = useCallback(
(wallet: Wallet) => {
setLastUsedState(wallet.uid)
},
[setLastUsedState]
[setLastUsedState],
)

const isLastUsed = useCallback(
(wallet: Wallet) => wallet.uid === lastUsedUid,
[lastUsedUid]
[lastUsedUid],
)

return { isLastUsed, setLastUsed, lastUsedUid }
Expand Down
2 changes: 1 addition & 1 deletion hooks/useWallets.ts
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ export function useWallets() {
}

const { data: wallets, error } = useSWR(genKey(requestUrl, body), url =>
fetcher<Wallet[]>(url, body)
fetcher<Wallet[]>(url, body),
)

return { wallets, error, isLoading: !wallets && !error }
Expand Down
46 changes: 37 additions & 9 deletions hooks/useWcUri.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@
import useSWR from 'swr'
import { useRpc } from '../contexts/FclContext'
import { DiscoveryNotification, FclRequest } from '../helpers/rpc'
import { useEffect } from 'react'

export function useWcUri() {
export function useWcUri(onConnected?: () => void) {
const rpc = useRpc()
const {
data: uri,
Expand All @@ -14,17 +15,44 @@ export function useWcUri() {
{},
)

// Subscribe to QR error notifications (e.g. user declined, QR expired)
const onError = ({ error: _error, uri: _uri }) => {
if (_uri === uri) {
// QR code is no longer valid, reset the URI and unsubscribe
return uri
})

useEffect(() => {
if (!rpc || !uri) {
return
}

const connectHandler = ({ uri: _uri }: { uri: string }) => {
if (uri === _uri) {
onConnected?.()
}
}

const errorHandler = ({
uri: errorUri,
error,
}: {
uri: string
error: string
}) => {
if (uri === errorUri) {
console.error('URI connection error:', error)
mutate()
rpc.unsubscribe(DiscoveryNotification.NOTIFY_QRCODE_ERROR, onError)
}
}
rpc.subscribe(DiscoveryNotification.NOTIFY_QRCODE_ERROR, onError)

return uri
})
rpc.subscribe(DiscoveryNotification.NOTIFY_QRCODE_CONNECTED, connectHandler)
rpc.subscribe(DiscoveryNotification.NOTIFY_QRCODE_ERROR, errorHandler)

return () => {
rpc.unsubscribe(
DiscoveryNotification.NOTIFY_QRCODE_CONNECTED,
connectHandler,
)
rpc.unsubscribe(DiscoveryNotification.NOTIFY_QRCODE_ERROR, errorHandler)
}
}, [uri, rpc])

return { uri, error, isLoading: !uri && !error }
}

0 comments on commit a0149d8

Please sign in to comment.