Skip to content

Commit ddb0a0a

Browse files
committed
fix: suspensable
1 parent 9eab58f commit ddb0a0a

File tree

2 files changed

+40
-33
lines changed

2 files changed

+40
-33
lines changed

src/components/Suspensable.tsx

Lines changed: 26 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -19,11 +19,14 @@ interface SuspensableProps {
1919
export const Suspensable: FCC<SuspensableProps> = ({
2020
children,
2121
retryDeps = [],
22-
pendingTitle = '加载中',
22+
pendingTitle,
2323
fetcher,
2424
errorFallback,
2525
}) => {
2626
const resetError = useRef<() => void>()
27+
const { t } = useTranslation()
28+
29+
pendingTitle = pendingTitle ?? t('components.Suspensable.loading')
2730

2831
useEffect(() => {
2932
resetError.current?.()
@@ -44,8 +47,12 @@ export const Suspensable: FCC<SuspensableProps> = ({
4447
return (
4548
<NonIdealState
4649
icon="issue"
47-
title="加载失败"
48-
description={fetcher ? '数据加载失败,请重试' : error.message}
50+
title={t('components.Suspensable.loadFailed')}
51+
description={
52+
fetcher
53+
? t('components.Suspensable.dataLoadFailedRetry')
54+
: error.message
55+
}
4956
className="py-8"
5057
action={
5158
fetcher && (
@@ -58,7 +65,7 @@ export const Suspensable: FCC<SuspensableProps> = ({
5865
fetcher()
5966
}}
6067
>
61-
重试
68+
{t('components.Suspensable.retry')}
6269
</Button>
6370
)
6471
}
@@ -94,43 +101,31 @@ export function withSuspensable<P extends object>(
94101
Component: ComponentType<P>,
95102
options: SuspensableOptions = {},
96103
): FC<P> {
97-
const { pendingTitle, retryOnChange = [] } = options
104+
const { pendingTitle, retryOnChange = [], errorFallback } = options
98105

99106
const SuspensableComponent: FC<P> = (props) => {
100-
const { t } = useTranslation()
101-
const resetErrorRef = useRef<(() => void) | undefined>()
102-
103-
useEffect(
104-
() => {
105-
resetErrorRef.current?.()
106-
resetErrorRef.current = undefined
107-
// eslint-disable-next-line react-hooks/exhaustive-deps
108-
},
109-
retryOnChange.map((key) => (props as any)[key]),
110-
)
111-
112-
const title =
113-
typeof pendingTitle === 'function'
114-
? pendingTitle(t)
115-
: pendingTitle || t('components.Suspensable.loading')
107+
const retryDeps = retryOnChange.map((key) => (props as any)[key])
116108

117109
return (
118-
<Suspense
119-
fallback={
120-
<div className="flex justify-center p-8">
121-
<div className="flex items-center gap-4">
122-
<Spinner size={20} />
123-
<div>{title}</div>
124-
</div>
125-
</div>
110+
<Suspensable
111+
pendingTitle={
112+
typeof pendingTitle === 'function'
113+
? pendingTitle(useTranslation().t)
114+
: pendingTitle
115+
}
116+
retryDeps={retryDeps}
117+
errorFallback={({ error }) =>
118+
errorFallback
119+
? errorFallback({ error, resetError: () => {} })
120+
: undefined
126121
}
127122
>
128123
<Component {...props} />
129-
</Suspense>
124+
</Suspensable>
130125
)
131126
}
132127

133-
// Debug
128+
// Format for display in DevTools
134129
SuspensableComponent.displayName = `Suspensable(${
135130
Component.displayName || Component.name || 'Component'
136131
})`

src/i18n/translations.json

Lines changed: 14 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2515,8 +2515,20 @@
25152515
},
25162516
"Suspensable": {
25172517
"loading": {
2518-
"cn": "加载中...",
2519-
"en": "Loading..."
2518+
"cn": "加载中",
2519+
"en": "Loading"
2520+
},
2521+
"loadFailed": {
2522+
"cn": "加载失败",
2523+
"en": "Load Failed"
2524+
},
2525+
"dataLoadFailedRetry": {
2526+
"cn": "数据加载失败,请重试",
2527+
"en": "Data load failed, please retry"
2528+
},
2529+
"retry": {
2530+
"cn": "重试",
2531+
"en": "Retry"
25202532
}
25212533
},
25222534
"UserFilter": {

0 commit comments

Comments
 (0)