-
Notifications
You must be signed in to change notification settings - Fork 202
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
refactor: move out useSyncConnectedChainToAnalytics (#1968)
- Loading branch information
1 parent
3d7d199
commit dc75402
Showing
2 changed files
with
72 additions
and
59 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
68 changes: 68 additions & 0 deletions
68
packages/arb-token-bridge-ui/src/components/App/useSyncConnectedChainToAnalytics.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,68 @@ | ||
import { useAccount } from 'wagmi' | ||
import { useEffect } from 'react' | ||
import * as Sentry from '@sentry/react' | ||
|
||
import { useNetworks } from '../../hooks/useNetworks' | ||
import { useNetworksRelationship } from '../../hooks/useNetworksRelationship' | ||
import { ProviderName, trackEvent } from '../../util/AnalyticsUtils' | ||
import { rpcURLs } from '../../util/networks' | ||
|
||
// connector names: https://github.com/wagmi-dev/wagmi/blob/b17c07443e407a695dfe9beced2148923b159315/docs/pages/core/connectors/_meta.en-US.json#L4 | ||
function getWalletName(connectorName: string): ProviderName { | ||
switch (connectorName) { | ||
case 'MetaMask': | ||
case 'Coinbase Wallet': | ||
case 'Trust Wallet': | ||
case 'Safe': | ||
case 'Injected': | ||
case 'Ledger': | ||
return connectorName | ||
|
||
case 'WalletConnectLegacy': | ||
case 'WalletConnect': | ||
return 'WalletConnect' | ||
|
||
default: | ||
return 'Other' | ||
} | ||
} | ||
|
||
/** given our RPC url, sanitize it before logging to Sentry, to only pass the url and not the keys */ | ||
function getBaseUrl(url: string) { | ||
try { | ||
const urlObject = new URL(url) | ||
return `${urlObject.protocol}//${urlObject.hostname}` | ||
} catch { | ||
// if invalid url passed | ||
return '' | ||
} | ||
} | ||
|
||
export function useSyncConnectedChainToAnalytics() { | ||
const [networks] = useNetworks() | ||
const { parentChain, childChain } = useNetworksRelationship(networks) | ||
const { isConnected, connector } = useAccount() | ||
|
||
useEffect(() => { | ||
if (isConnected && connector) { | ||
const walletName = getWalletName(connector.name) | ||
trackEvent('Connect Wallet Click', { walletName }) | ||
} | ||
|
||
// set a custom tag in sentry to filter issues by connected wallet.name | ||
Sentry.setTag('wallet.name', connector?.name ?? '') | ||
}, [isConnected, connector]) | ||
|
||
useEffect(() => { | ||
Sentry.setTag('network.parent_chain_id', parentChain.id) | ||
Sentry.setTag( | ||
'network.parent_chain_rpc_url', | ||
getBaseUrl(rpcURLs[parentChain.id] ?? '') | ||
) | ||
Sentry.setTag('network.child_chain_id', childChain.id) | ||
Sentry.setTag( | ||
'network.child_chain_rpc_url', | ||
getBaseUrl(rpcURLs[childChain.id] ?? '') | ||
) | ||
}, [childChain.id, parentChain.id]) | ||
} |