Skip to content

Commit

Permalink
Fix Back to Home button in global error
Browse files Browse the repository at this point in the history
  • Loading branch information
zerts committed May 8, 2024
1 parent e5b8dd8 commit 20ce09f
Show file tree
Hide file tree
Showing 3 changed files with 21 additions and 3 deletions.
13 changes: 11 additions & 2 deletions src/ui/App/App.tsx
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import React, { useMemo } from 'react';
import React, { useMemo, useState } from 'react';
import { AreaProvider } from 'react-area';
import { QueryClientProvider, useQuery } from '@tanstack/react-query';
import {
Expand Down Expand Up @@ -398,6 +398,7 @@ export interface AppProps {
}

export function App({ initialView, mode, inspect }: AppProps) {
const [retryKey, setRetryKey] = useState(0);
const bodyClassList = useMemo(() => {
const result = [];
if (pageTemplateType === 'dialog') {
Expand All @@ -423,7 +424,15 @@ export function App({ initialView, mode, inspect }: AppProps) {
<QueryClientProvider client={queryClient}>
<DesignTheme bodyClassList={bodyClassList} />
<Router>
<ErrorBoundary renderError={(error) => <ViewError error={error} />}>
<ErrorBoundary
retryKey={retryKey}
renderError={(error) => (
<ViewError
error={error}
onRetry={() => setRetryKey((count) => count + 1)}
/>
)}
>
<InactivityDetector />
<SessionResetHandler />
<ThemeDecoration />
Expand Down
7 changes: 7 additions & 0 deletions src/ui/components/ErrorBoundary/ErrorBoundary.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import { getError } from 'src/shared/errors/getError';
export class ErrorBoundary extends React.Component<
React.PropsWithChildren<{
forceIsErrorForTesting?: boolean;
retryKey?: number;
renderError: (
error?: (Error & { code?: number }) | null
) => React.ReactNode;
Expand All @@ -15,6 +16,12 @@ export class ErrorBoundary extends React.Component<
return { hasError: true, error: getError(error) };
}

componentDidUpdate(prevProps: Readonly<{ retryKey?: number | undefined }>) {
if (prevProps.retryKey !== this.props.retryKey) {
this.setState({ hasError: false, error: null });
}
}

render() {
if (this.state.hasError || this.props.forceIsErrorForTesting) {
return this.props.renderError(this.state.error);
Expand Down
4 changes: 3 additions & 1 deletion src/ui/components/ViewError/ViewError.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -30,10 +30,12 @@ export function ViewError({
title = 'Unable to perform this action right now',
subtitle = 'Please try again and report the issue if it persists.',
error,
onRetry,
}: {
title?: string;
subtitle?: string | null;
error?: Error | null;
onRetry?: () => void;
}) {
const navigate = useNavigate();
const { pathname, search } = useLocation();
Expand Down Expand Up @@ -131,7 +133,7 @@ export function ViewError({
if (pageTemplateType !== 'dialog') {
navigate('/');
}
window.location.reload();
onRetry?.();
}}
style={{ paddingInline: 8 }}
>
Expand Down

0 comments on commit 20ce09f

Please sign in to comment.