Skip to content

Commit e09727c

Browse files
authored
Merge pull request #99 from hufs-sports-live/feat/error-boundary
[FEAT] 에러 처리 및 스켈레톤, 로더 추가
2 parents f950673 + faa13e0 commit e09727c

File tree

16 files changed

+537
-72
lines changed

16 files changed

+537
-72
lines changed

src/app/match/[id]/page.tsx

Lines changed: 78 additions & 49 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,9 @@
11
'use client';
22

3-
import { Suspense, useRef, useState } from 'react';
3+
import { useRef, useState } from 'react';
44

5+
import AsyncBoundary from '@/components/common/AsyncBoundary';
6+
import Loader from '@/components/common/Loader';
57
import MatchBanner from '@/components/match/Banner';
68
import Cheer from '@/components/match/Cheer';
79
import CommentForm from '@/components/match/CommentForm';
@@ -54,72 +56,99 @@ export default function Match({ params }: { params: { id: string } }) {
5456

5557
return (
5658
<section>
57-
<Suspense fallback={<div>배너 로딩중...</div>}>
59+
<AsyncBoundary
60+
errorFallback={props => <MatchBanner.ErrorFallback {...props} />}
61+
loadingFallback={<MatchBanner.Skeleton />}
62+
>
5863
<MatchByIdFetcher matchId={params.id}>
5964
{data => <MatchBanner {...data} />}
6065
</MatchByIdFetcher>
61-
</Suspense>
62-
<Suspense fallback={<div>응원 로딩중...</div>}>
66+
</AsyncBoundary>
67+
<AsyncBoundary
68+
errorFallback={props => <Cheer.ErrorFallback {...props} />}
69+
loadingFallback={<div>응원 로딩중...</div>}
70+
>
6371
<MatchCheerByIdFetcher matchId={params.id}>
6472
{data => <Cheer cheers={data} />}
6573
</MatchCheerByIdFetcher>
66-
</Suspense>
67-
74+
</AsyncBoundary>
6875
<Panel options={options} defaultValue="라인업">
6976
{({ selected }) => (
70-
<Suspense fallback={<div>로딩중...</div>}>
77+
<>
7178
{selected === '라인업' && (
72-
<MatchLineupFetcher matchId={params.id}>
73-
{([firstTeam, secondTeam]) => (
74-
<div className="grid grid-cols-2 py-5 [&>*:first-child>ul]:before:absolute [&>*:first-child>ul]:before:right-0 [&>*:first-child>ul]:before:h-full [&>*:first-child>ul]:before:border-l-2 [&>*:first-child>ul]:before:bg-gray-2">
75-
<Lineup {...firstTeam} />
76-
<Lineup {...secondTeam} />
77-
</div>
78-
)}
79-
</MatchLineupFetcher>
79+
<AsyncBoundary
80+
errorFallback={props => <Lineup.ErrorFallback {...props} />}
81+
loadingFallback={<Loader />}
82+
>
83+
<MatchLineupFetcher matchId={params.id}>
84+
{([firstTeam, secondTeam]) => (
85+
<div className="grid grid-cols-2 py-5 [&>*:first-child>ul]:before:absolute [&>*:first-child>ul]:before:right-0 [&>*:first-child>ul]:before:h-full [&>*:first-child>ul]:before:border-l-2 [&>*:first-child>ul]:before:bg-gray-2">
86+
<Lineup {...firstTeam} />
87+
<Lineup {...secondTeam} />
88+
</div>
89+
)}
90+
</MatchLineupFetcher>
91+
</AsyncBoundary>
8092
)}
8193
{selected === '타임라인' && (
82-
<MatchTimelineFetcher matchId={params.id}>
83-
{([firstHalf, secondHalf]) => (
84-
<div className="overflow-y-auto p-5">
85-
<RecordList {...firstHalf} />
86-
<RecordList {...secondHalf} />
87-
</div>
88-
)}
89-
</MatchTimelineFetcher>
94+
<AsyncBoundary
95+
errorFallback={props => <RecordList.ErrorFallback {...props} />}
96+
loadingFallback={<Loader />}
97+
>
98+
<MatchTimelineFetcher matchId={params.id}>
99+
{([firstHalf, secondHalf]) => (
100+
<div className="overflow-y-auto p-5">
101+
<RecordList {...firstHalf} />
102+
<RecordList {...secondHalf} />
103+
</div>
104+
)}
105+
</MatchTimelineFetcher>
106+
</AsyncBoundary>
90107
)}
91108
{selected === '응원댓글' && (
92-
<MatchCommentFetcher matchId={params.id}>
93-
{({ commentList, ...data }) => (
94-
<div className="max-h-[450px] overflow-y-auto p-5">
95-
<ul className="pb-8">
96-
<CommentList
97-
commentList={commentList.pages.flat()}
109+
<AsyncBoundary
110+
errorFallback={props => (
111+
<CommentList.ErrorFallback {...props} />
112+
)}
113+
loadingFallback={<Loader />}
114+
>
115+
<MatchCommentFetcher matchId={params.id}>
116+
{({ commentList, ...data }) => (
117+
<div className="max-h-[450px] overflow-y-auto p-5">
118+
<ul className="pb-8">
119+
<CommentList
120+
commentList={commentList.pages.flat()}
121+
scrollToBottom={scrollToBottom}
122+
{...data}
123+
/>
124+
<CommentList.SocketList commentList={comments} />
125+
<li ref={scrollRef}></li>
126+
</ul>
127+
<CommentForm
128+
matchId={params.id}
129+
mutate={mutate}
98130
scrollToBottom={scrollToBottom}
99-
{...data}
100131
/>
101-
<CommentList.SocketList commentList={comments} />
102-
<li ref={scrollRef}></li>
103-
</ul>
104-
<CommentForm
105-
matchId={params.id}
106-
mutate={mutate}
107-
scrollToBottom={scrollToBottom}
108-
/>
109-
</div>
110-
)}
111-
</MatchCommentFetcher>
132+
</div>
133+
)}
134+
</MatchCommentFetcher>
135+
</AsyncBoundary>
112136
)}
113137
{selected === '경기영상' && (
114-
<MatchVideoFetcher matchId={params.id}>
115-
{data => (
116-
<div className="overflow-y-auto p-5">
117-
<Video {...data} />
118-
</div>
119-
)}
120-
</MatchVideoFetcher>
138+
<AsyncBoundary
139+
errorFallback={props => <Video.ErrorFallback {...props} />}
140+
loadingFallback={<Loader />}
141+
>
142+
<MatchVideoFetcher matchId={params.id}>
143+
{data => (
144+
<div className="overflow-y-auto p-5">
145+
<Video {...data} />
146+
</div>
147+
)}
148+
</MatchVideoFetcher>
149+
</AsyncBoundary>
121150
)}
122-
</Suspense>
151+
</>
123152
)}
124153
</Panel>
125154
</section>

src/app/page.tsx

Lines changed: 17 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
'use client';
22

3-
import { Suspense } from 'react';
4-
3+
import AsyncBoundary from '@/components/common/AsyncBoundary';
54
import SportsList from '@/components/league/SportsList';
65
import MatchList from '@/components/match/MatchList';
76
import { QUERY_PARAMS } from '@/constants/queryParams';
@@ -22,7 +21,10 @@ export default function Home() {
2221

2322
return (
2423
<section className="flex flex-col items-center">
25-
<Suspense>
24+
<AsyncBoundary
25+
errorFallback={() => <div>에러</div>}
26+
loadingFallback={<SportsList.Skeleton />}
27+
>
2628
<SportsListFetcher leagueId={params.get(QUERY_PARAMS.league) || '1'}>
2729
{data => (
2830
<SportsList
@@ -32,7 +34,7 @@ export default function Home() {
3234
/>
3335
)}
3436
</SportsListFetcher>
35-
</Suspense>
37+
</AsyncBoundary>
3638

3739
<div className="mb-8 flex w-fit items-center gap-5 rounded-xl bg-gray-2 text-center">
3840
<button
@@ -67,15 +69,18 @@ export default function Home() {
6769
</button>
6870
</div>
6971

70-
<div className="flex flex-col gap-8">
71-
<Suspense fallback={<div>MatchList 로딩중...</div>}>
72-
<MatchListFetcher {...paramsObj}>
73-
{({ matchList, ...props }) => (
72+
<AsyncBoundary
73+
errorFallback={props => <MatchList.ErrorFallback {...props} />}
74+
loadingFallback={<MatchList.Skeleton />}
75+
>
76+
<MatchListFetcher {...paramsObj}>
77+
{({ matchList, ...props }) => (
78+
<div className="flex w-full flex-col gap-8">
7479
<MatchList matchList={matchList.pages.flat()} {...props} />
75-
)}
76-
</MatchListFetcher>
77-
</Suspense>
78-
</div>
80+
</div>
81+
)}
82+
</MatchListFetcher>
83+
</AsyncBoundary>
7984
</section>
8085
);
8186
}
Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
import { QueryErrorResetBoundary } from '@tanstack/react-query';
2+
import { ReactNode, Suspense } from 'react';
3+
4+
import ErrorBoundary, { FallbackProps } from '../ErrorBoundary';
5+
6+
type AsyncBoundaryProps = {
7+
errorFallback: (props: FallbackProps) => ReactNode;
8+
loadingFallback: ReactNode;
9+
children: ReactNode;
10+
};
11+
12+
export default function AsyncBoundary({
13+
errorFallback,
14+
loadingFallback,
15+
children,
16+
}: AsyncBoundaryProps) {
17+
return (
18+
<QueryErrorResetBoundary>
19+
{({ reset }) => (
20+
<ErrorBoundary fallback={errorFallback} onReset={reset}>
21+
<Suspense fallback={loadingFallback}>{children}</Suspense>
22+
</ErrorBoundary>
23+
)}
24+
</QueryErrorResetBoundary>
25+
);
26+
}
Lines changed: 65 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,65 @@
1+
import { Component, ComponentType, createElement, ReactNode } from 'react';
2+
3+
type ErrorBoundaryState = {
4+
hasError: boolean;
5+
error: Error | null;
6+
};
7+
8+
export type FallbackProps = {
9+
error: Error | null;
10+
resetErrorBoundary: () => void;
11+
};
12+
13+
type ErrorBoundaryProps = {
14+
children: ReactNode;
15+
onReset: () => void;
16+
fallback: ComponentType<FallbackProps>;
17+
};
18+
19+
export default class ErrorBoundary extends Component<
20+
ErrorBoundaryProps,
21+
ErrorBoundaryState
22+
> {
23+
constructor(props: ErrorBoundaryProps) {
24+
super(props);
25+
26+
this.state = {
27+
hasError: false,
28+
error: null,
29+
};
30+
31+
this.resetErrorBoundary = this.resetErrorBoundary.bind(this);
32+
}
33+
34+
static getDerivedStateFromError(error: Error): ErrorBoundaryState {
35+
return {
36+
hasError: true,
37+
error,
38+
};
39+
}
40+
41+
resetErrorBoundary(): void {
42+
this.props.onReset();
43+
44+
this.setState({
45+
hasError: false,
46+
error: null,
47+
});
48+
}
49+
50+
render() {
51+
const { state, props, resetErrorBoundary } = this;
52+
53+
const { hasError, error } = state;
54+
const { fallback, children } = props;
55+
56+
const fallbackProps: FallbackProps = {
57+
error,
58+
resetErrorBoundary,
59+
};
60+
61+
const fallbackComponent = createElement(fallback, fallbackProps);
62+
63+
return hasError ? fallbackComponent : children;
64+
}
65+
}
Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
export default function Loader() {
2+
return (
3+
<div
4+
role="status"
5+
className="flex w-full items-center justify-center py-10"
6+
>
7+
<svg
8+
aria-hidden="true"
9+
className="h-8 w-8 animate-spin fill-primary text-gray-200 dark:text-gray-600"
10+
viewBox="0 0 100 101"
11+
fill="none"
12+
xmlns="http://www.w3.org/2000/svg"
13+
>
14+
<path
15+
d="M100 50.5908C100 78.2051 77.6142 100.591 50 100.591C22.3858 100.591 0 78.2051 0 50.5908C0 22.9766 22.3858 0.59082 50 0.59082C77.6142 0.59082 100 22.9766 100 50.5908ZM9.08144 50.5908C9.08144 73.1895 27.4013 91.5094 50 91.5094C72.5987 91.5094 90.9186 73.1895 90.9186 50.5908C90.9186 27.9921 72.5987 9.67226 50 9.67226C27.4013 9.67226 9.08144 27.9921 9.08144 50.5908Z"
16+
fill="currentColor"
17+
/>
18+
<path
19+
d="M93.9676 39.0409C96.393 38.4038 97.8624 35.9116 97.0079 33.5539C95.2932 28.8227 92.871 24.3692 89.8167 20.348C85.8452 15.1192 80.8826 10.7238 75.2124 7.41289C69.5422 4.10194 63.2754 1.94025 56.7698 1.05124C51.7666 0.367541 46.6976 0.446843 41.7345 1.27873C39.2613 1.69328 37.813 4.19778 38.4501 6.62326C39.0873 9.04874 41.5694 10.4717 44.0505 10.1071C47.8511 9.54855 51.7191 9.52689 55.5402 10.0491C60.8642 10.7766 65.9928 12.5457 70.6331 15.2552C75.2735 17.9648 79.3347 21.5619 82.5849 25.841C84.9175 28.9121 86.7997 32.2913 88.1811 35.8758C89.083 38.2158 91.5421 39.6781 93.9676 39.0409Z"
20+
fill="currentFill"
21+
/>
22+
</svg>
23+
<span className="sr-only">Loading...</span>
24+
</div>
25+
);
26+
}

src/components/common/MatchCard/pieces/Label.tsx

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -8,17 +8,17 @@ type LabelProps = {
88

99
export default function Label({ className }: LabelProps) {
1010
const { gameName, sportsName, startTime } = useMatchCardContext();
11-
const { year, month, date, weekday } = parseTimeString(startTime);
11+
const { month, date, weekday, period, hours, minutes } =
12+
parseTimeString(startTime);
1213

1314
return (
14-
<div className={$(className)}>
15-
{startTime && (
16-
<time>
17-
{year}. {month}. {date}. ({weekday})
18-
</time>
19-
)}
20-
{sportsName && <div className="text-center">{sportsName}</div>}
21-
{gameName && <div className="text-right">{gameName}</div>}
15+
<div className={$('flex items-center justify-between', className)}>
16+
<time>
17+
{month}. {date}. {weekday}요일 {period} {hours}:{minutes}
18+
</time>
19+
<div className="text-right">
20+
{sportsName} {gameName}
21+
</div>
2222
</div>
2323
);
2424
}

src/components/league/SportsList/index.tsx

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -36,3 +36,14 @@ export default function SportsList({
3636
</ul>
3737
);
3838
}
39+
40+
SportsList.Skeleton = function Skeleton() {
41+
return (
42+
<ul className="mb-5 flex h-[40px] w-full max-w-md items-center justify-start gap-5 overflow-hidden">
43+
<li className="text-gary-5 h-[40px] w-[70px] animate-pulse cursor-pointer rounded-xl bg-gray-2"></li>
44+
<li className="text-gary-5 h-[40px] w-[70px] animate-pulse cursor-pointer rounded-xl bg-gray-2"></li>
45+
<li className="text-gary-5 h-[40px] w-[70px] animate-pulse cursor-pointer rounded-xl bg-gray-2"></li>
46+
<li className="text-gary-5 h-[40px] w-[70px] animate-pulse cursor-pointer rounded-xl bg-gray-2"></li>
47+
</ul>
48+
);
49+
};

0 commit comments

Comments
 (0)