mirror of
https://github.com/hedgedoc/hedgedoc.git
synced 2024-11-28 17:01:00 -05:00
feat: add replace property to redirect
Signed-off-by: Tilman Vatteroth <git@tilmanvatteroth.de>
This commit is contained in:
parent
6ca0bd0668
commit
1df6eb7bf5
1 changed files with 5 additions and 3 deletions
|
@ -11,6 +11,7 @@ import React, { useEffect } from 'react'
|
|||
|
||||
export interface RedirectProps {
|
||||
to: string
|
||||
replace?: boolean
|
||||
}
|
||||
|
||||
const logger = new Logger('Redirect')
|
||||
|
@ -19,15 +20,16 @@ const logger = new Logger('Redirect')
|
|||
* Redirects the user to another URL. Can be external or internal.
|
||||
*
|
||||
* @param to The target URL
|
||||
* @param replace Defines if the current browser history entry should be replaced or not
|
||||
*/
|
||||
export const Redirect: React.FC<RedirectProps> = ({ to }) => {
|
||||
export const Redirect: React.FC<RedirectProps> = ({ to, replace }) => {
|
||||
const router = useRouter()
|
||||
|
||||
useEffect(() => {
|
||||
router?.push(to).catch((error: Error) => {
|
||||
;(replace ? router.replace(to) : router.push(to)).catch((error: Error) => {
|
||||
logger.error(`Error while redirecting to ${to}`, error)
|
||||
})
|
||||
})
|
||||
}, [replace, router, to])
|
||||
|
||||
return (
|
||||
<span {...testId('redirect')}>
|
||||
|
|
Loading…
Reference in a new issue