1
1
import { request } from '@/lib/request'
2
- import { useEffect , useState } from 'react'
2
+ import { useEffect , useState , useCallback , useRef } from 'react'
3
3
import { useSearchParams } from 'react-router-dom'
4
4
import { DashboardPage } from '@/components/config/plugin/plugins'
5
5
import { Spinner } from '@heroui/spinner'
@@ -25,23 +25,27 @@ export default function PluginConfigPage () {
25
25
info : {
26
26
id : '' ,
27
27
name : '' ,
28
- }
28
+ } ,
29
29
} )
30
30
31
31
const [ loading , setLoading ] = useState ( true )
32
32
const [ error , setError ] = useState < Error | null > ( null )
33
+ const requestSentRef = useRef ( false )
33
34
34
- const fetchConfig = async ( ) => {
35
+ const fetchConfig = useCallback ( async ( ) => {
35
36
if ( ! name || ! type ) return
37
+ // 如果已经发送过请求,则不再重复发送
38
+ if ( requestSentRef . current ) return
36
39
37
40
try {
38
41
setLoading ( true )
39
42
setError ( null )
43
+ requestSentRef . current = true // 标记请求已发送
40
44
const result = await request . serverPost < GetConfigResponse , GetConfigRequest > (
41
45
'/api/v1/plugin/config/get' ,
42
46
{
43
47
name,
44
- type
48
+ type,
45
49
}
46
50
)
47
51
setConfig ( result )
@@ -51,12 +55,17 @@ export default function PluginConfigPage () {
51
55
} finally {
52
56
setLoading ( false )
53
57
}
54
- }
58
+ } , [ name , type ] )
55
59
56
60
useEffect ( ( ) => {
57
61
if ( ! name || ! type ) return
58
62
fetchConfig ( )
59
- } , [ name , type ] )
63
+
64
+ // 组件卸载时重置标记,以便在组件重新挂载时可以再次发送请求
65
+ return ( ) => {
66
+ requestSentRef . current = false
67
+ }
68
+ } , [ name , type , fetchConfig ] )
60
69
61
70
if ( ! name || ! type ) {
62
71
return (
0 commit comments