From 129b1b1db03b943f5e00cbc306adf1072557659f Mon Sep 17 00:00:00 2001
From: katspaugh <381895+katspaugh@users.noreply.github.com>
Date: Tue, 20 Feb 2024 07:17:39 +0100
Subject: [PATCH] Fix: avoid fetching Safe Apps on all pages (#3254)
---
.../__tests__/SafeTokenWidget.test.tsx | 37 +++----------------
.../common/SafeTokenWidget/index.tsx | 34 +++++------------
src/config/constants.ts | 11 +++++-
.../__tests__/WalletConnectContext.test.tsx | 20 +---------
.../WalletConnectProvider/index.tsx | 18 +++------
.../analytics/__tests__/tx-tracking.test.ts | 2 +-
src/utils/gateway.ts | 7 +---
7 files changed, 36 insertions(+), 93 deletions(-)
diff --git a/src/components/common/SafeTokenWidget/__tests__/SafeTokenWidget.test.tsx b/src/components/common/SafeTokenWidget/__tests__/SafeTokenWidget.test.tsx
index 04d4c189e0..49090fab72 100644
--- a/src/components/common/SafeTokenWidget/__tests__/SafeTokenWidget.test.tsx
+++ b/src/components/common/SafeTokenWidget/__tests__/SafeTokenWidget.test.tsx
@@ -1,50 +1,23 @@
-import * as nextRouter from 'next/router'
+import * as nextNav from 'next/navigation'
import useChainId from '@/hooks/useChainId'
import { render, waitFor } from '@/tests/test-utils'
-import { SafeAppAccessPolicyTypes } from '@safe-global/safe-gateway-typescript-sdk'
import SafeTokenWidget from '..'
import { toBeHex } from 'ethers'
import { AppRoutes } from '@/config/routes'
import useSafeTokenAllocation, { useSafeVotingPower } from '@/hooks/useSafeTokenAllocation'
-const MOCK_GOVERNANCE_APP_URL = 'https://mock.governance.safe.global'
-
jest.mock('@/hooks/useChainId', () => jest.fn(() => '1'))
jest.mock('@/hooks/useSafeTokenAllocation')
-jest.mock(
- '@/hooks/safe-apps/useRemoteSafeApps',
- jest.fn(() => ({
- useRemoteSafeApps: () => [
- [
- {
- id: 61,
- url: MOCK_GOVERNANCE_APP_URL,
- chainIds: ['4'],
- name: 'Safe {DAO} Governance',
- description: '',
- iconUrl: '',
- tags: ['safe-dao-governance-app'],
- accessControl: {
- type: SafeAppAccessPolicyTypes.NoRestrictions,
- },
- },
- ],
- ],
- })),
-)
-
describe('SafeTokenWidget', () => {
const fakeSafeAddress = toBeHex('0x1', 20)
beforeEach(() => {
jest.restoreAllMocks()
- jest.spyOn(nextRouter, 'useRouter').mockImplementation(
+ jest.spyOn(nextNav, 'useSearchParams').mockImplementation(
() =>
({
- query: {
- safe: fakeSafeAddress,
- },
+ get: () => fakeSafeAddress,
} as any),
)
})
@@ -90,7 +63,9 @@ describe('SafeTokenWidget', () => {
const result = render()
await waitFor(() => {
expect(result.baseElement).toContainHTML(
- `href="${AppRoutes.apps.open}?safe=${fakeSafeAddress}&appUrl=${encodeURIComponent(MOCK_GOVERNANCE_APP_URL)}"`,
+ `href="${AppRoutes.apps.open}?safe=${fakeSafeAddress}&appUrl=${encodeURIComponent(
+ 'https://safe-dao-governance.dev.5afe.dev',
+ )}`,
)
})
})
diff --git a/src/components/common/SafeTokenWidget/index.tsx b/src/components/common/SafeTokenWidget/index.tsx
index ffd3f6b1bd..234f1d3b84 100644
--- a/src/components/common/SafeTokenWidget/index.tsx
+++ b/src/components/common/SafeTokenWidget/index.tsx
@@ -1,14 +1,12 @@
-import { SafeAppsTag, SAFE_TOKEN_ADDRESSES } from '@/config/constants'
+import { IS_PRODUCTION, SAFE_TOKEN_ADDRESSES } from '@/config/constants'
import { AppRoutes } from '@/config/routes'
-import { useRemoteSafeApps } from '@/hooks/safe-apps/useRemoteSafeApps'
import useChainId from '@/hooks/useChainId'
import useSafeTokenAllocation, { useSafeVotingPower, type Vesting } from '@/hooks/useSafeTokenAllocation'
import { OVERVIEW_EVENTS } from '@/services/analytics'
import { formatVisualAmount } from '@/utils/formatters'
import { Box, Button, ButtonBase, Skeleton, Tooltip, Typography } from '@mui/material'
import Link from 'next/link'
-import { useRouter } from 'next/router'
-import type { UrlObject } from 'url'
+import { useSearchParams } from 'next/navigation'
import Track from '../Track'
import SafeTokenIcon from '@/public/images/common/safe-token.svg'
import css from './styles.module.css'
@@ -36,13 +34,11 @@ const canRedeemSep5Airdrop = (allocation?: Vesting[]): boolean => {
return !sep5Allocation.isRedeemed && !sep5Allocation.isExpired
}
-const SEP5_DEADLINE = '27.10'
+const GOVERNANCE_APP_URL = IS_PRODUCTION ? 'https://governance.safe.global' : 'https://safe-dao-governance.dev.5afe.dev'
const SafeTokenWidget = () => {
const chainId = useChainId()
- const router = useRouter()
- const [apps] = useRemoteSafeApps(SafeAppsTag.SAFE_GOVERNANCE_APP)
- const governanceApp = apps?.[0]
+ const query = useSearchParams()
const [allocationData, , allocationDataLoading] = useSafeTokenAllocation()
const [allocation, , allocationLoading] = useSafeVotingPower(allocationData)
@@ -52,30 +48,20 @@ const SafeTokenWidget = () => {
return null
}
- const url: UrlObject | undefined = governanceApp
- ? {
- pathname: AppRoutes.apps.open,
- query: { safe: router.query.safe, appUrl: governanceApp.url },
- }
- : undefined
+ const url = {
+ pathname: AppRoutes.apps.open,
+ query: { safe: query.get('safe'), appUrl: GOVERNANCE_APP_URL },
+ }
const canRedeemSep5 = canRedeemSep5Airdrop(allocationData)
const flooredSafeBalance = formatVisualAmount(allocation || BigInt(0), TOKEN_DECIMALS, 2)
return (
-
+