Skip to content

Commit

Permalink
refactor: error boundary
Browse files Browse the repository at this point in the history
  • Loading branch information
ikkz committed Dec 19, 2024
1 parent 894cf98 commit 101e6c9
Show file tree
Hide file tree
Showing 4 changed files with 34 additions and 43 deletions.
31 changes: 0 additions & 31 deletions src/components/card-container.tsx

This file was deleted.

8 changes: 4 additions & 4 deletions src/components/card-shell.tsx
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
import { About } from './about';
import { Block } from './block';
import { CardContainer } from './card-container';
import { Dot } from './dot';
import { AnkiField } from './field';
import { SelectionMenu } from './selection-menu';
Expand Down Expand Up @@ -47,9 +46,10 @@ export const CardShell: FC<Props> = ({
const [showSettings, setShowSettings] = useState(false);

return (
<CardContainer
<div
className={clsx(
'tappable m-auto w-full max-w-xl px-5 py-7 font-sans text-base lg:max-w-3xl',
'tappable m-auto px-5 py-7 font-sans text-base',
'w-full max-w-xl lg:max-w-3xl',
`locale-${locale}`,
)}
>
Expand Down Expand Up @@ -114,6 +114,6 @@ export const CardShell: FC<Props> = ({
</Block>
)}
</div>
</CardContainer>
</div>
);
};
25 changes: 22 additions & 3 deletions src/entries/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -4,14 +4,17 @@ import { APP_CONTAINER_ID } from '@/utils/const';
import { Provider } from 'jotai';
import { render } from 'preact';
import { ComponentType } from 'react';
import { ErrorBoundary, FallbackProps } from 'react-error-boundary';

let container: HTMLElement | null = document.getElementById(APP_CONTAINER_ID);

export const setup = (App: ComponentType) => {
render(
<Provider store={store}>
<App />
</Provider>,
<ErrorBoundary fallbackRender={fallbackRender}>
<Provider store={store}>
<App />
</Provider>
</ErrorBoundary>,
container!,
);
const id = setInterval(() => {
Expand All @@ -22,3 +25,19 @@ export const setup = (App: ComponentType) => {
}
}, 500);
};
function fallbackRender({ error }: FallbackProps) {
return (
<div role="alert" className="p-10">
<p>Something went wrong.</p>
<p>
Please open an issue at{' '}
<a href="https://github.com/ikkz/anki-template/issues">
https://github.com/ikkz/anki-template/issues
</a>{' '}
with screenshot
</p>
<pre style={{ color: 'red' }}>{error?.message}</pre>
<pre style={{ color: 'red' }}>{error?.stack}</pre>
</div>
);
}
13 changes: 8 additions & 5 deletions src/entries/mcq.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -18,13 +18,12 @@ import { useAutoAnimate } from '@formkit/auto-animate/preact';
import useCreation from 'ahooks/es/useCreation';
import useMemoizedFn from 'ahooks/es/useMemoizedFn';
import useSelections from 'ahooks/es/useSelections';
import useTimeout from 'ahooks/es/useTimeout';
import { locale } from 'at/locale';
import { fields } from 'at/options';
import clsx from 'clsx';
import { useAtomValue } from 'jotai';
import { useEffect } from 'react';
import { shuffle } from 'remeda';
import { doNothing, shuffle } from 'remeda';

const fieldToAlpha = (field: string) => field.slice(field.length - 1);

Expand Down Expand Up @@ -90,11 +89,15 @@ export default () => {
});

const [parent] = useAutoAnimate();
useTimeout(() => {
useEffect(() => {
if (back) {
setOptions(originOptions);
const timeout = setTimeout(() => {
setOptions(originOptions);
}, 600);
return () => clearTimeout(timeout);
}
}, 600);
return doNothing;
}, [back]);

const note = useField('note');
const isMultipleChoice = answers.length > 1;
Expand Down

0 comments on commit 101e6c9

Please sign in to comment.