From 220552925999be7ad5100ca2d131b5884d66c7e5 Mon Sep 17 00:00:00 2001 From: Giuliano Caregnato Date: Wed, 16 Mar 2022 11:56:56 +0100 Subject: [PATCH] fix: fixed nav-guard behaviour around settings subsection --- src/ui-extras/nav-guard.tsx | 10 +++++++--- 1 file changed, 7 insertions(+), 3 deletions(-) diff --git a/src/ui-extras/nav-guard.tsx b/src/ui-extras/nav-guard.tsx index c83cf4bc..d85770d5 100644 --- a/src/ui-extras/nav-guard.tsx +++ b/src/ui-extras/nav-guard.tsx @@ -4,7 +4,7 @@ * SPDX-License-Identifier: AGPL-3.0-only */ -import React, { useEffect, useState, FC } from 'react'; +import React, { useEffect, useState, FC, useMemo } from 'react'; import { Location } from 'history'; import { Prompt, useHistory } from 'react-router-dom'; import { Modal } from '@zextras/carbonio-design-system'; @@ -15,8 +15,9 @@ export const RouteLeavingGuard: FC<{ onSave: () => void; }> = ({ children, when, onSave }) => { const history = useHistory(); + const lastLocationInitial = useMemo(() => history.location.pathname, [history]); const [modalVisible, setModalVisible] = useState(false); - const [lastLocation, setLastLocation] = useState(null); + const [lastLocation, setLastLocation] = useState(lastLocationInitial); const [confirmedNavigation, setConfirmedNavigation] = useState(false); const [t] = useTranslation(); const onClose = (): void => { @@ -24,7 +25,10 @@ export const RouteLeavingGuard: FC<{ setConfirmedNavigation(true); }; const handleBlockedNavigation = (nextLocation: Location): boolean => { - if (!confirmedNavigation) { + if ( + !confirmedNavigation && + nextLocation.pathname !== (lastLocation?.pathname || lastLocationInitial) + ) { setModalVisible(true); setLastLocation(nextLocation); return false;