Skip to content

Commit

Permalink
feat: add error boundary
Browse files Browse the repository at this point in the history
  • Loading branch information
jsun969 committed Oct 6, 2024
1 parent 2c3124e commit 3239e09
Show file tree
Hide file tree
Showing 7 changed files with 69 additions and 9 deletions.
2 changes: 2 additions & 0 deletions .env.example
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
VITE_API_BASE_URL=/mock
VITE_YEAR=2024
VITE_UMAMI_WEBSITE_ID=
VITE_FEEDBACK_FORM_URL=
VITE_FEEDBACK_FORM_URL_PREFILL_ERROR_MESSAGE=
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@
"mutative": "^1.0.8",
"react": "^18.3.1",
"react-dom": "^18.3.1",
"react-error-boundary": "^4.0.13",
"react-i18next": "^15.0.2",
"simple-zustand-devtools": "^1.1.0",
"sonner": "^1.5.0",
Expand Down
15 changes: 14 additions & 1 deletion pnpm-lock.yaml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

35 changes: 35 additions & 0 deletions src/components/Error.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
import { Code, Link } from '@nextui-org/react';
import type { FallbackProps } from 'react-error-boundary';

import { useMount } from '../utils/mount';

export const Error = ({ error }: FallbackProps) => {
const errorMessage = String(error);
const prefilledFeedbackForm = `${import.meta.env.VITE_FEEDBACK_FORM_URL_PREFILL_ERROR_MESSAGE}+${errorMessage}%0ACause:+`;

useMount(() => {
// Clear local data to reset the app
localStorage.clear();
});

return (
<div className="mx-2 flex h-dvh flex-col items-center justify-center gap-2">
<h1 className="text-4xl">Oops.. Something went wrong!</h1>
<p>
We're sorry, but we need to clear all saved data for this website
(including any locally stored courses and times).
</p>
<Code size="lg" className="max-w-full overflow-x-auto p-2">
<span className="font-bold">Error Message:</span> <br />
{errorMessage}
</Code>
<p>
Before refreshing the page, would you like to{' '}
<Link isExternal href={prefilledFeedbackForm}>
send us feedback
</Link>{' '}
along with the error message to help improve our app?
</p>
</div>
);
};
2 changes: 1 addition & 1 deletion src/components/Header.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@ export const Header = () => {
<Button
{...HEADER_BUTTON_PROPS}
as={Link}
href="https://forms.gle/4HFumRNK8jtEhxJz6"
href={import.meta.env.VITE_FEEDBACK_FORM_URL}
>
🗣
</Button>
Expand Down
21 changes: 14 additions & 7 deletions src/main.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,12 @@ import { QueryClientProvider } from '@tanstack/react-query';
import { ReactQueryDevtools } from '@tanstack/react-query-devtools';
import React from 'react';
import ReactDOM from 'react-dom/client';
import { ErrorBoundary } from 'react-error-boundary';
import { mountStoreDevtool } from 'simple-zustand-devtools';
import { Toaster } from 'sonner';

import { App } from './App';
import { Error } from './components/Error';
import { useEnrolledCourses } from './data/enrolled-courses';
import './i18n';
import './index.css';
Expand Down Expand Up @@ -35,12 +37,17 @@ if (import.meta.env.DEV) {

ReactDOM.createRoot(document.getElementById('root')!).render(
<React.StrictMode>
<QueryClientProvider client={queryClient}>
<ReactQueryDevtools initialIsOpen={false} buttonPosition="bottom-left" />
<NextUIProvider>
<Toaster richColors position="top-center" />
<App />
</NextUIProvider>
</QueryClientProvider>
<ErrorBoundary FallbackComponent={Error}>
<QueryClientProvider client={queryClient}>
<ReactQueryDevtools
initialIsOpen={false}
buttonPosition="bottom-left"
/>
<NextUIProvider>
<Toaster richColors position="top-center" />
<App />
</NextUIProvider>
</QueryClientProvider>
</ErrorBoundary>
</React.StrictMode>,
);
2 changes: 2 additions & 0 deletions src/vite-env.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@
interface ImportMetaEnv {
readonly VITE_API_BASE_URL: string;
readonly VITE_YEAR: string;
readonly VITE_FEEDBACK_FORM_URL: string;
readonly VITE_FEEDBACK_FORM_URL_PREFILL_ERROR_MESSAGE: string;
}

interface ImportMeta {
Expand Down

0 comments on commit 3239e09

Please sign in to comment.