Skip to content

Commit f5f6595

Browse files
committed
fix: modals sometimes not close after the route changes
1 parent 73b2457 commit f5f6595

File tree

3 files changed

+40
-22
lines changed

3 files changed

+40
-22
lines changed

src/AppContainer.tsx

Lines changed: 3 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,6 @@ import {
1111
HashRouterProps,
1212
} from 'react-router-dom'
1313

14-
import App from './App'
1514
import { ProfileProvider } from './models/profile'
1615

1716
const ReactRouter: React.FC<BrowserRouterProps | HashRouterProps> = (args) => {
@@ -27,16 +26,14 @@ const styleCache = createCache({
2726
key: 'yasd',
2827
})
2928

30-
const AppContainer: React.FC = () => {
29+
const AppContainer: React.FC = ({ children }) => {
3130
return (
32-
<Suspense fallback={<div></div>}>
31+
<Suspense fallback={<div />}>
3332
<CacheProvider value={styleCache}>
3433
<ReactRouter>
3534
<ProfileProvider>
3635
<ThemeProvider theme={light}>
37-
<ModalProvider>
38-
<App />
39-
</ModalProvider>
36+
<ModalProvider>{children}</ModalProvider>
4037
</ThemeProvider>
4138
</ProfileProvider>
4239
</ReactRouter>

src/components/ScrollToTop/index.tsx

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,25 @@
1+
import { useModal } from '@sumup/circuit-ui'
12
import React, { useEffect } from 'react'
23
import { useLocation } from 'react-router-dom'
34

45
const ScrollToTop: React.FC = () => {
56
const { pathname } = useLocation()
7+
const { isModalOpen, removeModal } = useModal()
68

79
useEffect(() => {
810
window.scrollTo(0, 0)
911
}, [pathname])
1012

13+
useEffect(
14+
() => {
15+
if (isModalOpen) {
16+
removeModal()
17+
}
18+
},
19+
// eslint-disable-next-line react-hooks/exhaustive-deps
20+
[pathname],
21+
)
22+
1123
return <></>
1224
}
1325

src/pages/Devices/components/DeviceItem.tsx

Lines changed: 25 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,22 @@ const DeviceItem = ({ device }: { device: DeviceInfo }): JSX.Element => {
2525
const surgeHost = useSurgeHost()
2626
const history = useHistory()
2727

28+
const onDeviceSettingsClick = useCallback(() => {
29+
setModal({
30+
children({ onClose }) {
31+
return onClose && device.dhcpDevice ? (
32+
<DeviceSettingsModal
33+
title={device.dhcpDevice.displayName || device.name}
34+
dhcpDevice={device.dhcpDevice}
35+
onClose={onClose}
36+
/>
37+
) : (
38+
<React.Fragment />
39+
)
40+
},
41+
})
42+
}, [device.dhcpDevice, device.name, setModal])
43+
2844
const onClick = useCallback(() => {
2945
const actions = [
3046
{
@@ -40,21 +56,7 @@ const DeviceItem = ({ device }: { device: DeviceInfo }): JSX.Element => {
4056
actions.push({
4157
id: 'device_settings',
4258
title: 'devices.device_settings',
43-
onClick: () => {
44-
setModal({
45-
children({ onClose }) {
46-
return onClose && device.dhcpDevice ? (
47-
<DeviceSettingsModal
48-
title={device.dhcpDevice.displayName || device.name}
49-
dhcpDevice={device.dhcpDevice}
50-
onClose={onClose}
51-
/>
52-
) : (
53-
<React.Fragment />
54-
)
55-
},
56-
})
57-
},
59+
onClick: onDeviceSettingsClick,
5860
})
5961
}
6062

@@ -71,7 +73,14 @@ const DeviceItem = ({ device }: { device: DeviceInfo }): JSX.Element => {
7173
)
7274
},
7375
})
74-
}, [device, setModal])
76+
}, [
77+
device.dhcpDevice,
78+
device.displayIPAddress,
79+
device.name,
80+
onDeviceSettingsClick,
81+
setModal,
82+
history,
83+
])
7584

7685
const gateway = useMemo<boolean>(() => {
7786
const { hasTCPConnection } = device

0 commit comments

Comments
 (0)