1
1
import React , { useState } from 'react' ;
2
- import { useTranslation , Trans } from 'react-i18next' ; // 确保从'react-i18next'导入了Trans
2
+ import { useTranslation , Trans } from 'react-i18next' ;
3
3
import useStore from '@store/store' ;
4
4
import useHideOnOutsideClick from '@hooks/useHideOnOutsideClick' ;
5
5
import PopupModal from '@components/PopupModal' ;
@@ -9,16 +9,12 @@ import DownChevronArrow from '@icon/DownChevronArrow';
9
9
const ApiMenu = ( { setIsModalOpen } : { setIsModalOpen : React . Dispatch < React . SetStateAction < boolean > > ; } ) => {
10
10
const { t } = useTranslation ( [ 'main' , 'api' ] ) ;
11
11
12
- const apiKey = useStore ( ( state ) => state . apiKey ) ;
13
12
const setApiKey = useStore ( ( state ) => state . setApiKey ) ;
14
- const apiEndpoint = useStore ( ( state ) => state . apiEndpoint ) ;
15
13
const setApiEndpoint = useStore ( ( state ) => state . setApiEndpoint ) ;
16
14
17
- const [ _apiKey , _setApiKey ] = useState < string > ( apiKey || '' ) ;
18
- const [ _apiEndpoint , _setApiEndpoint ] = useState < string > ( apiEndpoint ) ;
19
- const [ _customEndpoint , _setCustomEndpoint ] = useState < boolean > (
20
- ! availableEndpoints . includes ( apiEndpoint )
21
- ) ;
15
+ const [ _apiEndpoint , _setApiEndpoint ] = useState < string > ( defaultAPIEndpoint ) ;
16
+ const [ _apiKey , _setApiKey ] = useState < string > ( '' ) ;
17
+ const [ _customEndpoint , _setCustomEndpoint ] = useState < boolean > ( false ) ;
22
18
23
19
const handleSave = ( ) => {
24
20
setApiKey ( _apiKey ) ;
@@ -27,12 +23,28 @@ const ApiMenu = ({ setIsModalOpen }: { setIsModalOpen: React.Dispatch<React.SetS
27
23
} ;
28
24
29
25
const handleToggleCustomEndpoint = ( ) => {
30
- if ( _customEndpoint ) _setApiEndpoint ( defaultAPIEndpoint ) ;
31
- else _setApiEndpoint ( '' ) ;
26
+ if ( _customEndpoint ) {
27
+ _setApiEndpoint ( defaultAPIEndpoint ) ;
28
+ _setApiKey ( import . meta. env . VITE_DEFAULT_API_KEY ) ;
29
+ } else {
30
+ _setApiEndpoint ( '' ) ;
31
+ _setApiKey ( '' ) ;
32
+ }
32
33
_setCustomEndpoint ( prev => ! prev ) ;
33
34
} ;
34
35
35
- const preventCopy = ( e : React . ClipboardEvent < HTMLInputElement > ) => { // 明确指定e的类型
36
+ const handleApiEndpointChange = ( newEndpoint : string ) => {
37
+ _setApiEndpoint ( newEndpoint ) ;
38
+ if ( newEndpoint === import . meta. env . VITE_OFFICIAL_API_ENDPOINT ) {
39
+ _setApiKey ( import . meta. env . VITE_OFFICIAL_API_KEY ) ;
40
+ } else if ( newEndpoint === import . meta. env . VITE_CUSTOM_API_ENDPOINT ) {
41
+ _setApiKey ( import . meta. env . VITE_CUSTOM_API_KEY ) ;
42
+ } else {
43
+ _setApiKey ( '' ) ;
44
+ }
45
+ } ;
46
+
47
+ const preventCopy = ( e : React . ClipboardEvent < HTMLInputElement > ) => {
36
48
e . preventDefault ( ) ;
37
49
alert ( "Copying API key is not allowed." ) ;
38
50
} ;
@@ -63,12 +75,12 @@ const ApiMenu = ({ setIsModalOpen }: { setIsModalOpen: React.Dispatch<React.SetS
63
75
type = 'password'
64
76
className = 'text-gray-800 dark:text-white p-3 text-sm border-none bg-gray-200 dark:bg-gray-600 rounded-md m-0 w-full mr-0 h-8 focus:outline-none'
65
77
value = { _apiEndpoint }
66
- onChange = { ( e ) => _setApiEndpoint ( e . target . value ) }
78
+ onChange = { ( e ) => handleApiEndpointChange ( e . target . value ) }
67
79
/>
68
80
) : (
69
81
< ApiEndpointSelector
70
82
_apiEndpoint = { _apiEndpoint }
71
- _setApiEndpoint = { _setApiEndpoint }
83
+ handleApiEndpointChange = { handleApiEndpointChange }
72
84
/>
73
85
) }
74
86
</ div >
@@ -112,10 +124,10 @@ const ApiMenu = ({ setIsModalOpen }: { setIsModalOpen: React.Dispatch<React.SetS
112
124
113
125
const ApiEndpointSelector = ( {
114
126
_apiEndpoint,
115
- _setApiEndpoint ,
127
+ handleApiEndpointChange ,
116
128
} : {
117
129
_apiEndpoint : string ;
118
- _setApiEndpoint : React . Dispatch < React . SetStateAction < string > > ;
130
+ handleApiEndpointChange : ( endpoint : string ) => void ;
119
131
} ) => {
120
132
const [ dropDown , setDropDown , dropDownRef ] = useHideOnOutsideClick ( ) ;
121
133
@@ -143,7 +155,7 @@ const ApiEndpointSelector = ({
143
155
< li
144
156
className = 'px-4 py-2 hover:bg-gray-100 dark:hover:bg-gray-600 dark:hover:text-white cursor-pointer truncate'
145
157
onClick = { ( ) => {
146
- _setApiEndpoint ( endpoint ) ;
158
+ handleApiEndpointChange ( endpoint ) ;
147
159
setDropDown ( false ) ;
148
160
} }
149
161
key = { endpoint }
@@ -158,3 +170,5 @@ const ApiEndpointSelector = ({
158
170
} ;
159
171
160
172
export default ApiMenu ;
173
+
174
+
0 commit comments