Skip to content
This repository has been archived by the owner on Nov 10, 2023. It is now read-only.

fix: Redirect user to next available safe after removing current safe #3424

Merged
merged 4 commits into from
Feb 16, 2022
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
2 changes: 1 addition & 1 deletion src/logic/safe/hooks/useLocalSafes.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ import { ChainId } from 'src/config/chain.d'
import { SafeRecordProps } from '../store/models/safe'
import { getLocalNetworkSafesById } from '../utils'

type LocalSafes = Record<ChainId, SafeRecordProps[]>
export type LocalSafes = Record<ChainId, SafeRecordProps[]>

const getEmptyLocalSafes = (): LocalSafes => {
return getChains().reduce((safes, { chainId }) => ({ ...safes, [chainId]: [] }), {} as LocalSafes)
Expand Down
39 changes: 36 additions & 3 deletions src/routes/safe/components/Settings/RemoveSafeModal/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -11,24 +11,57 @@ import Row from 'src/components/layout/Row'
import PrefixedEthHashInfo from 'src/components/PrefixedEthHashInfo'
import { currentSafeWithNames } from 'src/logic/safe/store/selectors'
import removeSafe from 'src/logic/safe/store/actions/removeSafe'
import { getExplorerInfo } from 'src/config'
import { getChainById, getExplorerInfo } from 'src/config'
import Col from 'src/components/layout/Col'
import { WELCOME_ROUTE, history } from 'src/routes/routes'
import { WELCOME_ROUTE, history, SafeRouteParams, generateSafeRoute, SAFE_ROUTES } from 'src/routes/routes'
import useLocalSafes, { LocalSafes } from 'src/logic/safe/hooks/useLocalSafes'
import { currentChainId } from 'src/logic/config/store/selectors'
import { useMemo } from 'react'
import { SafeRecordProps } from 'src/logic/safe/store/models/safe'

type RemoveSafeModalProps = {
isOpen: boolean
onClose: () => void
}

function getNextAvailableSafe(currentChainId: string, currentSafeAddress: string, localSafes: LocalSafes) {
const availableSafes = Object.values(localSafes)
.flat()
.filter((safe) => safe.address !== currentSafeAddress)
const sameNetworkSafes = availableSafes.filter((safe) => safe.chainId === currentChainId)

if (sameNetworkSafes.length > 0) {
return sameNetworkSafes[0]
}
}

function getDestinationRoute(nextAvailableSafe: SafeRecordProps | undefined) {
if (!nextAvailableSafe || !nextAvailableSafe.chainId) return WELCOME_ROUTE

const { shortName } = getChainById(nextAvailableSafe.chainId)
const routesSlug: SafeRouteParams = {
shortName,
safeAddress: nextAvailableSafe.address,
}
return generateSafeRoute(SAFE_ROUTES.ASSETS_BALANCES, routesSlug)
}

const RemoveSafeModal = ({ isOpen, onClose }: RemoveSafeModalProps): React.ReactElement => {
const classes = useStyles()
const { address: safeAddress, name: safeName } = useSelector(currentSafeWithNames)
const curChainId = useSelector(currentChainId)
const localSafes = useLocalSafes()
const nextAvailableSafe = useMemo(
() => getNextAvailableSafe(curChainId, safeAddress, localSafes),
[curChainId, safeAddress, localSafes],
)
const dispatch = useDispatch()

const onRemoveSafeHandler = async () => {
const destination = getDestinationRoute(nextAvailableSafe)
dispatch(removeSafe(safeAddress))
onClose()
history.push(WELCOME_ROUTE)
history.push(destination)
}

return (
Expand Down