Skip to content

Commit

Permalink
collide swap and transfer into one route
Browse files Browse the repository at this point in the history
  • Loading branch information
marc-aurele-besner committed Feb 21, 2025
1 parent aaa23ce commit 10889a7
Show file tree
Hide file tree
Showing 12 changed files with 12 additions and 80 deletions.
7 changes: 0 additions & 7 deletions explorer/src/app/[chain]/swap/layout.tsx

This file was deleted.

16 changes: 0 additions & 16 deletions explorer/src/app/[chain]/swap/page.tsx

This file was deleted.

4 changes: 2 additions & 2 deletions explorer/src/app/[chain]/transfer/layout.tsx
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { MainLayout } from 'components/layout/Layout'
import { SwapHeader } from 'components/layout/SwapHeader'
import { TransferHeader } from 'components/layout/TransferHeader'
import type { ChildrenPageProps } from 'types/app'

export default async function Layout({ children }: ChildrenPageProps) {
return <MainLayout subHeader={<SwapHeader />}>{children}</MainLayout>
return <MainLayout subHeader={<TransferHeader />}>{children}</MainLayout>
}
4 changes: 2 additions & 2 deletions explorer/src/app/[chain]/transfer/page.tsx
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { Transfer } from 'components/Transfer'
import { NotFound } from 'components/layout/NotFound'
import { Swap } from 'components/Swap'
import { Routes } from 'constants/routes'
import { Metadata } from 'next'
import { FC } from 'react'
Expand All @@ -11,6 +11,6 @@ export const generateMetadata = ({ params: { chain } }: ChainPageProps): Metadat
getMetadata(chain, 'Transfer', undefined)

const Page: FC<ChainPageProps> = ({ params: { chain } }) =>
isRouteSupportingNetwork(chain, Routes.transfer) ? <Swap /> : <NotFound />
isRouteSupportingNetwork(chain, Routes.transfer) ? <Transfer /> : <NotFound />

export default Page
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,6 @@ import { FieldArray, Form, Formik } from 'formik'
import useIndexers from 'hooks/useIndexers'
import { useTxHelper } from 'hooks/useTxHelper'
import useWallet from 'hooks/useWallet'
import Link from 'next/link'
import { usePathname, useSearchParams } from 'next/navigation'
import { FC, useCallback, useEffect, useMemo, useState } from 'react'
import toast from 'react-hot-toast'
Expand All @@ -38,26 +37,7 @@ interface FormValues {
}

const DirectionBlock: FC<DirectionBlockProps> = ({ direction, maxAmount }) => {
const { network } = useIndexers()
const pathname = usePathname()
const searchParams = useSearchParams()

const linkToSwapOrTransfer = useMemo(() => {
const params = new URLSearchParams(searchParams.toString())
params.delete('receiver')
let href = `/${network}/${Routes.swap}?${params.toString()}`
let text = 'Send to your wallet?'
if (pathname.includes(Routes.swap)) {
href = `/${network}/${Routes.transfer}?${searchParams.toString()}`
text = 'Send to a different address?'
}

return (
<Link href={href}>
<span className='text-sm text-grayDarker dark:text-white'>{text}</span>
</Link>
)
}, [network, pathname, searchParams])

return (
<div className='flex flex-col space-y-1'>
Expand All @@ -66,19 +46,15 @@ const DirectionBlock: FC<DirectionBlockProps> = ({ direction, maxAmount }) => {
{capitalizeFirstLetter(direction)}
</span>
<NetworkSelector direction={direction} />
{direction === SwapDirection.TO ? (
<div className='w-1/2 pb-4'>{linkToSwapOrTransfer}</div>
) : (
<div className='w-1/2' />
)}
<div className='w-1/2' />
</div>
{direction === SwapDirection.TO && pathname.includes(Routes.transfer) && <ReceiverField />}
<AmountField maxAmount={maxAmount} disabled={direction === SwapDirection.TO} />
</div>
)
}

export const Swap: FC = () => {
export const Transfer: FC = () => {
const [hash, setHash] = useState<Hash | undefined>(undefined)
const [walletBalance, setWalletBalance] = useState<number>(0)
const { actingAccount, injector, api, domainsApis, subspaceAccount } = useWallet()
Expand Down Expand Up @@ -246,7 +222,7 @@ export const Swap: FC = () => {
break
}
default:
toast.error('Invalid swap', { position: 'bottom-center' })
toast.error('Invalid transfer', { position: 'bottom-center' })
break
}
},
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,14 +13,14 @@ import { useMemo, useState } from 'react'
import { HeaderChainDropdown } from './HeaderChainDropdown'
import { MobileHeader } from './MobileHeader'

export const SwapHeader = () => {
export const TransferHeader = () => {
const { isDark, toggleTheme } = useTheme()
const pathname = usePathname()
const isDesktop = useMediaQuery('(min-width: 1024px)')
const [isOpen, setIsOpen] = useState(false)
const { network } = useIndexers()

const menuList = useMemo(() => getSupportedHeaderLinks(network, Routes.swap), [network])
const menuList = useMemo(() => getSupportedHeaderLinks(network, Routes.transfer), [network])

return (
<header className='body-font z-9 py-[30px] text-gray-600'>
Expand Down
23 changes: 2 additions & 21 deletions explorer/src/constants/routes.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,6 @@ export enum Routes {
domains = 'domains',
autoevm = 'auto-evm',
autoid = 'auto-id',
swap = 'swap',
transfer = 'transfer',
testnetRewards = 'testnet-rewards',
}
Expand Down Expand Up @@ -47,11 +46,6 @@ export enum RoutesDomains {
autoid = '/auto-id',
}

export enum RoutesSwap {
transfer = '/transfer',
swap = '/swap',
}

export type AnyRoutes =
| Routes
| RoutesConsensus
Expand Down Expand Up @@ -170,22 +164,9 @@ export const ROUTES: Route[] = [
],
},
{
name: Routes.swap,
title: 'Transfer / Swap',
name: Routes.transfer,
title: 'Transfer',
networks: [NetworkId.TAURUS],
children: [
{
name: RoutesSwap.swap,
title: 'Swap',
networks: [NetworkId.TAURUS],
},
{
name: RoutesSwap.transfer,
title: 'Transfer',
networks: [NetworkId.TAURUS],
},
],
alias: [Routes.transfer],
},
{
name: Routes.testnetRewards,
Expand Down
1 change: 0 additions & 1 deletion explorer/src/types/app.ts
Original file line number Diff line number Diff line change
Expand Up @@ -42,5 +42,4 @@ export type Route = {
title: string
networks?: NetworkId[]
children?: Route[]
alias?: string[]
}
3 changes: 1 addition & 2 deletions explorer/src/utils/route.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,7 @@ import { NetworkId } from '@autonomys/auto-utils'
import { AnyRoutes, Routes, ROUTES } from 'constants/routes'
import { Route } from 'types/app'

const findRoute = (route: Routes): Route | undefined =>
ROUTES.find((item) => item.name === route || item.alias?.includes(route))
const findRoute = (route: Routes): Route | undefined => ROUTES.find((item) => item.name === route)

const isRouteSupported = (currentNetwork: NetworkId, routeItem: Route): boolean =>
!!currentNetwork &&
Expand Down

0 comments on commit 10889a7

Please sign in to comment.