11import React , { useState } from 'react' ;
2- import { useTranslation , Trans } from 'react-i18next' ; // 确保从'react-i18next'导入了Trans
2+ import { useTranslation , Trans } from 'react-i18next' ;
33import useStore from '@store/store' ;
44import useHideOnOutsideClick from '@hooks/useHideOnOutsideClick' ;
55import PopupModal from '@components/PopupModal' ;
@@ -9,16 +9,12 @@ import DownChevronArrow from '@icon/DownChevronArrow';
99const ApiMenu = ( { setIsModalOpen } : { setIsModalOpen : React . Dispatch < React . SetStateAction < boolean > > ; } ) => {
1010 const { t } = useTranslation ( [ 'main' , 'api' ] ) ;
1111
12- const apiKey = useStore ( ( state ) => state . apiKey ) ;
1312 const setApiKey = useStore ( ( state ) => state . setApiKey ) ;
14- const apiEndpoint = useStore ( ( state ) => state . apiEndpoint ) ;
1513 const setApiEndpoint = useStore ( ( state ) => state . setApiEndpoint ) ;
1614
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 ) ;
2218
2319 const handleSave = ( ) => {
2420 setApiKey ( _apiKey ) ;
@@ -27,12 +23,28 @@ const ApiMenu = ({ setIsModalOpen }: { setIsModalOpen: React.Dispatch<React.SetS
2723 } ;
2824
2925 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+ }
3233 _setCustomEndpoint ( prev => ! prev ) ;
3334 } ;
3435
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 > ) => {
3648 e . preventDefault ( ) ;
3749 alert ( "Copying API key is not allowed." ) ;
3850 } ;
@@ -63,12 +75,12 @@ const ApiMenu = ({ setIsModalOpen }: { setIsModalOpen: React.Dispatch<React.SetS
6375 type = 'password'
6476 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'
6577 value = { _apiEndpoint }
66- onChange = { ( e ) => _setApiEndpoint ( e . target . value ) }
78+ onChange = { ( e ) => handleApiEndpointChange ( e . target . value ) }
6779 />
6880 ) : (
6981 < ApiEndpointSelector
7082 _apiEndpoint = { _apiEndpoint }
71- _setApiEndpoint = { _setApiEndpoint }
83+ handleApiEndpointChange = { handleApiEndpointChange }
7284 />
7385 ) }
7486 </ div >
@@ -112,10 +124,10 @@ const ApiMenu = ({ setIsModalOpen }: { setIsModalOpen: React.Dispatch<React.SetS
112124
113125const ApiEndpointSelector = ( {
114126 _apiEndpoint,
115- _setApiEndpoint ,
127+ handleApiEndpointChange ,
116128} : {
117129 _apiEndpoint : string ;
118- _setApiEndpoint : React . Dispatch < React . SetStateAction < string > > ;
130+ handleApiEndpointChange : ( endpoint : string ) => void ;
119131} ) => {
120132 const [ dropDown , setDropDown , dropDownRef ] = useHideOnOutsideClick ( ) ;
121133
@@ -143,7 +155,7 @@ const ApiEndpointSelector = ({
143155 < li
144156 className = 'px-4 py-2 hover:bg-gray-100 dark:hover:bg-gray-600 dark:hover:text-white cursor-pointer truncate'
145157 onClick = { ( ) => {
146- _setApiEndpoint ( endpoint ) ;
158+ handleApiEndpointChange ( endpoint ) ;
147159 setDropDown ( false ) ;
148160 } }
149161 key = { endpoint }
@@ -158,3 +170,5 @@ const ApiEndpointSelector = ({
158170} ;
159171
160172export default ApiMenu ;
173+
174+
0 commit comments