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

refactor: change how to represent modal routes #11628

Merged
merged 2 commits into from
May 16, 2024
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
11 changes: 5 additions & 6 deletions packages/mask/popups/Popup.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ import { ProviderType } from '@masknet/web3-shared-evm'
import { Box } from '@mui/material'
import { Suspense, lazy, memo, useEffect, useMemo, useState, type ReactNode } from 'react'
import { useIdleTimer } from 'react-idle-timer'
import { HashRouter, Navigate, Route, Routes, useLocation, useNavigate } from 'react-router-dom'
import { HashRouter, Navigate, Route, Routes, useNavigate, useSearchParams } from 'react-router-dom'
import { usePopupTheme } from './hooks/usePopupTheme.js'
import Services from '#services'
import { LoadingPlaceholder } from './components/LoadingPlaceholder/index.js'
Expand Down Expand Up @@ -49,8 +49,7 @@ const personaInitialState = {
queryPersonaAvatar: Services.Identity.getPersonaAvatar,
}
const PopupRoutes = memo(function PopupRoutes() {
const location = useLocation()
const mainLocation = location.state?.mainLocation as Location | undefined
const [searchParams] = useSearchParams()

return (
<Suspense
Expand All @@ -62,7 +61,7 @@ const PopupRoutes = memo(function PopupRoutes() {
<PersonaContext.Provider initialState={personaInitialState}>
<UserContext.Provider>
<RouterListener />
<Routes location={mainLocation || location}>
<Routes>
<Route path="/" element={<PopupLayout />}>
<Route path={PopupPaths.Personas + '/*'} element={withSuspense(<Personas />)} />
<Route path={PopupPaths.Wallet + '/*'} element={withSuspense(<Wallet />)} />
Expand All @@ -73,8 +72,8 @@ const PopupRoutes = memo(function PopupRoutes() {
<Route path={PopupPaths.RequestPermission} element={<RequestPermissionPage />} />
<Route path="*" element={<Navigate replace to={PopupPaths.Personas} />} />
</Routes>
{mainLocation ?
<Routes>
{searchParams.get('modal') ?
<Routes location={searchParams.get('modal') || ''}>
<Route
path={PopupModalRoutes.verifyBackupPassword}
element={wrapModal(<VerifyBackupPasswordModal />)}
Expand Down
19 changes: 8 additions & 11 deletions packages/mask/popups/components/ActionModal/ActionModalContext.tsx
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { useTheme } from '@mui/material'
import { useCallback, useRef, useState } from 'react'
import { useNavigate, useLocation, type NavigateOptions } from 'react-router-dom'
import { useNavigate, useLocation, useSearchParams } from 'react-router-dom'
import type { PopupModalRoutes } from '@masknet/shared-base'
import { createContainer } from 'unstated-next'
import urlcat from 'urlcat'
Expand Down Expand Up @@ -39,22 +39,19 @@ export function useActionModal() {
}

/**
* To open a modal, should navigate with setting state `{ mainLocation: location }`
* Open a modal
*/
export function useModalNavigate() {
const location = useLocation()
const navigate = useNavigate()
const [, setSearchParams] = useSearchParams()
const openModal = useCallback(
(path: PopupModalRoutes, params?: Record<string, any>, options?: NavigateOptions) => {
navigate(urlcat(path, params ?? {}), {
...options,
state: {
...options?.state,
mainLocation: location.state?.mainLocation ?? location,
},
(path: PopupModalRoutes, params?: Record<string, any>) => {
setSearchParams((prev) => {
prev.set('modal', urlcat(path, params || {}))
return prev
})
},
[location, navigate],
[location, setSearchParams],
)
return openModal
}
5 changes: 1 addition & 4 deletions packages/mask/popups/components/NFTAvatarPicker/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -74,10 +74,7 @@ export const NFTAvatarPicker = memo<NFTAvatarPickerProps>(function NFTAvatarPick

const tokens = useMemo(() => uniqBy(assets, (x) => x.contract?.address.toLowerCase() + x.tokenId), [assets])

const handleChangeWallet = useCallback(
() => modalNavigate(PopupModalRoutes.SelectProvider, { onlyMask: true }, { replace: true }),
[],
)
const handleChangeWallet = useCallback(() => modalNavigate(PopupModalRoutes.SelectProvider, { onlyMask: true }), [])

const handleChange = useCallback((address: string, pluginID: NetworkPluginID, chainId: Web3Helper.ChainIdAll) => {
setAccount(address)
Expand Down
10 changes: 1 addition & 9 deletions packages/mask/popups/components/SelectProvider/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -74,15 +74,7 @@ export const SelectProvider = memo(function SelectProvider() {
} else {
const currentPopupWindowId = await Services.Helper.queryCurrentPopupWindowId()
if (currentPopupWindowId) {
modalNavigate(
PopupModalRoutes.ConnectProvider,
{
providerType,
},
{
replace: true,
},
)
modalNavigate(PopupModalRoutes.ConnectProvider, { providerType })
return
}

Expand Down
6 changes: 2 additions & 4 deletions packages/mask/popups/pages/Personas/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -27,10 +27,8 @@ const Persona = memo(() => {
return CrossIsolationMessages.events.popupWalletConnectEvent.on(({ open, uri }) => {
if (!open || location.href.includes(PopupRoutes.WalletConnect)) return
navigate(PopupRoutes.WalletConnect, {
replace: location.hash.includes('/modal/select-provider'),
state: {
uri,
},
replace: params.get('modal')?.includes(PopupModalRoutes.SelectProvider),
state: { uri },
})
})
})
Expand Down
32 changes: 16 additions & 16 deletions packages/shared-base/src/types/Routes.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,22 +20,22 @@ export enum DashboardRoutes {
}

export enum PopupModalRoutes {
ChooseCurrency = '/modal/choose-currency',
ChooseNetwork = '/modal/choose-network',
SwitchWallet = '/modal/switch-wallet',
ConnectSocialAccount = '/modal/connect-social-account',
SelectProvider = '/modal/select-provider',
ConnectProvider = '/modal/connect-provider',
SwitchPersona = '/modal/switch-persona',
PersonaSettings = '/modal/persona-setting',
PersonaRename = '/modal/persona-rename',
SetBackupPassword = '/modal/set-backup-password',
verifyBackupPassword = '/modal/verify-backup-password',
WalletAccount = '/modal/wallet-accounts',
SelectLanguage = '/modal/select-language',
SelectAppearance = '/modal/select-appearance',
SupportedSitesModal = '/modal/supported-sites',
ChangeBackupPassword = '/modal/change-backup-password',
ChooseCurrency = '/choose-currency',
ChooseNetwork = '/choose-network',
SwitchWallet = '/switch-wallet',
ConnectSocialAccount = '/connect-social-account',
SelectProvider = '/select-provider',
ConnectProvider = '/connect-provider',
SwitchPersona = '/switch-persona',
PersonaSettings = '/persona-setting',
PersonaRename = '/persona-rename',
SetBackupPassword = '/set-backup-password',
verifyBackupPassword = '/verify-backup-password',
WalletAccount = '/wallet-accounts',
SelectLanguage = '/select-language',
SelectAppearance = '/select-appearance',
SupportedSitesModal = '/supported-sites',
ChangeBackupPassword = '/change-backup-password',
}

export enum PopupRoutes {
Expand Down
4 changes: 2 additions & 2 deletions packages/web3-hooks/base/src/useContext.tsx
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { createContext, useContext, useState, useMemo, memo, type PropsWithChildren } from 'react'
import { isUndefined, omitBy } from 'lodash-es'
import { usePersistSubscription, useValueRef } from '@masknet/shared-base-ui'
import { compose, Sniffings, NetworkPluginID, getSiteType, pluginIDsSettings } from '@masknet/shared-base'
import { compose, Sniffings, NetworkPluginID, getSiteType, pluginIDsSettings, PopupRoutes } from '@masknet/shared-base'
import { MaskWalletProvider } from '@masknet/web3-providers'
import { ProviderType } from '@masknet/web3-shared-evm'
import type { Web3Helper } from '@masknet/web3-helpers'
Expand Down Expand Up @@ -78,7 +78,7 @@ export const ChainContextProvider = memo(function ChainContextProvider(props: Pr
const [_providerType, setProviderType] = useState<Web3Helper.ProviderTypeAll>()

const location = useLocation()
const is_popup_wallet_page = Sniffings.is_popup_page && location.hash?.includes('/wallet')
const is_popup_wallet_page = Sniffings.is_popup_page && location.hash?.includes(PopupRoutes.Wallet)
const account =
controlled ? props.account : _account ?? props.account ?? (is_popup_wallet_page ? maskAccount : globalAccount)
const chainId =
Expand Down
Loading