Skip to content

Commit

Permalink
Merge pull request #1145 from oasisprotocol/lw/route-validation
Browse files Browse the repository at this point in the history
Move route address validation from AccountPage into routes
  • Loading branch information
lukaw3d authored Nov 14, 2022
2 parents ccd5676 + 79430aa commit a371410
Show file tree
Hide file tree
Showing 5 changed files with 49 additions and 24 deletions.
5 changes: 3 additions & 2 deletions extension/src/popup/routes.tsx
Original file line number Diff line number Diff line change
@@ -1,10 +1,11 @@
import React from 'react'
import { RouteObject } from 'react-router-dom'
import { App } from 'app'
import { ConnectDevicePage } from 'app/pages/ConnectDevicePage'
import { OpenWalletPageWebExtension } from 'app/pages/OpenWalletPage/webextension'
import { commonRoutes, Route } from '../../../src/commonRoutes'
import { commonRoutes } from '../../../src/commonRoutes'

export const routes: Route[] = [
export const routes: RouteObject[] = [
{
path: '/*',
element: <App />,
Expand Down
29 changes: 29 additions & 0 deletions src/app/components/ErrorBoundary/index.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
import * as React from 'react'
import { isRouteErrorResponse, useRouteError } from 'react-router-dom'
import { ErrorPayload, WalletError, WalletErrors } from 'types/errors'
import { Box } from 'grommet'
import { AlertBox } from 'app/components/AlertBox'
import { ErrorFormatter } from 'app/components/ErrorFormatter'

export function ErrorBoundary() {
const error = useRouteError()

let errorPayload: ErrorPayload
if (isRouteErrorResponse(error)) {
errorPayload = { code: WalletErrors.UnknownError, message: error.statusText }
} else if (error instanceof WalletError) {
errorPayload = { code: error.type, message: error.message }
} else if (error instanceof Error) {
errorPayload = { code: WalletErrors.UnknownError, message: error.message }
} else {
errorPayload = { code: WalletErrors.UnknownError, message: error as string }
}

return (
<Box pad={'small'}>
<AlertBox color="status-error">
<ErrorFormatter code={errorPayload.code} message={errorPayload.message} />
</AlertBox>
</Box>
)
}
16 changes: 5 additions & 11 deletions src/app/pages/AccountPage/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@ import {
import { mobileHeaderZIndex } from '../../components/Sidebar'
import { AccountSummary } from './Features/AccountSummary'
import { isValidAddress } from '../../lib/helpers'
import { WalletError, WalletErrors } from 'types/errors'

const StyledNavItem = styled(NavLink)`
display: flex;
Expand Down Expand Up @@ -87,7 +88,7 @@ interface AccountPageParams {
address: string
}

function AccountPageInternal(props: AccountPageProps) {
export function AccountPage(props: AccountPageProps) {
const { t } = useTranslation()
const isMobile = React.useContext(ResponsiveContext) === 'small'
const { address } = useParams<keyof AccountPageParams>()
Expand Down Expand Up @@ -210,15 +211,8 @@ function AccountPageInternal(props: AccountPageProps) {
)
}

export function AccountPage(props: AccountPageProps) {
const { t } = useTranslation()
const { address } = useParams<keyof AccountPageParams>()
if (!isValidAddress(address!)) {
return (
<Box pad={'small'}>
<AlertBox color={'status-error'}>{t('errors.invalidAddress', 'Invalid address')}</AlertBox>
</Box>
)
export function validateRoute(params: AccountPageParams) {
if (!isValidAddress(params.address!)) {
throw new WalletError(WalletErrors.InvalidAddress, 'Invalid address')
}
return <AccountPageInternal {...props} />
}
18 changes: 9 additions & 9 deletions src/commonRoutes.tsx
Original file line number Diff line number Diff line change
@@ -1,23 +1,19 @@
import React, { ReactNode } from 'react'
import React from 'react'
import { RouteObject } from 'react-router-dom'
import { CreateWalletPage } from 'app/pages/CreateWalletPage'
import { HomePage } from 'app/pages/HomePage'
import { FromLedger } from 'app/pages/OpenWalletPage/Features/FromLedger'
import { FromMnemonic } from 'app/pages/OpenWalletPage/Features/FromMnemonic'
import { FromPrivateKey } from 'app/pages/OpenWalletPage/Features/FromPrivateKey'
import { AccountPage } from 'app/pages/AccountPage'
import { AccountPage, validateRoute } from 'app/pages/AccountPage'
import { AccountDetails } from 'app/pages/AccountPage/Features/AccountDetails'
import { ValidatorList } from 'app/pages/StakingPage/Features/ValidatorList'
import { ActiveDelegationList } from 'app/pages/StakingPage/Features/DelegationList/ActiveDelegationList'
import { DebondingDelegationList } from 'app/pages/StakingPage/Features/DelegationList/DebondingDelegationList'
import { ParaTimes } from 'app/pages/ParaTimesPage'
import { ErrorBoundary } from 'app/components/ErrorBoundary'

export type Route = {
path: string
element: ReactNode
children?: Route[]
}

export const commonRoutes: Route[] = [
export const commonRoutes: RouteObject[] = [
{
path: '',
element: <HomePage />,
Expand All @@ -29,6 +25,10 @@ export const commonRoutes: Route[] = [
{
path: 'account/:address/*',
element: <AccountPage />,
errorElement: <ErrorBoundary></ErrorBoundary>,
loader: async ({ params }) => {
validateRoute(params as any)
},
children: [
{
path: '',
Expand Down
5 changes: 3 additions & 2 deletions src/routes.tsx
Original file line number Diff line number Diff line change
@@ -1,9 +1,10 @@
import React from 'react'
import { RouteObject } from 'react-router-dom'
import { App } from 'app'
import { OpenWalletPage } from 'app/pages/OpenWalletPage'
import { commonRoutes, Route } from './commonRoutes'
import { commonRoutes } from './commonRoutes'

export const routes: Route[] = [
export const routes: RouteObject[] = [
{
path: '/*',
element: <App />,
Expand Down

0 comments on commit a371410

Please sign in to comment.