Skip to content

Commit

Permalink
fix: swap page infinite rerender (#1185)
Browse files Browse the repository at this point in the history
  • Loading branch information
chrisling-dev authored Oct 7, 2024
1 parent 315fc70 commit 5c91853
Show file tree
Hide file tree
Showing 3 changed files with 14 additions and 7 deletions.
14 changes: 11 additions & 3 deletions apps/portal/src/domains/common/recoils/api.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import { useSubstrateApiEndpoint } from '..'
import { ApiPromise, WsProvider } from '@polkadot/api'
import * as AvailJsSdk from 'avail-js-sdk'
import { atom, useAtom } from 'jotai'
import { useEffect } from 'react'
import { useEffect, useRef } from 'react'
import { selectorFamily, useRecoilCallback } from 'recoil'

export const substrateApiState = selectorFamily<ApiPromise, string | undefined>({
Expand All @@ -27,9 +27,12 @@ export const substrateApiState = selectorFamily<ApiPromise, string | undefined>(

export const useSubstrateApiState = () => substrateApiState(useSubstrateApiEndpoint())

export const substrateApiGetterAtom = atom<{ getApi: (endpoint: string) => Promise<ApiPromise> } | null>(null)
export const substrateApiGetterAtom = atom<{
getApi: (endpoint: string) => Promise<ApiPromise>
} | null>(null)

export const useSetJotaiSubstrateApiState = () => {
const initRef = useRef(false)
const [substrateApiGetter, setSubstrateApiGetter] = useAtom(substrateApiGetterAtom)

const getSubstrateApi = useRecoilCallback(
Expand All @@ -39,6 +42,11 @@ export const useSetJotaiSubstrateApiState = () => {
)

useEffect(() => {
if (substrateApiGetter === null) setSubstrateApiGetter({ getApi: getSubstrateApi })
if (substrateApiGetter === null && !initRef.current) {
// somehow this causes infinite rerender and substrateApiGetter is always null
setSubstrateApiGetter({ getApi: getSubstrateApi })
// this is a hack to prevent infinite rerender
initRef.current = true
}
}, [getSubstrateApi, setSubstrateApiGetter, substrateApiGetter])
}
4 changes: 2 additions & 2 deletions apps/portal/src/routes/layout.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -270,7 +270,7 @@ const Layout = () => {
<Link to="/staking">
<NavigationBar.Item label="Staking" icon={<Zap />} />
</Link>
<Link to="/transport">
<Link to="/transport/swap">
<NavigationBar.Item label="Swap" icon={<Repeat />} />
</Link>
<Link to="/crowdloans/participated">
Expand All @@ -295,7 +295,7 @@ const Layout = () => {
<Link to="/staking">
<NavigationRail.Item label="Staking" icon={<Zap />} />
</Link>
<Link to="/transport">
<Link to="/transport/swap">
<NavigationRail.Item label="Swap" icon={<Repeat />} />
</Link>
<Link to="/explore">
Expand Down
3 changes: 1 addition & 2 deletions apps/portal/src/routes/transport/index.tsx
Original file line number Diff line number Diff line change
@@ -1,14 +1,13 @@
import Layout from './layout'
import Swap from './swap'
import Xcm from './xcm'
import { Navigate, type RouteObject } from 'react-router-dom'
import { type RouteObject } from 'react-router-dom'

const routes = {
element: <Layout />,
children: [
{ path: 'swap', element: <Swap /> },
{ path: 'xcm', element: <Xcm /> },
{ path: '', element: <Navigate to="swap" /> },
],
} satisfies RouteObject

Expand Down

0 comments on commit 5c91853

Please sign in to comment.