Skip to content

Commit 43617e7

Browse files
fix: improve error handling and re-routing (#607)
* fix: improve error handling and re-routing * fix: improve global error clearing logic --------- Co-authored-by: svcAPLBot <174728082+svcAPLBot@users.noreply.github.com>
1 parent ebb7c52 commit 43617e7

File tree

5 files changed

+41
-12
lines changed

5 files changed

+41
-12
lines changed

src/components/Error.tsx

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -93,7 +93,11 @@ export default function ({ error }: Props): React.ReactElement {
9393
}
9494

9595
return (
96-
<Button variant='contained' color='primary' onClick={() => (globalError ? clearError() : window.history.back())}>
96+
<Button
97+
variant='contained'
98+
color='primary'
99+
onClick={() => (globalError.status === 409 ? clearError() : window.history.back())}
100+
>
97101
{t('Back', { ns: 'error' })}
98102
</Button>
99103
)

src/components/Header.tsx

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ import { useHistory, useLocation } from 'react-router-dom'
88
import { useGetTeamsQuery } from 'redux/otomiApi'
99
import useSettings from 'hooks/useSettings'
1010
import React from 'react'
11+
import { useLocalStorage } from 'hooks/useLocalStorage'
1112
import AccountPopover from './AccountPopover'
1213
import { IconButtonAnimate } from './animate'
1314
import Iconify from './Iconify'
@@ -62,9 +63,11 @@ export default function Header({ onOpenSidebar, isCollapse = false, verticalLayo
6263
const { pathname } = useLocation()
6364
const {
6465
user: { email, teams: userTeams, isPlatformAdmin },
65-
oboTeamId,
66+
oboTeamId: sessionOboTeamId,
6667
setOboTeamId,
6768
} = useSession()
69+
const [localOboTeamId] = useLocalStorage<string>('oboTeamId', undefined)
70+
const oboTeamId = sessionOboTeamId || localOboTeamId || undefined
6871
const { data: allTeams } = useGetTeamsQuery(!isPlatformAdmin && skipToken)
6972
// END HOOKs
7073
let teams: string[] = []
@@ -108,6 +111,7 @@ export default function Header({ onOpenSidebar, isCollapse = false, verticalLayo
108111
history.push(nextPathname)
109112
event.preventDefault()
110113
}
114+
if (!teams && oboTeamId) teams = [oboTeamId]
111115

112116
return (
113117
<RootStyle

src/components/NavConfig.tsx

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ import generateDownloadLink from 'generate-download-link'
22
import SvgIconStyle from 'components/SvgIconStyle'
33
import { useSession } from 'providers/Session'
44
import { canDo } from 'utils/permission'
5+
import { useLocalStorage } from 'hooks/useLocalStorage'
56

67
const getIcon = (name: string) => <SvgIconStyle src={`/assets/${name}`} sx={{ width: 1, height: 1 }} />
78

@@ -10,7 +11,9 @@ const getIcon = (name: string) => <SvgIconStyle src={`/assets/${name}`} sx={{ wi
1011
// it's SVG format.
1112

1213
export default function NavConfig() {
13-
const { ca, appsEnabled, oboTeamId, user, settings } = useSession()
14+
const { ca, appsEnabled, oboTeamId: sessionOboTeamId, user, settings } = useSession()
15+
const [localOboTeamId] = useLocalStorage<string>('oboTeamId', undefined)
16+
const oboTeamId = sessionOboTeamId || localOboTeamId || undefined
1417
const hasExternalIDP = settings?.otomi?.hasExternalIDP ?? false
1518
const isManaged = settings?.otomi?.isPreInstalled ?? false
1619
const hasApiServerConfigured = settings?.cluster?.apiServer ?? false

src/layouts/Paper.tsx

Lines changed: 6 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -21,20 +21,18 @@ export default function ({ loading, comp, title, children }: Props): React.React
2121
const dashboardStyle =
2222
location.pathname === '/' ? { backgroundColor: 'background.contrast' } : { backgroundColor: 'transparent' }
2323
const dispatch = useAppDispatch()
24-
const error = useAppSelector(({ global: { error } }) => error)
24+
const globalError = useAppSelector(({ global: { error } }) => error)
2525
useEffect(() => {
26-
return () => {
27-
// clear 409 Conflict error when navigating away from the page to prevent it from reappearing
28-
if (error && error.status === 409) dispatch(setError(undefined))
29-
}
30-
}, [error])
26+
// clear global error when pathname changes to prevent the error from reappearing
27+
if (globalError) dispatch(setError(undefined))
28+
}, [location.pathname])
3129
return (
3230
<MainLayout title={title}>
3331
<Container maxWidth='lg'>
3432
<Card sx={{ ...dashboardStyle }}>
3533
<Error />
36-
{loading && <LoadingScreen />}
37-
<Box sx={{ display: error && 'none' }}>
34+
{loading && !globalError && <LoadingScreen />}
35+
<Box sx={{ display: globalError && 'none' }}>
3836
{!loading && comp}
3937
{children}
4038
</Box>

src/pages/Error.tsx

Lines changed: 21 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,34 @@
11
import { Card, Container } from '@mui/material'
22
import Error from 'components/Error'
33
import MainLayout from 'layouts/Base'
4-
import React from 'react'
4+
import React, { useEffect, useState } from 'react'
5+
import { useLocation } from 'react-router-dom'
6+
import { useAppDispatch } from 'redux/hooks'
7+
import { setError } from 'redux/reducers'
58
import { HttpError } from 'utils/error'
69

710
interface Props {
811
error?: HttpError
912
}
1013

1114
export default function ({ error }: Props): React.ReactElement {
15+
const [prevPath, setPrevPath] = useState<string | undefined>(undefined)
16+
const location = useLocation()
17+
const dispatch = useAppDispatch()
18+
19+
useEffect(() => {
20+
if (error) setPrevPath(location.pathname)
21+
}, [error])
22+
23+
useEffect(() => {
24+
// clear the error when navigating to a different page
25+
// reload the page to prevent the error from reappearing
26+
if (prevPath && prevPath !== location.pathname) {
27+
dispatch(setError(undefined))
28+
window.location.reload()
29+
}
30+
}, [location.pathname])
31+
1232
return (
1333
<MainLayout title='Error Boundary'>
1434
<Container maxWidth='lg'>

0 commit comments

Comments
 (0)