@@ -19,11 +19,14 @@ interface SuspensableProps {
19
19
export const Suspensable : FCC < SuspensableProps > = ( {
20
20
children,
21
21
retryDeps = [ ] ,
22
- pendingTitle = '加载中' ,
22
+ pendingTitle,
23
23
fetcher,
24
24
errorFallback,
25
25
} ) => {
26
26
const resetError = useRef < ( ) => void > ( )
27
+ const { t } = useTranslation ( )
28
+
29
+ pendingTitle = pendingTitle ?? t ( 'components.Suspensable.loading' )
27
30
28
31
useEffect ( ( ) => {
29
32
resetError . current ?.( )
@@ -44,8 +47,12 @@ export const Suspensable: FCC<SuspensableProps> = ({
44
47
return (
45
48
< NonIdealState
46
49
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
+ }
49
56
className = "py-8"
50
57
action = {
51
58
fetcher && (
@@ -58,7 +65,7 @@ export const Suspensable: FCC<SuspensableProps> = ({
58
65
fetcher ( )
59
66
} }
60
67
>
61
- 重试
68
+ { t ( 'components.Suspensable.retry' ) }
62
69
</ Button >
63
70
)
64
71
}
@@ -94,43 +101,31 @@ export function withSuspensable<P extends object>(
94
101
Component : ComponentType < P > ,
95
102
options : SuspensableOptions = { } ,
96
103
) : FC < P > {
97
- const { pendingTitle, retryOnChange = [ ] } = options
104
+ const { pendingTitle, retryOnChange = [ ] , errorFallback } = options
98
105
99
106
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 ] )
116
108
117
109
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
126
121
}
127
122
>
128
123
< Component { ...props } />
129
- </ Suspense >
124
+ </ Suspensable >
130
125
)
131
126
}
132
127
133
- // Debug
128
+ // Format for display in DevTools
134
129
SuspensableComponent . displayName = `Suspensable(${
135
130
Component . displayName || Component . name || 'Component'
136
131
} )`
0 commit comments