Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix: Safe App share URL not always working #2830

Merged
merged 2 commits into from
Nov 28, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
37 changes: 0 additions & 37 deletions src/hooks/safe-apps/useChainFromQueryParams.ts

This file was deleted.

42 changes: 10 additions & 32 deletions src/pages/share/safe-app.tsx
Original file line number Diff line number Diff line change
@@ -1,49 +1,27 @@
import { useEffect } from 'react'
import Head from 'next/head'
import { useRouter } from 'next/router'
import { Box, CircularProgress } from '@mui/material'
import { useSafeAppUrl } from '@/hooks/safe-apps/useSafeAppUrl'
import { useChainFromQueryParams } from '@/hooks/safe-apps/useChainFromQueryParams'
import { SafeAppLanding } from '@/components/safe-apps/SafeAppLandingPage'
import { AppRoutes } from '@/config/routes'
import { useCurrentChain } from '@/hooks/useChains'

const ShareSafeApp = () => {
const router = useRouter()
const appUrl = useSafeAppUrl()
const { chain, validChain, loading: chainLoading, error: chainError } = useChainFromQueryParams()

useEffect(() => {
if (chainLoading) return

if (router.isReady && (!appUrl || !validChain || !chain)) {
router.push(AppRoutes.index)
}
}, [appUrl, validChain, chain, chainLoading, router])

if (chainLoading) {
return (
<Box py={4} textAlign="center">
<CircularProgress size={40} />
</Box>
)
}

if (!appUrl || !validChain || !chain) {
return null
}

if (chainError) {
throw new Error(chainError)
}
const chain = useCurrentChain()

return (
<>
<Head>
<title>Safe Apps – Share</title>
<title>{`Safe{Wallet} – Safe Apps`}</title>
</Head>

<main>
<SafeAppLanding appUrl={appUrl} chain={chain} />
{appUrl && chain ? (
<SafeAppLanding appUrl={appUrl} chain={chain} />
) : (
<Box py={4} textAlign="center">
<CircularProgress size={40} />
</Box>
)}
</main>
</>
)
Expand Down
32 changes: 28 additions & 4 deletions src/tests/pages/apps-share.test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,14 @@ describe('Share Safe App Page', () => {
})

it('Should show the app name, description and URL', async () => {
Object.defineProperty(window, 'location', {
writable: true,
value: {
pathname: '/share/safe-app',
search: '?appUrl=https://apps-portal.safe.global/tx-builder&chain=eth',
},
})

render(<ShareSafeApp />, {
routerProps: {
query: {
Expand Down Expand Up @@ -147,20 +155,28 @@ describe('Share Safe App Page', () => {
})

it('Should link to Safe Creation flow when the connected wallet has no owned Safes', async () => {
Object.defineProperty(window, 'location', {
writable: true,
value: {
pathname: '/share/safe-app',
search: '?appUrl=https://apps-portal.safe.global/tx-builder&chain=gor',
},
})

const address = `0x${crypto.randomBytes(20).toString('hex')}`
jest.spyOn(useWalletHook, 'default').mockImplementation(() => ({
ens: 'craicis90.eth',
address,
provider: jest.fn() as unknown as EIP1193Provider,
label: 'Metamask',
chainId: '4',
chainId: '5',
}))

render(<ShareSafeApp />, {
routerProps: {
query: {
appUrl: TX_BUILDER,
chain: 'rin',
chain: 'gor',
},
},
initialReduxState: {
Expand All @@ -173,14 +189,22 @@ describe('Share Safe App Page', () => {
})

await waitFor(() => {
expect(fetchSafeAppFromManifestSpy).toHaveBeenCalledWith(TX_BUILDER, '4')
expect(getSafeAppsSpy).toHaveBeenCalledWith('4', { url: TX_BUILDER })
expect(fetchSafeAppFromManifestSpy).toHaveBeenCalledWith(TX_BUILDER, '5')
expect(getSafeAppsSpy).toHaveBeenCalledWith('5', { url: TX_BUILDER })

expect(screen.getByText('Create new Safe Account')).toBeInTheDocument()
})
})

it('Should show a select input with owned safes when the connected wallet owns Safes', async () => {
Object.defineProperty(window, 'location', {
writable: true,
value: {
pathname: '/share/safe-app',
search: '?appUrl=https://apps-portal.safe.global/tx-builder&chain=eth',
},
})

const address = `0x${crypto.randomBytes(20).toString('hex')}`
const safeAddress = `0x${crypto.randomBytes(20).toString('hex')}`
jest.spyOn(useWalletHook, 'default').mockImplementation(() => ({
Expand Down
Loading