Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix Back to Home button in global error #456

Draft
wants to merge 3 commits into
base: main
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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();
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can something break because we're no longer reloading the page?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Good question. I'll check

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I don't know, tbh.
My logic was: we are updating a prop for ErrorBoundary, so it should be updated after going to home screen. However, I think window.reload can be helpful

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If we're reloading, what does onRetry accomplish?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This looked like GlobalBoundary wasn't updated during this reload properly. I will check again

onRetry?.();
}}
style={{ paddingInline: 8 }}
>
Expand Down
Loading