@@ -29,7 +29,18 @@ const Match: React.FC<{
2929 const [ excludeVisible , setExcludeVisible ] = useState < boolean > ( false ) ;
3030 const { t } = useTranslation ( ) ; // 使用 react-i18next 的 useTranslation 钩子函数获取翻译函数
3131
32- useEffect ( ( ) => {
32+ // 自定义的状态更新函数,会在更新后自动刷新数据
33+ const updateMatchAndRefresh = ( newMatch : MatchItem [ ] ) => {
34+ setMatch ( newMatch ) ;
35+ refreshMatch ( ) ;
36+ } ;
37+
38+ const updateExcludeAndRefresh = ( newExclude : MatchItem [ ] ) => {
39+ setExclude ( newExclude ) ;
40+ refreshMatch ( ) ;
41+ } ;
42+
43+ const refreshMatch = ( ) => {
3344 if ( script ) {
3445 // 从数据库中获取是简单处理数据一致性的问题
3546 scriptDAO . get ( script . uuid ) . then ( ( res ) => {
@@ -72,7 +83,11 @@ const Match: React.FC<{
7283 setExclude ( e ) ;
7384 } ) ;
7485 }
75- } , [ script , exclude , match ] ) ;
86+ } ;
87+
88+ useEffect ( ( ) => {
89+ refreshMatch ( ) ;
90+ } , [ script ] ) ;
7691
7792 const columns : ColumnProps [ ] = [
7893 {
@@ -109,7 +124,7 @@ const Match: React.FC<{
109124 exclude . map ( ( m ) => m . match )
110125 )
111126 . then ( ( ) => {
112- setExclude ( [ ...exclude ] ) ;
127+ updateExcludeAndRefresh ( [ ...exclude ] ) ;
113128 // 如果包含在里面,再加回match
114129 if ( item . hasMatch ) {
115130 match . push ( item ) ;
@@ -120,7 +135,7 @@ const Match: React.FC<{
120135 match . map ( ( m ) => m . match )
121136 )
122137 . then ( ( ) => {
123- setMatch ( [ ...match ] ) ;
138+ updateMatchAndRefresh ( [ ...match ] ) ;
124139 } ) ;
125140 }
126141 } ) ;
@@ -143,7 +158,7 @@ const Match: React.FC<{
143158 match . map ( ( m ) => m . match )
144159 )
145160 . then ( ( ) => {
146- setMatch ( [ ...match ] ) ;
161+ updateMatchAndRefresh ( [ ...match ] ) ;
147162 // 添加到exclue
148163 if ( ! item . byUser ) {
149164 exclude . push ( item ) ;
@@ -153,7 +168,7 @@ const Match: React.FC<{
153168 exclude . map ( ( m ) => m . match )
154169 )
155170 . then ( ( ) => {
156- setExclude ( [ ...exclude ] ) ;
171+ updateExcludeAndRefresh ( [ ...exclude ] ) ;
157172 } ) ;
158173 }
159174 } ) ;
@@ -188,7 +203,7 @@ const Match: React.FC<{
188203 match . map ( ( m ) => m . match )
189204 )
190205 . then ( ( ) => {
191- setMatch ( [ ...match ] ) ;
206+ updateMatchAndRefresh ( [ ...match ] ) ;
192207 setMatchVisible ( false ) ;
193208 } ) ;
194209 }
@@ -220,7 +235,7 @@ const Match: React.FC<{
220235 exclude . map ( ( m ) => m . match )
221236 )
222237 . then ( ( ) => {
223- setExclude ( [ ...exclude ] ) ;
238+ updateExcludeAndRefresh ( [ ...exclude ] ) ;
224239 setExcludeVisible ( false ) ;
225240 } ) ;
226241 }
@@ -250,7 +265,7 @@ const Match: React.FC<{
250265 title = { t ( "confirm_reset" ) }
251266 onOk = { ( ) => {
252267 scriptClient . resetMatch ( script . uuid , undefined ) . then ( ( ) => {
253- setMatch ( [ ] ) ;
268+ updateMatchAndRefresh ( [ ] ) ;
254269 } ) ;
255270 } }
256271 >
@@ -279,7 +294,7 @@ const Match: React.FC<{
279294 title = { t ( "confirm_reset" ) }
280295 onOk = { ( ) => {
281296 scriptClient . resetExclude ( script . uuid , undefined ) . then ( ( ) => {
282- setExclude ( [ ] ) ;
297+ updateExcludeAndRefresh ( [ ] ) ;
283298 } ) ;
284299 } }
285300 >
0 commit comments