jellyfish-web/src/components/router/Redirect.tsx

29 lines
565 B
TypeScript
Raw Normal View History

2023-09-15 10:31:48 -04:00
import React from 'react';
2023-09-20 00:04:08 -04:00
import { Navigate, Route, useLocation } from 'react-router-dom';
2023-09-15 10:31:48 -04:00
export interface Redirect {
from: string
to: string
}
2023-09-20 00:04:08 -04:00
const RedirectWithSearch = ({ to }: { to: string }) => {
const { search } = useLocation();
return (
<Navigate
replace
to={`${to}${search}`}
/>
);
};
2023-09-15 10:31:48 -04:00
export function toRedirectRoute({ from, to }: Redirect) {
return (
<Route
key={from}
path={from}
2023-09-20 00:04:08 -04:00
element={<RedirectWithSearch to={to} />}
2023-09-15 10:31:48 -04:00
/>
);
}