Skip to content

Commit

Permalink
bump react router and fix issues
Browse files Browse the repository at this point in the history
  • Loading branch information
Tbaut committed Dec 3, 2024
1 parent afc2ecb commit eeae06f
Show file tree
Hide file tree
Showing 4 changed files with 41 additions and 48 deletions.
3 changes: 2 additions & 1 deletion packages/ui/src/contexts/MultiProxyContext.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -77,6 +77,7 @@ const MultiProxyContextProvider = ({ children }: MultisigContextProps) => {
}, [multisigList, pureProxyList])
const { ownAddressList } = useAccounts()
const { watchedAddresses } = useWatchedAddresses()

const getMultiProxyByAddress = useCallback(
(address?: string) => {
if (!address) return undefined
Expand Down Expand Up @@ -118,6 +119,7 @@ const MultiProxyContextProvider = ({ children }: MultisigContextProps) => {
() => isWatchedAccount(selectedMultiProxy),
[isWatchedAccount, selectedMultiProxy]
)

const [, setSearchParams] = useSearchParams({
address: ''
})
Expand All @@ -126,7 +128,6 @@ const MultiProxyContextProvider = ({ children }: MultisigContextProps) => {
setMultisigList(null)
setPureProxyList(null)
}, [])

const setAddressInUrl = useCallback(
(address: string) => {
setSearchParams((prev) => {
Expand Down
56 changes: 16 additions & 40 deletions packages/ui/src/hooks/useSwitchAddress.tsx
Original file line number Diff line number Diff line change
@@ -1,15 +1,16 @@
import { useCallback, useEffect, useMemo } from 'react'
import { useEffect, useMemo } from 'react'
import { useSearchParams } from 'react-router-dom'
import { useMultiProxy } from '../contexts/MultiProxyContext'

export const useSwitchAddress = () => {
const [searchParams, setSearchParams] = useSearchParams({
const [searchParams] = useSearchParams({
address: '',
network: ''
})
const urlAddress = useMemo(() => {
return searchParams.get('address') || ''
}, [searchParams])

const {
multiProxyList,
isLoading: isMultiproxyLoading,
Expand All @@ -19,59 +20,34 @@ export const useSwitchAddress = () => {
setCanFindMultiProxyFromUrl
} = useMultiProxy()

const setAddress = useCallback(
(address: string) => {
setSearchParams((prev) => {
prev.set('address', address)
return prev
})
},
[setSearchParams]
)

useEffect(() => {
if (isMultiproxyLoading) {
// we're not yet initialized
return
}

if (!urlAddress && !!defaultAddress) {
// no address in the url, init with the first multiProxy from the list
setAddress(defaultAddress)
return
}
}, [
defaultAddress,
isMultiproxyLoading,
multiProxyList,
setAddress,
setCanFindMultiProxyFromUrl,
urlAddress
])
if (!!urlAddress && !selectedMultiProxyAddress) {
// this looks like a first load with an address
const isSuccess = selectMultiProxy(urlAddress)
setCanFindMultiProxyFromUrl(isSuccess)

// the url address is driving the UI if there's a mismatch
// force the url address to match
useEffect(() => {
if (isMultiproxyLoading) {
// we're not yet initialized
return
}

if (!urlAddress || urlAddress === selectedMultiProxyAddress) {
setCanFindMultiProxyFromUrl(true)
}

if (!!urlAddress && urlAddress !== selectedMultiProxyAddress) {
const isSuccess = selectMultiProxy(urlAddress)
if (!urlAddress && !!defaultAddress) {
// no address in the url, init with the default
const isSuccess = selectMultiProxy(defaultAddress)
setCanFindMultiProxyFromUrl(isSuccess)

return
}
}, [
urlAddress,
defaultAddress,
isMultiproxyLoading,
multiProxyList,
selectMultiProxy,
defaultAddress,
selectedMultiProxyAddress,
isMultiproxyLoading,
setCanFindMultiProxyFromUrl
setCanFindMultiProxyFromUrl,
urlAddress
])
}
7 changes: 6 additions & 1 deletion packages/ui/src/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,11 @@ const root = createRoot(container!)

root.render(
<StrictMode>
<RouterProvider router={router} />
<RouterProvider
router={router}
future={{
v7_startTransition: true
}}
/>
</StrictMode>
)
23 changes: 17 additions & 6 deletions packages/ui/src/pages/routes.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -44,11 +44,22 @@ export const ROUTES: Route[] = [
}
]

export const router = createBrowserRouter([
export const router = createBrowserRouter(
[
{
path: '/',
element: <App />,
errorElement: <ErrorFallback />,
children: ROUTES
}
],
{
path: '/',
element: <App />,
errorElement: <ErrorFallback />,
children: ROUTES
future: {
v7_relativeSplatPath: true,
v7_fetcherPersist: true,
v7_normalizeFormMethod: true,
v7_partialHydration: true,
v7_skipActionErrorRevalidation: true
}
}
])
)

0 comments on commit eeae06f

Please sign in to comment.