0
Fork 0
mirror of https://github.com/logto-io/logto.git synced 2024-12-16 20:26:19 -05:00

fix(console): close confirm modal on the location path changed (#3981)

This commit is contained in:
Xiao Yijun 2023-06-07 11:29:37 +08:00 committed by GitHub
parent 3acb0d2db1
commit b5b8486105
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
2 changed files with 19 additions and 9 deletions

View file

@ -1,6 +1,6 @@
import type { Nullable } from '@silverhand/essentials';
import { noop } from '@silverhand/essentials';
import { useState, useRef, useMemo, createContext, useCallback } from 'react';
import { useState, useRef, useMemo, createContext, useCallback, useEffect } from 'react';
import ConfirmModal from '@/components/ConfirmModal';
import type { ConfirmModalProps } from '@/components/ConfirmModal';
@ -85,6 +85,22 @@ function AppConfirmModalProvider({ children }: Props) {
[handleCancel, handleConfirm, handleShow]
);
useEffect(() => {
const handlePathChange = () => {
handleCancel();
};
/**
* Note: Close the modal when the location path changes.
* We don't use react-router-dom's `UseLocation` since this is a global component and not relying on the router.
*/
window.addEventListener('popstate', handlePathChange);
return () => {
window.removeEventListener('popstate', handlePathChange);
};
}, [handleCancel]);
const { ModalContent, type, ...restProps } = modalState;
return (

View file

@ -1,7 +1,7 @@
import { withAppInsights } from '@logto/app-insights/react';
import { type HookResponse, type Hook } from '@logto/schemas';
import classNames from 'classnames';
import { useEffect, useState } from 'react';
import { useState } from 'react';
import { toast } from 'react-hot-toast';
import { useTranslation } from 'react-i18next';
import { Outlet, useLocation, useNavigate, useParams } from 'react-router-dom';
@ -48,17 +48,11 @@ function WebhookDetails() {
const theme = useTheme();
const WebhookIcon = theme === 'light' ? Webhook : WebhookDark;
const { show, cancel } = useConfirmModal();
const { show } = useConfirmModal();
const [isDeleting, setIsDeleting] = useState(false);
const [isUpdatingEnableState, setIsUpdatingEnableState] = useState(false);
useEffect(() => {
return () => {
cancel();
};
}, [cancel, pathname]);
const handleDelete = async () => {
if (!data || isDeleting) {
return;