Skip to content

Commit 567ef5a

Browse files
committed
chore: 자주 사용될 ErrorFallback에 대해 별도의 컴포넌트로 분리
1 parent 3fa6d9e commit 567ef5a

File tree

3 files changed

+57
-40
lines changed

3 files changed

+57
-40
lines changed
Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
import * as React from "react";
2+
3+
import { Button, Typography } from "@mui/material";
4+
import { Suspense } from "@suspensive/react";
5+
6+
import CommonContext from '../hooks/';
7+
8+
const DetailedErrorFallback: React.FC<{ error: Error, reset: () => void }> = ({ error, reset }) => {
9+
const errorObject = Object.getOwnPropertyNames(error).reduce((acc, key) => ({ ...acc, [key]: (error as unknown as { [key: string]: unknown })[key] }), {});
10+
return <>
11+
<Typography variant="body2" color="error">error.message = {error.message}</Typography>
12+
<details open>
13+
<summary>오류 상세</summary>
14+
<pre style={{
15+
whiteSpace: "pre-wrap",
16+
backgroundColor: "#f5f5f5",
17+
padding: "1em",
18+
borderRadius: "4px",
19+
userSelect: "text",
20+
}}>
21+
<code>{JSON.stringify(errorObject, null, 2)}</code>
22+
</pre>
23+
</details>
24+
<br />
25+
<Button variant="outlined" onClick={reset}>다시 시도</Button>
26+
</>;
27+
};
28+
29+
const SimplifiedErrorFallback: React.FC<{ reset: () => void }> = ({ reset }) => {
30+
return <>
31+
<Typography variant="body2" color="error">
32+
문제가 발생했습니다, 잠시 후 다시 시도해주세요.<br />
33+
만약 문제가 계속 발생한다면, 파이콘 한국 준비 위원회에게 알려주세요!<br />
34+
<br />
35+
An error occurred, please try again later.<br />
36+
If the problem persists, please let the PyCon Korea organizing committee know!
37+
</Typography>
38+
<br />
39+
<Button variant="outlined" onClick={reset}>다시 시도 | Retry</Button>
40+
</>;
41+
}
42+
43+
export const ErrorFallback: React.FC<{ error: Error, reset: () => void }> = ({ error, reset }) => {
44+
const InnerErrorFallback: React.FC<{ error: Error, reset: () => void }> = ({ error, reset }) => {
45+
const { debug } = CommonContext.useCommonContext();
46+
return debug ? <DetailedErrorFallback error={error} reset={reset} /> : <SimplifiedErrorFallback reset={reset} />;
47+
}
48+
49+
return <Suspense fallback={<>로딩 중...</>}><InnerErrorFallback error={error} reset={reset} /></Suspense>
50+
}
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,13 @@
11
import { CommonContextProvider as CommonContextProviderComponent } from './common_context';
2+
import { ErrorFallback as ErrorFallbackComponent } from './error_handler';
23
import { MDXRenderer as MDXRendererComponent } from "./mdx";
34
import { PythonKorea as PythonKoreaComponent } from './pythonkorea';
45

56
namespace Components {
67
export const CommonContextProvider = CommonContextProviderComponent;
78
export const MDXRenderer = MDXRendererComponent;
89
export const PythonKorea = PythonKoreaComponent;
10+
export const ErrorFallback = ErrorFallbackComponent;
911
}
1012

1113
export default Components;

packages/common/src/components/mdx.tsx

Lines changed: 5 additions & 40 deletions
Original file line numberDiff line numberDiff line change
@@ -3,12 +3,13 @@ import * as runtime from "react/jsx-runtime";
33
import * as R from "remeda";
44

55
import { evaluate, EvaluateOptions } from "@mdx-js/mdx";
6-
import { Button, CircularProgress, Typography } from "@mui/material";
6+
import { CircularProgress } from "@mui/material";
77
import { ErrorBoundary, Suspense } from "@suspensive/react";
88
import { useSuspenseQuery } from "@tanstack/react-query";
99
import components, { MuiMdxComponentsOptions } from 'mui-mdx-components';
1010

11-
import { useCommonContext } from '../hooks/useCommonContext';
11+
import Components from "../components";
12+
import Hooks from "../hooks";
1213

1314
const MDXComponents: MuiMdxComponentsOptions = {
1415
overrides: {
@@ -26,41 +27,6 @@ const MDXComponents: MuiMdxComponentsOptions = {
2627
}
2728
}
2829

29-
const SimplifiedMDXErrorFallback: React.FC<{ reset: () => void }> = ({ reset }) => {
30-
return <>
31-
<Typography variant="body2" color="error">
32-
페이지를 그리던 중 문제가 발생했습니다, 잠시 후 다시 시도해주세요.<br />
33-
만약 문제가 계속 발생한다면, 파이콘 한국 준비 위원회에게 알려주세요!<br />
34-
<br />
35-
Problem occurred while drawing the page, please try again later.<br />
36-
If the problem persists, please let the PyCon Korea organizing committee know!
37-
</Typography>
38-
<br />
39-
<Button variant="outlined" onClick={reset}>다시 시도 | Retry</Button>
40-
</>;
41-
}
42-
43-
const DetailedMDXErrorFallback: React.FC<{ error: Error, reset: () => void }> = ({ error, reset }) => {
44-
const errorObject = Object.getOwnPropertyNames(error).reduce((acc, key) => ({ ...acc, [key]: (error as unknown as { [key: string]: unknown })[key] }), {});
45-
return <>
46-
<Typography variant="body2" color="error">MDX 변환 오류: {error.message}</Typography>
47-
<details open>
48-
<summary>오류 상세</summary>
49-
<pre style={{
50-
whiteSpace: "pre-wrap",
51-
backgroundColor: "#f5f5f5",
52-
padding: "1em",
53-
borderRadius: "4px",
54-
userSelect: "text",
55-
}}>
56-
<code>{JSON.stringify(errorObject, null, 2)}</code>
57-
</pre>
58-
</details>
59-
<br />
60-
<Button variant="outlined" onClick={reset}>다시 시도</Button>
61-
</>;
62-
};
63-
6430
const InnerMDXRenderer: React.FC<{ text: string, baseUrl: string }> = ({ text, baseUrl }) => {
6531
const options: EvaluateOptions = { ...runtime, baseUrl };
6632

@@ -92,16 +58,15 @@ const lineFormatterForMDX = (line: string) => {
9258
export const MDXRenderer: React.FC<{ text: string }> = ({ text }) => {
9359
// 원래 MDX는 각 줄의 마지막에 공백 2개가 있어야 줄바꿈이 되고, 또 연속 줄바꿈은 무시되지만,
9460
// 편의성을 위해 렌더러 단에서 공백 2개를 추가하고 연속 줄바꿈을 <br />로 변환합니다.
95-
const { baseUrl, debug } = useCommonContext();
61+
const { baseUrl, debug } = Hooks.useCommonContext();
9662

97-
const ErrorHandler = debug ? DetailedMDXErrorFallback : SimplifiedMDXErrorFallback;
9863
const processedText = text
9964
.split("\n")
10065
.map(lineFormatterForMDX)
10166
.join("")
10267
.replaceAll("\n\n", "\n<br />\n");
10368

104-
return <ErrorBoundary fallback={ErrorHandler}>
69+
return <ErrorBoundary fallback={Components.ErrorFallback}>
10570
<Suspense fallback={<CircularProgress />}>
10671
<InnerMDXRenderer text={processedText} baseUrl={baseUrl} />
10772
</Suspense>

0 commit comments

Comments
 (0)