-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Redirect old routes to new ones (#84)
* Redirect old routes to new ones closes #83 * Minor import path changes
- Loading branch information
1 parent
fba8304
commit 7f1dd2d
Showing
3 changed files
with
73 additions
and
6 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
7f1dd2d
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
π Published on https://vocdoni-explorer-dev.netlify.app as production
π Deployed on https://66aa1c3136b5a01aeb0766f8--vocdoni-explorer-dev.netlify.app
7f1dd2d
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
π Published on https://vocdoni-explorer-stg.netlify.app as production
π Deployed on https://66aa1c40d60a9c1553b4b63b--vocdoni-explorer-stg.netlify.app