Skip to content

Commit

Permalink
Redirect old routes to new ones
Browse files Browse the repository at this point in the history
closes #83
  • Loading branch information
elboletaire committed Jul 31, 2024
1 parent fba8304 commit ca83efc
Show file tree
Hide file tree
Showing 3 changed files with 68 additions and 1 deletion.
11 changes: 11 additions & 0 deletions src/constants.ts
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,17 @@ export enum RoutePath {
Verify = '/verify/:verifier?',
}

// old explorer route paths (used by RouteRedirector)
export enum OldRoutePath {
BlockDetails = '/blocks/show/#/',
EnvelopeDetails = '/envelopes/show/#/',
OrganizationDetails = '/organizations/show/#/',
ProcessDetails = '/processes/show/#/',
TransactionDetails = '/transactions/show/#/',
Verify = '/verify',
Stats = '/stats',
}

// Used to test if a string is base64 encoded. Used by b64ToHex
export const isB64Regex = /^([0-9a-zA-Z+/]{4})*(([0-9a-zA-Z+/]{2}==)|([0-9a-zA-Z+/]{3}=))?$/

Expand Down
54 changes: 54 additions & 0 deletions src/router/RouteRedirector.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
import { useEffect } from 'react'
import { Outlet, useLocation, useNavigate } from 'react-router-dom'
import { OldRoutePath, RoutePath } from '~constants'

const mapOldRouteToNewRoute = (route: string): string => {
const routeMappings: { [key: string]: string } = {
[OldRoutePath.BlockDetails]: RoutePath.Block,
[OldRoutePath.EnvelopeDetails]: RoutePath.Envelope,
[OldRoutePath.OrganizationDetails]: RoutePath.Organization,
[OldRoutePath.ProcessDetails]: RoutePath.Process,
[OldRoutePath.TransactionDetails]: RoutePath.Transaction,
[OldRoutePath.Verify]: RoutePath.Verify,
[OldRoutePath.Stats]: RoutePath.Base,
}

for (const oldPath in routeMappings) {
if (route.startsWith(oldPath)) {
const newPath = routeMappings[oldPath]

if (!newPath) return '' // fallback to undefined route

const params = route.replace(oldPath, '').split('/').filter(Boolean)
let paramIndex = 0

const finalRoute = newPath.replace(/:(\w+)\??/g, (match) => {
const value = params[paramIndex]
paramIndex++
return value || ''
})

return finalRoute
}
}

return '' // fallback to undefined route
}

const RouteRedirector = () => {
const navigate = useNavigate()
const location = useLocation()

useEffect(() => {
const oldRoute = location.pathname + location.hash
const newRoute = mapOldRouteToNewRoute(oldRoute)

if (newRoute && newRoute !== oldRoute) {
navigate(newRoute, { replace: true })
}
}, [location, navigate])

return <Outlet />
}

export default RouteRedirector
4 changes: 3 additions & 1 deletion src/router/index.tsx
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import { ExtendedSDKClient } from '@vocdoni/extended-sdk'
import { useClient } from '@vocdoni/react-providers'
import { IChainTxReference } from '@vocdoni/sdk'
import { lazy } from 'react'
import { createBrowserRouter, RouteObject, RouterProvider } from 'react-router-dom'
import { RoutePath } from '~constants'
Expand All @@ -9,7 +10,7 @@ import { ElectionError } from '~src/router/errors/ElectionError'
import Error404 from '~src/router/errors/Error404'
import RouteError from '~src/router/errors/RouteError'
import { SuspenseLoader } from '~src/router/SuspenseLoader'
import { IChainTxReference } from '@vocdoni/sdk'
import RouteRedirector from './RouteRedirector'

const Home = lazy(() => import('~pages/Home'))
const Block = lazy(() => import('~pages/block'))
Expand All @@ -32,6 +33,7 @@ export const RoutesProvider = () => {
element: <Layout />,
children: [
{
element: <RouteRedirector />,
errorElement: <RouteError />,
children: [
{
Expand Down

0 comments on commit ca83efc

Please sign in to comment.